Sprites are simply images that exist in the context of your background. They can be players, pickups or whatever. It's possible to simply load regular images and use them instead. Either way, you end up with the same thing. However, sprites simplify this, especially in more complicated situations where you have various images on the screen, and provide an object-oriented approach to it all. Let's create a sprite by the name of StickMan based on this image:
We simply need to subclass pygame.sprite.Sprite, setting the image and the proper rectangle as an attribute. Then, we can blit it on to the screen: import pygame # Subclass pygame.sprite.Sprite def __init__(self, position): # Call pygame.sprite.Sprite.__init__ to do some internal # Load the stickMan # Create a rectangle # Position the rectangle pygame.init() # Create a StickMan sprite # Blit the sprite pygame.display.update() while True: The sprite is rendered, but what if we want to move the sprite when a user presses a key? This requires waiting for the user to press a key, moving the sprite and erasing its old location. Let's extend our application to do this: import pygame class StickMan(pygame.sprite.Sprite): def __init__(self, position): pygame.sprite.Sprite.__init__(self) # Save a copy of the screen's rectangle # Create a variable to store the previous position of the self.image = pygame.image.load('stickMan.gif').convert() def update(self, amount): # Make a copy of the current rectangle for use in erasing # Move the rectangle by the specified amount # Check to see if we are off the screen pygame.init() character = StickMan((screen.get_rect().x, screen.get_rect().y)) # Create a Surface the size of our character pygame.display.update() while True: # Check for movement # Erase the old position by putting our blank Surface on it # Draw the new position # Update ONLY the modified areas of the screen The first change in the StickMan class is the addition of screen. This is simply a copy of the screen's rectangle and is used to check whether the sprite has gone off the screen. We also add old, which is also a rectangle. When the sprite is updated, we store its old rectangle here so that our program knows what to erase. The update method simply moves the sprite and checks to see if it has gone partially off the screen. If it has, then it's bumped back on to the screen. Further down, we define blank, a Surface object that is blitted over the old position of the character sprite each time the sprite moves. We then check for keyboard input, moving the sprite ten pixels in the appropriate direction if the user has pressed one of the arrow keys. This is done by calling the update method described above. Next, we erase the old position by blitting blank over it, and we draw the new position. Finally, we make a call to update. However, note that we supply a list as an argument. The list contains character's old and new positions. By doing this, we tell PyGame only to update those areas of the screen. If we were to update the whole screen, then PyGame would have to redraw everything. This simply wastes resources since we have no need to update the entire screen when only two things have changed.
blog comments powered by Disqus |
|
|
|
|
|
|
|