A Lispy Jigsaw Puzzle Game

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):

11 Responses to “A Lispy Jigsaw Puzzle Game”

  1. びっくり Says:

    So, a couple questions: first, what does ‘blit’ mean? Is that a typo or just another technical term that is way over my head?

    Second, can you handle images with strange aspect ratios? In this case do you strive to keep the pieces generally close to square, or do you end up with very skinny rectangles in some cases?

    A general comment about jigsaw games: I often find the working space is very limited. Usually I can’t arrange the pieces in a way that I can see all of them at a time. Also, not-yet-used pieces end up getting lost under the connected pieces. If your software avoids these issues, then: It Rocks! When we did family jigsaw puzzles (the real kind) we often used baking sheets to keep the pieces that wouldn’t fit on the table. Maybe you could have a second (baking sheet) window for the unconnected pieces.

    Since you are hoping to finish a project I won’t try to pull you away for my personal project. :) I think I can guess where some of your pics came from… hint = fish ball soup.

    Keep up the good work.

  2. anthonyf Says:

    びっくり,

    Blit or blitting is a memory copying operation that copies memory from one image to another or from an image to the screen. Here’s what wikipedia says about it: http://en.wikipedia.org/wiki/Bit_blit

    My post really does not give many details about other features. To solve the limited workspace problem, I’m planning on implementing multiple desktops where the user can switch and drag pieces between them. This is useful for doing things like separating all edge pieces out to assemble them without having the clutter of non-edge pieces lying around. I will probably do 2 desktops and show a little mini-map of the other desktop in the corner. Should be cool.

    I have already implemented a nice feature where you hit the TAB key and the unsolved pieces get arranged in a grid where they can all be seen unobstructed by other pieces.

    Other features coming soon include: missing pieces, extra pieces (from other puzzles maybe?) and no edge pieces. Varying levels of insanity :-)

    Yes, and you guessed it, the keyboard pic was donated by Mark “Midiwall”. Maybe I should have given him credit for it!

  3. anthonyf Says:

    びっくり,

    I forgot to answer the aspect ratio question. I can create a puzzle from an image with any aspect ratio. This can be a bit tricky but I came up with a system that works well. First, the total number of pieces should be a number with a high number of factors. 12 pieces is a good number for an easy puzzle. With 12 pieces, I can handle aspect ratios of 1/12 , 2/6, 3/4, 4/3, 6/2 and 12/1. After I decide how many pieces I want in the puzzle, I then determine which aspect ratio best fits the image.

    There is still the problem with making the pieces perfectly square. I do this by trimming the source image just enough on either the top and bottom or left and right to make the image width and height evenly divisible by aspect ratio .

    Make sense?

  4. びっくり Says:

    Yeah, everything makes sense. I suppose I should have googled ‘blit’ before asking my question. I like all of your ideas: dragging pieces to other windows hits the cookie sheet idea on the nose.

    As usual, another great post, but I’m baffled why there isn’t a string of other interested parties posting; perhaps the holidays. Or perhaps what I find most interesting doesn’t have general draw.

  5. J Says:

    Were you able to accomplish the bit operations using lisp, or did you use a more low level environment like C?

  6. anthonyf Says:

    J,

    I have two functions written in Lisp that do bit operations. ROTATE-SURFACE and FLOOD-FILL. In Lispworks I can get the functions to run fast enough, but admittedly not as fast as C. In Clisp, the functions run very slow.

    If I were to release my game in Clisp, then I would have to write C versions of the rotate and flood fill functions. For now I plan on doing most of the development in Lispworks.

    I’ve heard that SBCL runs quite well on Windows now so I may see what kind of bit operation performance I get out of that.

    Anthony

  7. LukeC Says:

    Anthony,

    How is your game progressing? Did you manage to convert to the new *-sdl API’s?

  8. anthonyf Says:

    Hi Luke,

    The game is progressing slowly. I’ve been working on converting to the new API’s. It is mostly working but I’m having some problems with ROTOZOOM. I’m not sure if its user error or bug. You may see a post about it on the forums soon!

    When I get everything working like it did before the API changes I plan on adding enough features to make it fun and playable, then make it available to others to download. After that I will decide if I want to go any further with it, or start something new.

    I appreciate all of your hard work on the lispbuilder-sdl-* libraries. I think Lisp is a great programming language for games and having a good set of libraries will make it attractive to more game developers. I would really like to see Lisp and lispbuilder-sdl become as popular (or more!) as PyGame.

  9. sunkissd1 Says:

    I would like to volunteer my services as a test dummy for games. This looks like it would be especially fun & challenging!

    Sudoku has a new twist too. Rather than numbers being preset, the twist has varying groups of blocks that are supposed to not only add up to the total designated to said groups of blocks, but they still have to fit in the 9 square grid as well as the vertical and horizontal rows.

  10. v.dhivya Says:

    i want the jigsaw game

  11. KerinDorinevich Says:

    Hello dear gentlemen

    Nowadays in Canada business called toronto escorts services or toronto vip escorts quickly moving. And contributed to this, in first, toronto escort service or toronto female escorts, Services which is in demand among medium business, officials and other representatives Business. escort service in toronto or female escorts in toronto Practice only female, who were able to obtain higher education, learn skills and manners of possessing its own image.Girls in toronto incall escort or escort services in toronto well and clever, and the level of their sex skills, able to hit and even kept from the mind of any gentleman. Most this girls in toronto escorts or incall escorts in toronto demand not only for carnal comfort,but as a representative environment for the Meetings and other elite events. There are cases, when toronto escort agencies or escort in toronto for Miscellaneous people after meeting with such female passes in long-term relationships.escorts in toronto many Masters. http://www.callvipgirls.com/toronto.htm

    Sincerely, your friend Felix

Leave a Reply