A Lispy Jigsaw Puzzle Game

December 27, 2006

My blog has been neglected lately, but I have a good reason. I’ve been playing around with lispbuilder-sdl, a Lisp interface to SDL, used for creating games. I decided to resurrect an old jigsaw puzzle game I wrote a few years ago using pygame. The game was never really finished, as with many of my other personal programming projects.

Converting the old Python code to Lisp was fun. I was able to simplify a lot and performance is much better, mostly due to Lisp being a compiled language. The python version was slow and I really had no way to optimize unless I wrote C extensions. Now with Lisp I can profile the code and add compiler optimizations and declarations to speed things up where they need to be. I still have the option to write C extensions but its unlikely I’ll need to for this game.

Creating each jigsaw puzzle piece was a little challenging and very processor intensive. I start by chopping up an image into square pieces. Each piece overlaps adjacent pieces by about 1/3. These pieces are then rotated 90, 180 and 270 degrees. Next I create a mask image where I draw the puzzle piece outline using bézier curves. I flood fill the outside of the mask image with the mask color, then blit it onto original piece image. Repeat for each rotation. OK, I think I left a few steps out, but my point is, it takes a while to create a puzzle from a source image. Using this process I can create a puzzle from any image.

Each puzzle piece is placed randomly on the screen. Right clicking a piece rotates it. Pieces that belong together can be joined by dragging one piece next to another. They snap together and then remain joined when moving them around. Joined pieces can also be rotated.

It is nowhere near done. I plan on adding many features, at least enough to be able to call it a beta. I would like to get a website with a downloadable binary and source code going soon. Hopefully I can maintain my interest in this and finish something for a change ;-)

Here are few screenshots of the game (sorry about the imageshack ads):