HomePython Page 2 - A PyGame Working Example: Starting a Game
Preparing a Level - Python
In PyGame for Game Development, I showed you the very basics of PyGame's graphical side. However, creating a game with PyGame requires a bit more. All the concepts described before need to be glued together somehow, and new concepts will need to be introduced in order to create a functional game. In this article, we'll do just that by tackling a working example of PyGame's capabilities—a Python-powered game.
Let's start by making a level and building the game around it. The idea is to create the level out of an object that loads the required images and returns the layout list described in the last section. Before we do that, though, let's create a class from which all level classes can be derived. That way, we can organize things a bit better:
With the actual levels that come from the Level class, our game will be concerned with four methods: getPlayer, getObjects, getLayout and getBackround. The first method will be responsible for returning the player's image. The second method will be responsible for returning a list of the images of the objects involved. The third method will be responsible for returning the layout list. The final method returns the background image, but it will also need to return the number of rows visible on the screen at a time. This way, we can divide up the background and figure out how long each space will be. The width of each space will be determined by measuring how big the first element (which will be a list itself, of course) is inside of the layout list.
Each element of the layout list will be a number. Zero will represent a blank space, and one and above will represent objects. These objects will be based on images obtained from getObjects, but since the list index will start at zero, we'll have to subtract from each of the elements in getLayout to obtain the proper image. So, to express this in more basic terms, a value of 1 in getLayout would correspond to the first object in getObjects (0), and a value of 2 in getLayout would correspond to the second object in getObjects (1).