Before our game is allowed to work, we must initialize PyGame and create the necessary Sprite objects. The first function we'll define is the setup function, which simply initializes PyGame and creates a screen for us to work with: def setup(): global screen Now that we have a Surface object to draw on, we must blit the background image onto the Surface object. As you know already, this isn't rocket science: def loadBackground(): screen.blit(background, background.get_rect()) Finally, we have to create our game's Sprite objects. The first Sprite object we'll have to create is the player's sprite. We'll position the sprite in the bottom left of the player's screen to begin with, which will require a few calculations based on the data we retrieved when loading the level. The player Sprite should also be added to its own group: def loadSprites(): global player, playerSprite # Find the position of the player # Load the player sprite # Create a player sprite group Next come the object sprites. Each non-zero position in the layout list will need to be converted into a sprite, and we'll need to calculate the exact x-position of each object: def loadSprites(): ... # Load each object sprite In the above code section, we simply iterate through the list and replace every non-zero element with a corresponding Sprite object. We'll also need to convert each row in the layout list to a group. This way, we can move an entire row of objects at once: def loadSprites(): ... # Turn each layout row into a sprite group Now, we've reduced our list to a simple list of sprite groups.
blog comments powered by Disqus |
|
|
|
|
|
|
|