TextWidget 0.1

It’s been a long time since I worked on TextWidget at all, but since someone posted a question about it I decided to fix the issue and re-release the source. But since I didn’t want to simply update the blog post I decided to give the project a proper home on google code: http://code.google.com/p/textwidget/

The project is really simple and meant as an easy way for you to create “text buttons” for your PyGame projects. It’s not meant to be the definitive way to do this, just a simple solution for people that just want to drop a class in and have working “text buttons”. It’s LGPL so you can use it in whatever way you want. If you do decide to use it please drop me an email and let me know.

For more information on how to use the project please take a look at the initial blog post.

New Comment Moderation

Hey everyone, I’ve been getting hit with a bunch of spam comments over the last few days so I’ve made it so that all comments must be moderated before they will appear.

Sorry about this, I don’t like this form of comment posting, but hopefully after a few days I will be able to switch it back to normal.

And sorry about the lack of posts here, yes I am still alive, it’s just that real life has been working me pretty hard these last few weeks.

I’ve been playing around with my PyGTK app and a new PyGame engine that I am working on from the ground up. Something that I can understand and that I can use. I’ll post more information about it when there is more information…right now it does next to nothing!

Spanish Translations

So I’ve been working on PyGame for PyWeek lately and I’ve been having a lot of fun. I also took a weekend off from my email so I did not notice that Lord Taran has been creating Spanish translations for a bunch of my PyGTK tutorials.

If you are interested in the Spanish tutorials check them out, or if you are interested in seeing what happened at pyweek and the car crash that was my entry check it out as well.

TextWidget – A simple text class for PyGame


All right, this is just a little tutorial about working with text in pygame. Now, this isn’t the only way to work with text, there are many other methods to do this, in fact much of the time you’ll probably end up using images for interactive text. So this is mainly meant to serve as a informative guide to using the text features in PyGame, and how you might want to implement them.

The TextWidget object discussed in this tutorial can be used to make something that looks like this (but you’d probably want to use better looking colours):

The full source and necessary files for this tutorial can be downloaded here.

So, in order to make this easy to use and very reusable we’re going to create a class called TextWidget in a file called TextWidget.py. The top of the file is full of the standard python initialization:

[code lang=”python”]
#! /usr/bin/env python

import pygame


We import pygame and then we set a define TEXT_WIDGET_CLICK , which will be used later on as the event type when the TextWidget is clicked on.

The next thing to do is define the actual class:

[code lang=”python”]
class TextWidget(object):

Now the astute among you will recognize that this is a “new-style” python class (i.e. it’s base class is the object class) rather then a classic class. I did this for two reasons:

  1. I wanted to be able to control what happens when people set values in the class. So, for example, if you set the size of the font to be something, I wanted the display to automatically adjust to reflect the new size.
  2. I haven’t really used new-style classes before so I thought I’d try them out.

Note: If you are unfamiliar with properties or new-style classes you might want to give this a read:

Continue reading TextWidget – A simple text class for PyGame


Hi everyone just a quick post to let you know that I probably won’t be posting much during this month as I have decided to participate in nanowrimo which leaves me almost no time to come up with tutorials.

I as mentioned in the past Satoshi Tanabe over at satolog has translated a few of my tutorials over into Japanese, which is very cool. It also turns out that someone over at blog.chinaunix.net has also translated one of my tutorials (Building an application with PyGTK and Glade) into Chinese, again very cool! Thanks a lot for the translations guys.

I’ve also been working on some pyGame in my spare time over the last few months, I’ve been working on a level editor and a widget set:

PyGame Window

Of course that doesn’t show very much but it show show my scrollbars! The problem with building the level editor is that y you always have to keep working on the smallest details (like a scrollable area0 before you can get to the real heart of the problem. Oh well it’s till fun!

Talk to you all soon!


Recently I’ve been playing around in PyGame for the past PyWeek and having a blast! Sadly my PyWeek entry was mostly a bust as real life got the better of me, but it reminded my how much I enjoy python and doing simple game programming.

Hopefully I’ll continue on with both my PyGTK and PyGame work! If you like Python and are interested in creating games, I definately think that you should check out PyGame.

Creating a Game in Python Using PyGame – Part 3 – Adding the bad guys

All right in this section of the tutorial we are going to start adding the bad guys. If you are familiar with the changes that we made in part two it should be pretty clear to you how are are going to create these bad guys. If you haven’t already you should check out part one and part two. If you would like the full source and all of the images you can get it here.

First we are going have to create a new class based on our basicSprite class. I created a new file called basicMonster.py and inside of that I created a class called Monster:

[code lang=”python”]
class Monster(basicSprite.Sprite):

For now we’re not going to go too crazy in our enemy AI, we’re just going to get them to choose a random direction to move and move in that direction until they hit a wall. We’ll also add in another option where after a certain amount of moves the monster will choose another random direction. This is just to add a little but more randomness into the Monster, it’s not really what we’d like to use at the end of the day but it works out all right.

Now we’ll take a look at the Monsters __init__ function:

[code lang=”python”]
def __init__(self, centerPoint, image, scared_image=None):

basicSprite.Sprite.__init__(self, centerPoint, image)
“””Save the original rect”””
self.original_rect = pygame.Rect(self.rect)
self.normal_image = image
if scared_image !=None:
self.scared_image = scared_image
self.scared_image = image

self.scared = False
“””Initialize the direction”””
self.direction = random.randint(1,4)
self.dist = 3
self.moves = random.randint(100,200)
self.moveCount = 0;

Continue reading Creating a Game in Python Using PyGame – Part 3 – Adding the bad guys

Creating a Game in Python Using PyGame – Part Two – Creating a level

Python RSS ReaderAll right in Part one we actually created a semi-working almost-game, in part two we’re going to go a bit further, in part two we’re going to add the walls that will make help make PyMan (our python based PacMan clone) an actual game.

Note: Part one has been mistakenly half-deleted by me, so it is not fully available at this time. I am working to re-write it so it should be up soon.

Python RSS ReaderNote: I also changed the images that I used in Part one so now we have a new smaller snake.

You can download the full source for Part Two here.

What we are going to do is base some of our level code on a great PyGame tutorial over at DevShed specifically the idea of the level layout. This is something that has always puzzled me about games, how they create their levels? I’ve read some articles and beginnings of books but I’ve never really be able to understand it, but the multidimensional array approach discussed in the DevShed article is nothing if not simple.

So the idea is that our game world, or game board, will basically be a grid of 24×24 pixel items, and we will have a multidimensional array or list that we will used to describe it.

So for example lets say we have a grid like this:

0 0 0 0 0
1 1 1 1 1
1 0 0 0 1
1 1 1 1 1
0 0 0 0 0

Now that grid may result in the following:

PyGame Window

Where 0 is nothing and where 1 is a blue square. We could then add 2’s if we wanted to and make those red squares, or anything else that we wanted. So that is basically how our levels are going to be defined.

Continue reading Creating a Game in Python Using PyGame – Part Two – Creating a level

Creating a Game in Python Using PyGame – Part One


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:

./configure --enable-framework
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:

export PATH=/usr/local/bin:$PATH

Part One

The full source of this tutorial can be downloaded here.

Python SnakeSo 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.

Python foldersThen 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.
Continue reading Creating a Game in Python Using PyGame – Part One

Creating a Game in Python Using PyGame – Introduction

Well as someone who programs a lot and likes using computers creating a viedo game is always something that I’ve wanted to do. In the past I spent a lot of time learning OpenGL and actually went quite far in the creation of a simple/not-so-simple game. The game was put on hold indefinitely once real life responsibilities started to encroach on my evenings. Since then I’ve longed to try again.

Every since I heard about PyGame from import this, I thought it looked promising. It’s cross platform, which is something that is very important to me as a developer, and it lets me stratch my Python skills which is the whole reason I have this website. Here is some of the about information from the PyGame website:

Pygame is a set of Python modules designed for writing games. It is written on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Pygame is highly portable and runs on nearly every platform and operating system.

Continue reading Creating a Game in Python Using PyGame – Introduction