#! /usr/bin/env python """ PyGame Sprite Tutorial By PyMike (pymike93@gmail.com) March 1, 2008 This tutorial will show you how to use the pygame sprites and groups. In my opinion, these modules are some of the most useful features in pygame. """ """Import pygame!""" import pygame from pygame.locals import * """ Things to remember when using the pygame sprite: -- A sprite must always have a .image value and a .rect value. .image should be a pygame surface, and .rect should be a pygame rect. -- The pygame sprite is especially useful with pygame groups. You should always use groups when using the pygame sprite, otherwise the sprite is more or less useless. """ class Player(pygame.sprite.Sprite): def __init__(self): """This initialises the sprite group. self.containers is a number of groups to add this sprite to. Calls: pygame.sprite.Sprite.__init__(self, *groups)""" pygame.sprite.Sprite.__init__(self, self.containers) """self.image = pygame.Surface((32, 32)) creates a surface/image, 32x32 pixels in size. The first integer is the width, the second the height. self.image.fill fills the image with a color. The first integer is the red value, the second the green value, the third the blue value. 0 is the minimum the color value can be, and 255 the max.""" self.image = pygame.Surface((32, 32)) self.image.fill((0, 255, 0)) """This creates the rect for the sprite. Rects are exteremely useful in pygame, and are most commonly used for postitioning, collisions, and so forth. The arguments (topleft = (0, 0)) are for positioning the rect. The first arg is what part of the rect should center on the second. So topleft = (0, 0) would position the topleft part of the rect at 0,0 pixels. center = (320, 240) would position the center of the rect at 320,240 pixels.""" self.rect = self.image.get_rect(topleft = (0, 0)) """This function is called once every frame in the main loop.""" def update(self): """Check for keys getting pressed.""" keystate = pygame.key.get_pressed() """The stuff below lets you move around with the arrow keys. What it's doing is moving the sprite while a certain key is being pressed. self.rect.move_ip(x_speed, y_speed) is what is moving the sprite around. The first argument is how many pixels the sprite moves horizontally each frame. The second is how many it moves vertically.""" if keystate[K_LEFT]: self.rect.move_ip(-5, 0) if keystate[K_RIGHT]: self.rect.move_ip(5, 0) if keystate[K_UP]: self.rect.move_ip(0, -5) if keystate[K_DOWN]: self.rect.move_ip(0, 5) """Create a rect of the screen, then clamp the player's rect to it. Rect(0, 0, 640, 480) <-- first arg = x pos, second = y pos, third = width, fourth = height.""" SCREENRECT = Rect(0, 0, 640, 480) self.rect.clamp_ip(SCREENRECT) """Main game script that sets everything up.""" def main(): """Initialise pygame.""" pygame.init() """Set up the display mode.""" screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("PyGame Demo by PyMike, March 2008") """RenderUpdates is a pygame group, with a fancy draw function. It speeds up drawing a lot.""" all = pygame.sprite.RenderUpdates() """Remember when we called Sprite.__init__(self, self.containers)? We must define containers now.""" Player.containers = all """Clock() is another great module with pygame. With it you can cap framerate, get the frames per second, and some other cool stuff.""" clock = pygame.time.Clock() """Create the player sprite!""" player = Player() """Main loop. This keeps the game running.""" while 1: """Cap the framerate at 60 frames per second""" clock.tick(60) """This calls the update function of the RenderUpdates group, which calls the update function of all the sprites.""" all.update() """Quit the game if the X button on the window bar is pressed or if the Escape key is pressed.""" for e in pygame.event.get(): if e.type == QUIT: return if e.type == KEYDOWN: if e.key == K_ESCAPE: return """Fill the screen with a color.""" screen.fill((0, 0, 0)) """Call the draw function of the RenderUpdates group, which draws all the sprites to the screen.""" all.draw(screen) """Make stuff appear!""" pygame.display.flip() """Run the game script if this script is executed.""" if __name__ == "__main__": main()