Home arrow Python arrow Page 2 - A PyGame Working Example, continued

Setting Things Up - Python

In this fourth article in our series covering the creation of a video game using Python and PyGame, we will code the internals of our game.

  1. A PyGame Working Example, continued
  2. Setting Things Up
  3. Cleaning Up
  4. Constructing the Main Loop
  5. The Game in Action
By: Peyton McCullough
Rating: starstarstarstarstar / 9
February 14, 2006

print this article



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
   screen = pygame.display.set_mode((background.get_rect().width,

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
   colWidth = background.get_rect().width / columns
   xPlayer = colWidth / 2
   rowHeight = background.get_rect().height / rows
   yPlayer = (rowHeight * (rows - 1)) + (rowHeight / 2)

   # Load the player sprite
   playerSprite = gamesprites.Player(player, xPlayer, yPlayer)

   # Create a player sprite group
   player = pygame.sprite.RenderUpdates(playerSprite)

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
   for y in xrange(len(layout)):
      for x in xrange(len(layout[y])):
         if layout[y][x]:
            layout[y][x] = gamesprites.Object(objects[layout[y]
[x] - 1], (colWidth * (x)) + (colWidth / 2))

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
   for y in xrange(len(layout)):
      group = pygame.sprite.RenderUpdates()
      for x in xrange(len(layout[y])):
         if layout[y][x]:
      group.y = 0
      layout[y] = group

Now, we've reduced our list to a simple list of sprite groups.

>>> More Python Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: