This.
This is what I did.
Friday, January 31, 2014
Friday, January 17, 2014
ANIMATION, INPUT, AND FOODS OH MY (01.13.2014 - 01.17.2014)
if programming.level() > 9000:
print 'Ben Uleau'
Here are my programs:
Animation:
import pygame, sys, time
from pygame.locals import *
#Set up pygame
pygame.init
#set up window
WINDOWWIDTH=400
WINDOWHEIGHT=400
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
#set up direction variables
DOWNLEFT=1
DOWNRIGHT=3
UPLEFT=7
UPRIGHT=9
MOVESPEED=4
#set up colors
BLACK=(0, 0, 0)
RED=(255, 0, 0)
GREEN=(0, 255, 0)
BLUE=(0, 0, 255)
#Set up block data structure
b1={'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}
b2={'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}
b3={'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}
blocks=[b1, b2, b3]
#run the game loop
while True:
#check for the QUIT event
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
#draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
#move the block data structure
if b['dir']==DOWNLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==DOWNRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==UPLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top-=MOVESPEED
if b['dir']==UPRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top-=MOVESPEED
#Check if the block has moved out of the window
if b['rect'].top<0:
#block has moved past top
if b['dir']==UPLEFT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=DOWNRIGHT
if b['rect'].bottom>WINDOWHEIGHT:
#block has moved past bottom
if b['dir']==DOWNLEFT:
b['dir']=UPLEFT
if b['dir']==DOWNRIGHT:
b['dir']=UPRIGHT
if b['rect'].left<0:
#block has moved past left side
if b['dir']==DOWNLEFT:
b['dir']=DOWNRIGHT
if b['dir']==UPLEFT:
b['dir']=UPRIGHT
if b['rect'].right>WINDOWHEIGHT:
#block has moved past right side
if b['dir']==DOWNRIGHT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=UPLEFT
#Draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
#Draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
Keyboard input with pygame:
import pygame, sys, random
from pygame.locals import *
#Set up pygame
pygame.init()
mainClock=pygame.time.Clock()
#Set up the window
WINDOWWIDTH=400
WINDOWHEIGHT=400
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Input')
#Set up colors
BLACK=(0, 0, 0,)
GREEN=(0, 255, 0)
WHITE=(255, 255, 255)
#Set up the player and food data structure
foodCounter=0
NEWFOOD=40
FOODSIZE=20
player=pygame.Rect(300, 100, 50, 50)
foods=[]
for i in range(20):
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH-FOODSIZE), random.randint(0, WINDOWHEIGHT-FOODSIZE), FOODSIZE, FOODSIZE))
#set up movement variables
moveLeft=False
moveRight=False
moveUp=False
moveDown=False
MOVESPEED=6
#Run the game loop
while True:
#Check for events
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
#Change the keyboard variables
if event.key==K_LEFT or event.key==ord('a'):
moveRight=False
moveLeft=True
if event.key==K_RIGHT or event.key==ord('d'):
moveLeft=False
moveRight=True
if event.key==K_UP or event.key==ord('w'):
moveDown=False
moveUp=True
if event.key==K_DOWN or event.key==ord('s'):
moveUp=False
moveDown=True
if event.type==KEYUP:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
if event.key==K_LEFT or event.key==ord('a'):
moveLeft=False
if event.key==K_RIGHT or event.key==ord('d'):
moveRight=False
if event.key==K_UP or event.key==ord('w'):
moveUp=False
if event.key==K_DOWN or event.key==ord('d'):
moveDown=False
if event.key==ord('x'):
player.top=random.randint(0, WINDOWHEIGHT-player.height)
player.left=random.randint(0, WINDOWWIDTH-player.width)
if event.type==MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter+=1
if foodCounter>=NEWFOOD:
#Add new food
foodCounter=0
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH-FOODSIZE), random.randint(0, WINDOWHEIGHT-FOODSIZE), FOODSIZE, FOODSIZE))
#Draw the black background onto the surface
windowSurface.fill(BLACK)
#Move the player
if moveDown and player.bottom<WINDOWHEIGHT:
player.top+=MOVESPEED
if moveUp and player.top>0:
player.top-=MOVESPEED
if moveLeft and player.left>0:
player.left-=MOVESPEED
if moveRight and player.right<WINDOWWIDTH:
player.right+=MOVESPEED
#Draw the player onto the surface
pygame.draw.rect(windowSurface, WHITE, player)
#Check if the player has intersected ith any food squares
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
#Draw the food
for i in range(len(foods)):
pygame.draw.rect(windowSurface, GREEN, foods[i])
#Draw the window onto the screen
pygame.display.update()
mainClock.tick(40)
Friday, January 10, 2014
Animation (01.05.2014 - 01.10.2014)
This week I got animation down.
import pygame, sys, time
from pygame.locals import *
#Set up pygame
pygame.init
#set up window
WINDOWWIDTH=400
WINDOWHEIGHT=400
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
#set up direction variables
DOWNLEFT=1
DOWNRIGHT=3
UPLEFT=7
UPRIGHT=9
MOVESPEED=4
#set up colors
BLACK=(0, 0, 0)
RED=(255, 0, 0)
GREEN=(0, 255, 0)
BLUE=(0, 0, 255)
#Set up block data structure
b1={'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}
b2={'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}
b3={'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}
blocks=[b1, b2, b3]
#run the game loop
while True:
#check for the QUIT event
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
#draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
#move the block data structure
if b['dir']==DOWNLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==DOWNRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==UPLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top-=MOVESPEED
if b['dir']==UPRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top-=MOVESPEED
#Check if the block has moved out of the window
if b['rect'].top<0:
#block has moved past top
if b['dir']==UPLEFT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=DOWNRIGHT
if b['rect'].bottom>WINDOWHEIGHT:
#block has moved past bottom
if b['dir']==DOWNLEFT:
b['dir']=UPLEFT
if b['dir']==DOWNRIGHT:
b['dir']=UPRIGHT
if b['rect'].left<0:
#block has moved past left side
if b['dir']==DOWNLEFT:
b['dir']=DOWNRIGHT
if b['dir']==UPLEFT:
b['dir']=UPRIGHT
if b['rect'].right>WINDOWHEIGHT:
#block has moved past right side
if b['dir']==DOWNRIGHT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=UPLEFT
#Draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
#Draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
import pygame, sys, time
from pygame.locals import *
#Set up pygame
pygame.init
#set up window
WINDOWWIDTH=400
WINDOWHEIGHT=400
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
#set up direction variables
DOWNLEFT=1
DOWNRIGHT=3
UPLEFT=7
UPRIGHT=9
MOVESPEED=4
#set up colors
BLACK=(0, 0, 0)
RED=(255, 0, 0)
GREEN=(0, 255, 0)
BLUE=(0, 0, 255)
#Set up block data structure
b1={'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}
b2={'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}
b3={'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}
blocks=[b1, b2, b3]
#run the game loop
while True:
#check for the QUIT event
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
#draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
#move the block data structure
if b['dir']==DOWNLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==DOWNRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top+=MOVESPEED
if b['dir']==UPLEFT:
b['rect'].left-=MOVESPEED
b['rect'].top-=MOVESPEED
if b['dir']==UPRIGHT:
b['rect'].left+=MOVESPEED
b['rect'].top-=MOVESPEED
#Check if the block has moved out of the window
if b['rect'].top<0:
#block has moved past top
if b['dir']==UPLEFT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=DOWNRIGHT
if b['rect'].bottom>WINDOWHEIGHT:
#block has moved past bottom
if b['dir']==DOWNLEFT:
b['dir']=UPLEFT
if b['dir']==DOWNRIGHT:
b['dir']=UPRIGHT
if b['rect'].left<0:
#block has moved past left side
if b['dir']==DOWNLEFT:
b['dir']=DOWNRIGHT
if b['dir']==UPLEFT:
b['dir']=UPRIGHT
if b['rect'].right>WINDOWHEIGHT:
#block has moved past right side
if b['dir']==DOWNRIGHT:
b['dir']=DOWNLEFT
if b['dir']==UPRIGHT:
b['dir']=UPLEFT
#Draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
#Draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
Subscribe to:
Comments (Atom)


