All | 43 Folders | Accessibility | BoingBoing | Books | Computer Related | Family | Films | General | Hacking | Hobbies | Humor | Java | Links | Omni | Puzzles and Games

« Twenty Years At Sun | Main | I Want a Freeware... »
20070109 Tuesday January 09, 2007

My First Pygame Game

After discovering pygame back in November, I thought I'd write a small simple game to see how easy it was to use. I finished the first version of it over the Christmas break.

Just like for Zillions of Games, I decided to implement a variant of Sam Loyd's 15 puzzle. I used the Duncan and Dusti Christmas picture from 2 years ago, and turned it into a 3x3 sliding puzzle.

If you have Pygame (and its dependancies) installed on your computer, then you can downloaded a compressed tarball of all the files you need.

Once you've unpacked the gzipped tarball, change directories to the slidepuzzle directory, and type:

% ./slidepuzzle.py

Type "s" to randomly shuffle the tiles, then use the arrow keys or the mouse to try to solve the puzzle. "q" or Esc will quit the game.

If you just want to look at the Python source code, I made the slidepuzzle.py file available too.

Note that this version uses some nice sound files, shamelessly copied from the public domain Magicor game.

The pygame API is nice and clean. Admittedly I haven't explored all there is to offer yet, but it wasn't too hard to work out what I needed in order to get this simple game working, especially after reading the tutorials first.

The next changes will be to allow you to specify any image file on the command line and the size of the grid (3x3, 4x4 or 5x5). I'll also allow you to select which square is the one that is initially removed. If I get enthused, I might further adjust it so that you don't need to use any command line options, and all these things are settable via a normal Preferences type screen.

[]

[]

[]

( Jan 09 2007, 08:17:32 AM PST ) [Listen] Permalink Comments [2]

Comments:

Does it always generate a solvable puzzle? Many years ago, I made a javascript version of this game (long gone) but I do recall that there is a simple algorithm that was easy to find with Google.

Posted by Stoffe on January 09, 2007 at 10:48 AM PST #

Yes, it's always solvable because the shuffle algorithm starts from the "final" image and makes a series of random legal moves. Solving it (in the best possible number of moves) would just be a reversal of that.

The game currently doesn't have an auto-solver.

Posted by Rich Burridge on January 09, 2007 at 11:19 AM PST #

Post a Comment:

Comments are closed for this entry.