In order to follow along with this tutorial you will need Python 2.4 and PyGame 1.7.0+ installed. Since I’m doing this all on a Mac and Python 2.4.2 is not available from MacPython or any other site as a disk image I decided to install from source.
If you are try to build Python on a Mac as well you should probably install into the /Library/Frameworks path as explained by this site.
Basically once you have downloaded the source from python.org unpack the tar ball as follows:
tar -zxvf Python-2.4.2.tgz
Once that has finished change to the Python-2.4.2 directory that was just created and install it by issuing the following commands:
sudo make frameworkinstall
For Windows or Linux you should be able to install easily given the instructions on the Python site.
This will install Python in the /Library/Frameworks path, and create a symlink to the python executable in /use/local/bin, which is not part of the PATH environment variable on new OS X builds, so you will probably want to add it:
The full source of this tutorial can be downloaded here.
So let’s actually start creating this game using PyGame. For our snake image in the game I’m going to use the snake to the left for now. As you can see I’m not artist, but I was able to install the gimp on my Mac and get that image to a point that I think is relatively acceptable. If anyone out there has any graphics skills and can whip me up a better looking python in a 64×64 png I’d gladly use it!
So the first thing we are going to do is create a new PyDev project in Eclipse. I’m going to use Python 2.4 for this project because it is the version that is compatible with my PyGame Installation.
Then I’m going to create a new file called PyMan.py, this will be the main file of our game for now. The architecture may change as I go through this and discover better ways to use the files in my projects but for now this will work as a main file. In the directory that I create my project I will also create a subfolder entitled “data” and in that subfolder I will have another folder entitled “images” where I will store the above snake.png image.
Note: A lot of the information in this post was taken from the Pete Shinner’s great Line by Line Chimp and Python Pygame Introduction tutorials.
Now the first thing that we are going to have to do is import the libraries that we are going to need, we’ll also warn the user if the font or sound mixers are not available:
import os, sys
from pygame.locals import *
if not pygame.font: print ‘Warning, fonts disabled’
if not pygame.mixer: print ‘Warning, sound disabled’
Then we’re going to create a class called PyManMain, this is going to be the main class of our game, it will handle all of the main functions in our game, things like the game loop, the screen creation, and keeping track of all of our sprites:
“””The Main PyMan Class – This class handles the main
initialization and creating of the Game.”””
def __init__(self, width=640,height=480):
“””Set the window Size”””
self.width = width
self.height = height
“””Create the Screen”””
self.screen = pygame.display.set_mode((self.width
As you can see our __init__ function takes two optional parameters height and width, this will be the height and the width of the screen that we create. The __init__ function basically initializes pygame (pygame.init()) and then creates our main screen using the pygame.display.set_mode function.
So far so good! The next thing that we are going to need is a game loop, this is the loop that will process all of the events that PyGame sends our way. To do that, weÃ¢Â€Â™re going to add a function to our PyManMain class called MainLoop:
“””This is the Main Loop of the Game”””
for event in pygame.event.get():
if event.type == pygame.QUIT:
WeÃ¢Â€Â™ll also add the code to our class that will start out game:
if __name__ == “__main__”:
MainWindow = PyManMain()
If you run this now you will be with a very uninspiring black screen. It may not be much but for the amount of code that weÃ¢Â€Â™ve written itÃ¢Â€Â™s pretty good.
The next thing that we need to create is out snake sprite. We are going to wrap our snake sprite in itÃ¢Â€Â™s own class called snake (of course) and it will be based off of the pygame.sprite.Sprite class. If you want to read more about PyGames sprite and group classes you should read pimanÃ¢Â€Â™s great sprite tutorial:
“””This is our snake that will move around the screen”””
self.image, self.rect = load_image(‘snake.png’,-1)
self.pellets = 0
As you can see the Snake class really isnÃ¢Â€Â™t doing much besides initializing the Sprite base class and loading the snake image. It also sets pellets to zero, this variable isnÃ¢Â€Â™t used yet but it is the number of pellets that our snake has eaten.
YouÃ¢Â€Â™ll notice that we use load_image to load our snake image. This function is taken from the Line by Line Chimp tutorial. I created a new file for my project called helpers.py which will contain all helper functions. I put load_image in this file and added the following to the top of PyMan.py:
from helpers import *
Now we have to create an instance of our snake sprite and display it. To do this I created another function in the PyManMain class called LoadSprites, this function will take care of loading all of our sprites:
“””Load the sprites that we need”””
self.snake = Snake()
self.snake_sprites = pygame.sprite.RenderPlain((self.snake))
This function creates the sprite (self.snake = Snake) and then creates a group that contains our snake sprite (self.snake_sprites = pygame.sprite.RenderPlain((self.snake))).
We are also going to have to make some changes to the MainLoop function, we are going to have to load all of our sprites before we enter the while loop:
“””Load All of our Sprites”””
Then after our event for loop but within the while loop we need to tell PyGame to draw our sprit:
The next thing that weÃ¢Â€Â™re going to want to do is create and display all of our pellets, this isnÃ¢Â€Â™t very difficult if you understood the snake sprite above youÃ¢Â€Â™ll understand this:
def __init__(self, rect=None):
self.image, self.rect = load_image(‘pellet.png’,-1)
if rect != None:
self.rect = rect
The only difference youÃ¢Â€Â™ll see in the pellet class is the fact that we have an optional rect parameter, which lets us place the pellet where ever we want. The pellets IÃ¢Â€Â™ve created arenÃ¢Â€Â™t the best pellets that IÃ¢Â€Â™ve ever seen but just like the snake.png theyÃ¢Â€Â™ll do for this example.
We then need to load the pellet sprites in our LoadSprites function:
“””figure out how many pellets we can display”””
nNumHorizontal = int(self.width/64)
nNumVertical = int(self.height/64)
“””Create the Pellet group”””
self.pellet_sprites = pygame.sprite.Group()
“””Create all of the pellets and add them to the
for x in range(nNumHorizontal):
for y in range(nNumVertical):
self.pellet_sprites.add(Pellet(pygame.Rect(x*64, y*64, 64, 64)))
Then we have to draw the pellets in out MainLoop:
Now itÃ¢Â€Â™s time to make that snake move, to do so weÃ¢Â€Â™re going to have to look at the event loop in our MainLoop. ( for event in pygame.event.get():) What we are going to do is move the snake around when the arrow keys are pressed. To do that we are going to check to see if the event is a KEYDOWN event, and if it is a key down event whether the key being pressed is one of the arrow keys:
if event.type == pygame.QUIT:
elif event.type == KEYDOWN:
if ((event.key == K_RIGHT)
or (event.key == K_LEFT)
or (event.key == K_UP)
or (event.key == K_DOWN)):
Then we have to add a move function to our snake class that will actually move our snake:
def move(self, key):
“””Move your self in one of the 4 directions according to key”””
“””Key is the pyGame define for either up,down,left, or right key
we will adjust ourselves in that direction”””
xMove = 0;
yMove = 0;
if (key == K_RIGHT):
xMove = self.x_dist
elif (key == K_LEFT):
xMove = -self.x_dist
elif (key == K_UP):
yMove = -self.y_dist
elif (key == K_DOWN):
yMove = self.y_dist
YouÃ¢Â€Â™ll notice that the move function references self.x_dist and self.y_dist, these are basically two integers that I set in the snakeÃ¢Â€Â™s __init__ function, they are the number of pixels that we want the snake to move in the x or y direction. I set mine to 5.
To move the snake we adjust itÃ¢Â€Â™s rect so that the next time it is displayed it will be drawn in a different direction. To do that we call the rect.move_ip function with the amount of pixels in the x and y directions to move.
Now we are going to have to add some collision detection to make our snake eat our pellets. Thankfully the spirt and group classes have built in collision detection that we are able to use:
“””Check for collision”””
lstCols = pygame.sprite.spritecollide(self.snake
“””Update the amount of pellets eaten”””
self.snake.pellets = self.snake.pellets + len(lstCols)
We use the pygame.sprite.spritecollide function to see if our snake sprite collided with any of the pellet sprites. If they did we kill which ever pellet that was hit by passing True as the third parameter. pygame.sprite.spritecollide basically goes through all of the sprites in the group passes and sees if the sprites rect intersects with the sprit passed in as parameter one.
pygame.sprite.spritecollide also returns a list of all the sprites that were collided by our snake, so we use that to update the number of pellets that we have eaten.
The last thing that we our going to do in this tutorial is display the number of pellets that we have eaten to the user:
font = pygame.font.Font(None, 36)
text = font.render(“Pellets %s” % self.snake.pellets
, 1, (255, 0, 0))
textpos = text.get_rect(centerx=self.width/2)
This code is taken from the line by line chimp example with very few changes. Instead of telling the user to hit the chimp for cash, weÃ¢Â€Â™re telling them how many pellets that the snake has eaten so far. We need to put the code right right before we draw our sprites in the game loop.
ThatÃ¢Â€Â™s about it for this example, there is a bit of code in the finished Ã¢Â€ÂœproductÃ¢Â€Â that I didnÃ¢Â€Â™t discuss but if you download the full source it the new code should be pretty self explanatory. Now this isnÃ¢Â€Â™t a full example by any means and probably isnÃ¢Â€Â™t something that youÃ¢Â€Â™d really want to base any game off of, but it does give you a pretty basic idea about what is required to create a game in using Python and PyGame.
If you are really interested in creating a game IÃ¢Â€Â™d suggest you download the full source and play around with it a bit. IÃ¢Â€Â™m also suggest (insist?) that you read the documentation and tutorials provided for you on the main PyGame site, if it wasnÃ¢Â€Â™t for them I would have been unable to write this simple tutorial.
Whew, that was a lot a typing, itÃ¢Â€Â™s time for me to go make some dinner and have a glass of wine.
If you like this post remember to digg it.
Note: Whew! Many thanks to James for finding the google cahce for me! I looked for it when I first deleted it but couldn’t fine it. Much appreciated!
Here are some alternate images that were sent to me by a kind reader named Jordan: