""" basicSprite.py works just like moveBox.py from chapter 4, but now uses a sprite a couple of changes for better oo by jsantore """ import pygame class Box(pygame.sprite.Sprite): """ this is a simple sprite that will move itself accross the screen. When it gets to the right edge it will move itself back to the left edge of the screen""" def __init__(self, disp): self.screen = disp pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((25, 25)) self.image = self.image.convert() self.image.fill((255, 0, 0)) self.rect = self.image.get_rect() self.rect.centerx = 0 self.rect.centery = 200 self.dx = 10 self.dy = 0 def update(self): self.rect.centerx += self.dx if self.rect.right > self.screen.get_width(): self.rect.left = 0 def main(): '''sample doc comment this main function creates a window with a mono colored background it puts a Box sprite on it and then starts the main event loop''' pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("basic sprite demo") background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((255, 255, 0)) screen.blit(background, (0,0)) box = Box(screen) allSprites = pygame.sprite.Group(box) clock = pygame.time.Clock() keepGoing = True while keepGoing: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() pygame.quit() #python 2.6+ seems to need this to get the window to close if __name__ == "__main__": main()