fps = 30 width = 640 height = 480 import random class Alien: """ Effectivly a very simple sprite class, with it's properties defined in it's init code. """ def __init__(self): self.alienImage = pygame.image.load('eyealien.png') self.alienImage.convert_alpha() self.pos = [50,50] self.direction = [1,1] self.velocity = [2.5, 2.5] def draw(self, display): display.blit(self.alienImage, self.pos) def update(self): """ Update is called every frame. Adjust the alien't position based on it's velocity. """ if self.pos[0] > ( width - self.alienImage.get_width() ) \ or self.pos[0] < 0: self.direction[0] *= -1 if self.pos[1] > ( height - self.alienImage.get_width() ) \ or self.pos[1] < 0: self.direction[1] *= -1 self.pos[0] += self.velocity[0] * self.direction[0] self.pos[1] += self.velocity[1] * self.direction[1] # Ugh, has to be global because it is initialized in a function # and used in a different scope. global alien def initState(): """ Called by the controller, used to initialize variables and objects that maintain state. """ global alien alien = Alien() # check to see if the pygame module is in the namespace try: pygame except NameError: # pygame doesn't exsist, so import it. # this only happens once. import pygame from pygame.locals import * # initialize the pygame display pygame.init() pygame.display.set_mode((width,height),0,32) display = pygame.display.get_surface() # initialize the pygame clock clk = pygame.time.Clock() initState() def play(): """ Main game logic, called once per frame.""" global alien # fill the display with black. Clears the screen. pygame.draw.rect(display, (0,0,0,255),(0,0,width,height)) # draw a shape. pygame.draw.ellipse(display, (180,40,200,255),(200,160,100,50)) # move the alien alien.update() # draw the alien alien.draw(display) # update the display. pygame.display.flip() # check for events, and return true if we get an exit signal. events = pygame.event.get() for event in events: if event.type == KEYDOWN and event.key == K_r: # reinitialize the program state. initState() if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): return True # try to maintain a set frame rate. clk.tick(fps) # No exit signal was recived, so return false. return False