www.NewsDownload.co.uk Page 18

Raspberry Pi Python PiMan Game

2013-03-01 20:43 By Jason Birch

Open Source Python Game for the Raspberry Pi. An example of how to write a game using classes in Python scripting language.

VIDEO
The video here demonstrates the project which this article describes.

The article breaks the project down into several stages:

  • Background
  • Configuring the OS
  • Source Code

Reference:

Background
This Python game has been created to provide coding beginners with a simple example and framework for creating games, using Python classes. It is best to read this article, install the game and then read through the Python script files. If at that point you require a simpler starting point, I recommend you view the video tutorials created by Andy Balaam, click here to see his channel.



Configuring the OS
I have tried this game on the Raspberry Pi OS distributions, Arch Linux and Raspbian.

When playing the game, press '1' to start a new game. Use the cursor keys to play the game. Press the 'Esc' key to exit the game.

Install the following by typing the at the command line as a root user.


For Raspbian:
# Update OS.
apt-get update
apt-get upgrade
reboot

# Get the game source code.
cd ~
wget https://github.com/BirchJD/PiMan/archive/master.zip
unzip master.zip

# Start the game.
cd ~/PiMan-master
modprobe snd-bcm2835
xinit /usr/bin/python PiMan.py


For Arch Linux:
# Update OS.
pacman -Syu
reboot

# Install Raspberry Pi Video drivers.
pacman -S xf86-video-vesa
pacman -S xf86-video-fbdev

# Install basic X Windows.
pacman -S xorg-server
pacman -S xorg-xinit

# Install PyGame library.
pacman -S python2-pygame

# Unzip required to uncompress library.
pacman -S unzip

# Get the game source code.
cd ~
wget https://github.com/BirchJD/PiMan/archive/master.zip
unzip master.zip

# Start the game.
cd ~/PiMan-master
modprobe snd-bcm2835
xinit /usr/bin/python2 PiMan.py


Source Code
The application for the project consists of the following files:

README.txt
   - Information about the package.
GNU-GeneralPublicLicense.txt
   - User license agreement.
PiMan.py
   - The main application script.
GameSprite.py
   - The super class for game sprites. Containing
     the common code shared by all game sprites.
PiManSprite.py
   - The sub class of GameSprite which is the 
     player's character.
WormSprite.py
   - The sub class of GameSprite which is the
     computer's worm characters.
Map.py
   - The play area, which can load a series of
     levels defined in text files.

The application then has three directories of support files:
Levels
   - Text files representing the levels of
     the game.
Images
   - Image files for the graphics of the game,
     associated with each character and map
     element.
Sounds
   - The sounds associated with each character
     of the game.



Download the source code here.

The Github repository can be found here.

OBSERVATION: Audio Delay
There is a delay between issuing an audio instruction and the sound being heard. This may be that the Raspberry Pi is under too much load or there is an issue with the PyGame library. I haven't looked into this issue.



UPDATE 2013-03-02

How To Add A Level
In the Levels directory copy an existing level txt file to a new file with the next level number. The rest of the file name must remain exactly identical apart from the level number.
e.g.
cp Level_2.txt Level_3.txt

Edit the file in a text editor. The letters in the file represent the types of map cell which occupy each part of the map. Do not change the number of rows or columns, just alter the letters to the new values you require. Note that the PiMan and Worms will always start from the same location on each level. The letters in the text file are as follows:
MAP_WALL = "A"
MAP_PATH = "B"
MAP_RASPBERRY = "C"
MAP_PIP = "D"
MAP_LAYER_LEFT = "E"
MAP_LAYER_MIDDLE = "F"
MAP_LAYER_EXIT = "G"
MAP_LAYER_RIGHT = "H"

Finally in order for the game to know there is a new level, alter the constant MAX_LEVEL near the top of the file Map.py to the number of levels which exist, in this example it would be changing from 2 to 3. If you later add another level it will go from 3 to 4, and so on.


Changing The Number Of Lives Per Game
In the file PiManSprite.py change the value of the constant LIVES to the required value.


Changing The Score Values
In the file PiManSprite.py change the value of the following constants to set the score value for each action:
SCORE_PIP
SCORE_RASPBERRY
SCORE_HYPER


Changing Time Period The Worms Can Be Eaten For
In the file PiManSprite.py change the value of the constant HYPER_COUNT to the required value.


Changing Time Period Before The Worms Start At The Beginning Of A Level
In the file WormSprite.py change the value of the constant DELAY_START to the required value.


Other Changes
In addition you can modify the sound and graphics in the Sounds and Images directories. Make sure the dimensions of the images are kept the same as their current dimensions.

Loading...