Home arrow Python arrow 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.

  1. A PyGame Working Example: Starting a Game
  2. Preparing a Level
  3. Creating a Level
  4. Sprite Definitions
By: Peyton McCullough
Rating: starstarstarstarstar / 22
February 07, 2006

print this article



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:

class Level:

   def getPlayer(self):
   def getObjects(self):
   def getLayout(self):
   def getBackground(self):

Save the script as levelbase.py.

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).

>>> 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: