Home arrow Python arrow Page 3 - A PyGame Working Example: Starting a Game

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

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

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.



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