Home arrow Python arrow A PyGame Working Example: Starting a Game

A PyGame Working Example: Starting a Game

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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The Game Plan

Before we jump into Python code, we need to develop the idea behind our game. Nothing complex is needed for our game, since its purpose is just to reinforce the basics of PyGame and demonstrate how to put them to use. With that said, let's move into the idea. The idea is to have the player at the bottom of the screen with the ability to move horizontally. Horizontal movement will be required to dodge incoming objects that will constantly be hurled at the player at fast speeds. Picture the player as a ship having to navigate at high speeds (which will be essentially an illusion, since the player will not be moving vertically; the incoming objects will) and dodge mines or enemy ships.

However, to give it an added degree of complexity and to make it more dynamic, levels will be stored as Python objects. Each level object will be responsible for loading the required images and laying them out in a layout list. The layout list will contain many more lists as elements that correspond to rows of objects. So, for example, picture a layout list as looking something like this:

[[0, 0, 0, 0],

 [1, 0, 1, 0],

 [1, 0, 1, 0],

 [1, 0, 1, 0],

 [0, 0, 0, 0]]

A zero would be mapped to a blank space, and a one would be mapped to an object of some sort. The result would look something like this, to draw a very rough sketch with gridlines for visibility:


The green block represents the player, and the red blocks represent the objects that the player is required to dodge. We would simply have to move the objects down and see if the player has collided with any of the objects.

Of course, since the levels would be generated by Python classes that determine what the images are and how they are positioned, levels would not be limited to the mine-dodging scenario I presented earlier. You could have a fish dodging sharks or a spaceship dodging asteroids if you felt inclined to do so.

Also, it's possible to create random object layouts or images using this method. This way, levels could be different each time they are played.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

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