Sunday, 1 March 2015

Beginner Tips on Game Programming in Clojure

The barrier to entry on game programming in Clojure is surprisingly low, thanks to the ease of use of play-clj and the excellent example programs provided by Zach Oakes. This makes it possible to get cracking on the actual content of a game very quickly.

That is, assuming one is already a fairly competent programmer. In my case, being a longtime Lisper and possessing a very deep knowledge of Java, it only took a couple hours to get up and running on James' game. But I thought it might be useful for some if I spelled out what the steps I followed were, in case it might help out a newer clojure hacker unsure of what to do when confronted with all these unfamiliar parentheses.

After completing the absolute beginner's first steps (installing a JDK, Clojure, and Leiningen, learning the basics of Clojure syntax, knowing how to execute the example games, and knowing to have the language and library documentation open in tabs of your browser), here's how I got rolling on making a platformer of my own.

I began with a bit of time away from the code, scanning in my son's artwork and retooling it a bit in the GIMP. I then downloaded the Tiled Map Editor and began by modifying the existing map of the example, replacing the art assets with tiles made from my son's drawings.

Next, it was time to begin appropriating the code. Super Koalio's actual game code is contained in just three .clj files, each only about a hundred lines long, so the first step was simply to read over each file in detail and understand what it was doing. Along the way I added comments, and made notes of things I wanted to toy with—either to better understand what was happening by testing different behaviour, or to implement different features (such as double jump).

I then moved the files into a new namespace and began hacking on the game in earnest, but I publish here the original clojure Super Koalio files, annotated with my comments but before I began altering the code, in case they might help guide someone else through the same exercise:

  • core.clj—sets up the game, loads the level, and the main screen rendering routine.
  • entities.clj—mostly concerned with the player character, his animation and movement rules (there are no enemies or other moving sprites in the demo game).
  • utils.clj—utility functions, mostly related to handling player input

I know that my comments reflect an imperfect understanding of the code in some places, but I wanted to present a methodology for figuring this stuff out, rather than official documentation (that's why I use comments instead of docstrings, too). This is my process, and it works pretty well for me.

Once one has the game running and has understood what the source code is doing, the only thing left to do is begin playing with changing the code and adding features. There's no substitute for actually getting one's hands dirty with the code, and as a Lisp, Clojure is particularly easy to play around with, since the code can all be inspected—and modified—even when the program is running. Have fun!

Posted by jon at 10:00 AM in Programming 
 
« March »
SunMonTueWedThuFriSat
1234567
891011121314
15161718192021
22232425262728
293031    
       
 
Older articles
Non enim id agimus ut exerceatur vox, sed ut exerceat.