Pong in Clojure

Me and Cesar Canassa decided to create a game. I suggested a roguelike, which we will build eventually, but as both newbies in game development, we are going to start small.

With Pong.

In the last couple months I have been teaching myself some Clojure, and said to Canassa I would like to try writing Pong using it. Now here’s where his personality shines: He had never written a single line of Clojure, but said “sure!”. Later on, while researching about functional programming, he mentioned how he had read in several places how one of the few areas of development where this paradigm is not recommended is… game development.

Ooops. Too late.

Beers in hands on a Saturday night, we promised to get something running. The plan didn’t go well. Our lack of Java and Clojure knowledge, and our lack of intelligence after ten beers got in the way. We managed to open a black window and draw white lines on it, but at the same time there was a sense of struggling, a fight against the language and its libraries. We were driving the car down the hill again and again.

Somehow on Sunday, Canassa created a repository on Bitbucket for Clojure Pong, and the first changesets started to roll in.

Today we finished the game. We thought about adding sound to the ball collision on racquets and walls but the game already does much more than training and learning code should do.

We are looking for criticism, specially on idiomatic Clojure. We tried to replace the usual maintaining of a “global” state in variables that is possible in other languages and paradigms, with a “game” map that flies around functions. It is a simple approach, but probably contains a lot of unnecessary overhead. Not in terms of performance, but in terms of function responsibility.

For example, the racquet collision function takes the whole game as parameter. Feels like cheating: Instead of using variables sprinkled to determine what is happening, the values are keys of “game”. It feels like we are still using state, but it is concentrated on a map structure.

But it works, and it is also probably more fun than training and learning code should be. There is a pause screen, the ball rolls slightly angled on game start and scoring, and a FPS counter.

We are going to use some of these stuff on our next project. Breakout is coming, and I will post again when we have some news.

  • tonyl

    I am a bit of a newbie to clojure and game development :P
    But I heard that usually to keep the state of the game is either the approached you went for, or if it is to heavy on performance to have the game map as a ref and work the state of the game like that. Prog21 has a good list of how to program a game in functional programming. I’ve started reading them, because like you I want to learn to do that :P http://prog21.dadgum.com/23.html

  • Pingback: Clojure GUI demo of Pong | Jumble Agile Manuals