Forums

python

Category: beginnings

I know it’s been a while and for that I apologize the last few months have been pretty crazy around here…although I’m starting to see a trend with life in general lately, namely that it’s always crazy.

I’ve been busy with work, Python Magazine, my wife, trips to Dallas, and yes whenever I get a chance this slowly growing level editor. Let’s see what I’ve been working on for the last little while:

Name: There has a been a name for the editor ever since I started working on it. I wasn’t sure if I was going to think up something really cool and change, or leave it. Turns out I just left it.

So from this point forward this project is christened: “Dodger”, or probably more correctly: “Dodger Level Editor”.

The name has its roots in the name of one of my cats and a history in the multitudes of level editors and game engines that I have tried to create in the past, but I wont’ go into that. So Dodger it is.

Saving and Loading: Saving and loading in the default YAML project type now really works. I still need to put in support for optional project types: XML, JSON, and other formats

Welcome Dialog: This was a real pain, but it’s made the last while so much easier. I added a working (at least I hope) welcome dialog with a way to create new projects, open old ones, and a recent file list. The recent file list really makes testing easier for me.

Rect tracking: Rect tracking is finally working properly. I’ve had the rect tracker in there for a while but it didn’t really do anything until now.

Multiple Selection/Multiple Properties: I’ve also finally got multiple selection going, which is what makes the rect tracker actually useful. You can select multiple sprties, move them around and add properties to all of them.

Remove Properties: Now you can remove custom properties that you have added. This was a must but I was lazy and left it for a while.

Steps towards being made public: A lot of the changes (and I do mean a lot) that I’ve been making have been behind the scenes. There has been a lot of refactoring and reorganizing of the code, often the result of quick and dirty implementations that I made earlier (sigh). I’ve also started working on getting the distribution of this editor going so that other people can use/develop it.

I’ve added support for zc.Buildout so that if anyone wants to develop they can quickly gather dependencies and won’t have to install the editor system wide, not that you have to anyways but zc.Buildout is really neat.

I’ve also worked on the license (GPLv3) and setup.py and README and all of that. None of it’s done but it’s working its way forward.

Faster: It’s also much faster now. None of you have used it so you’ll probably think it’s slow, but trust me it’s much faster then it was before.

Bugs: There have been loads of bugs that have been fixed and created. Plus one doozy related to
changes made to pyglet. Not pyglets fault but it took a long time to figure out what the issue was.

Google Code: The project has a temporary homepage over at google code: http://code.google.com/p/dodger-editor/ There’s nothing there yet but over time I will start to host the project there so that people can easily download it. I’ll still post updates here until the site has a full-time home.

I’m going to use Mercurial for the revision control system for the project so the CVS support at the google code site will just be for downloading. Eventually I will want to host the project on some space of my own and get a nice web interface for the mercurial repository going. I’ll have to find a new hosting company so it will take a while.

So that’s it, that’s what’s happened to the Dodger Level Editor over the last few months. I know I promised to make it public earlier but given the shape it was in at that time there really was no point. I want this to be at a point where people can actually almost use it before I make it public.

I know it’s been a while, and I know the few of you that actually care about this project have probably moved onto bigger and better things, but hopefully if you stick with me there will be something out soon.

What this gives us is basically a plain old blank Tkinter window, not too exciting. The next thing we are going to do is add create our root menu, before going any further you might want to read the excellent information about menu widgets in PythonWare’s Tkinter introduction. You don’t have to but it’s an excellent source of information.

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

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

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:

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.

Prerequisites

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

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.

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.

Up until now we’ve focused on python applications that only run in the command line. For a lot of tasks this is great, but in order to be really impressive we’re going to want to create a GUI for some of our python applications.

There are a lot of python GUI toolkits out there, but for our first application we are going to use Tkinter, which is considered somewhat of a standard right now and is installed when pyhon is installed. Another nice thing about the Tkinter GUI is that it is cross platform and offers a native look and feel on *nix, Windows, and OS X.

Classes and Generators

All right, now that we have split our RSS reader up into functions, we’re going to go one step further and put our code into a class.

We’re also going to do something a bit more advanced with our class and create a generator. The reason that we are going to do this is because python has such nice iteration handling and because in the future we’ll probably want to handle each RSS item individually rather then simply dumping it out to the terminal.

Generators are basically a type of iterator, except their syntax is slightly different. In fact anything you can accomplish in a generator you can accomplish in a standard iterator.

Here is an example generator that yields all the factors of the specified number:

This post is my continuation of my Python based RSS reader that I wrote in part one. As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn’t broken up in to functions properly. So, in this part we’re going to work on breaking the old script up into functions.

Functions

Functions are defined in python using the def keyword. So if I wanted to define a function called “count” that counts from 1 to a certain number I would do so like so:

def count(nNum):

Where count is the name of the function and nNum is a parameter that is being passed into the function. If I wanted to call the function I would do so like this:

All right so I’ve already figured out how to write an executable script that writes out “Hello World!” to the command line, now I need to figure out how to do something interesting.

As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing seemed interesting enough or easy enough for me to do until I visited one of my favorite websites: slashdot.org.

It was there that I noticed that RSS icon that Firefox always shows me whenever I come across a website with an RSS feed (like this one.)

Well after reading the python documentation for a few days I decided it was time for me to write my first real script. Now I’ve written a few scripts before in the python interpreter, but I wanted to try my hand at creating some stand-alone executable file.

Now I didn’t want to go too crazy with my first script so I thought I would start off with the simplest program that everyone knows, the Hello World program. For this example I’m going to use the gedit since I already have it installed and it comes with built-in python syntax highlighting, but any standard or fancy text editor could be used in its place.Continue reading Writing my first python script