A PyGame Working Example: Starting a Game - Creating a Level
(Page 3 of 4 )
Now, we're ready to create a level for our game. Name this asteroid.py:
import levelbase
import pygame
class Level(levelbase.Level):
def getPlayer(self):
return pygame.image.load('ship.gif')
def getObjects(self):
return [pygame.image.load('asteroid.gif')]
def getLayout(self):
return [[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],\
[0, 1, 1, 0, 0, 0, 0, 1, 1, 0],\
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],\
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\
[1, 1, 1, 0, 0, 0, 1, 1, 0, 0],\
[0, 0, 0, 0, 1, 0, 0, 0, 0, 1],\
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],\
[1, 1, 0, 0, 0, 0, 1, 0, 0, 0],\
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0]]
def getBackground(self):
return pygame.image.load('background.gif'), 5
As you can see, we simply return the required images, along with the layout list and the number of rows that will be visible at once. This particular level goes with one of the ideas I described above: a spaceship dodging asteroids. Here are the images that the level references:
ship.gif

asteroid.gif

background.gif

Notice how the background is not solid, but, rather, it's an image. Because of this, we can't simply erase a sprite by drawing a solid-colored Surface object over it. Instead, we have to get the original background image that the sprite replaced and then draw it over the sprite. Thankfully, this is very simple, and we'll look at how it's done later on.
Next: Sprite Definitions >>
More Python Articles
More By Peyton McCullough