This is probably not a secret for anyone, but I am a big fan of the Scala programming language. I’ve been taught functional programming first in Scala, and since them I’ve never looked back. So when I decided to get into Android game development, choosing Scala was a no-brainer.
I had modest goals for this project. First, I wanted to learn more about the Android framework, and in particular its graphics primitives. Second, I wanted to get started with writing games. In particular, I set the rule of not using an engine or any third-party library, my learning objectives was to see how a game is made just from the basic 2D drawing primitives. Finally, I wanted to build something fun, hopefully.
I had a few ideas of games, but, for my first game ever, I had to be reasonable and pick something simple. Since I was targeting Android, I aimed at a design that would be touchscreen-friendly. My brother told me about a puzzle flash game that was pretty entertaining and addictive, and I was sold. The game was simple to play and would work great on a mobile device. It was also relatively basic, with no real-time needs, a discrete tile-map, and no physics and collisions. It seemed like a perfect way to get my hands dirty with game programming.
I decided that in my version of the game, the goal would be to protect a delicious cheese from an evil rat. Each turn, the player can place a rat trap on the map, blocking the path of the rat towards the exit that leads to the cheese. The player wins if the rat is totally trapped, and loses if the rat reaches the borders of the map.
I started implementing the basic functionalities of the game. It was a simple game, but it still had a few algorithmic thingies. In particular, I had to implement a basic AI for the rat. I represented the tilemap as a graph with one node per tile, edges between adjacent tiles, and reduced the path finding to a shortest path problem from the rat to an exit node. Tiles with traps were marked as blocked, and the shortest path algorithm was searching a path through open nodes. For the record, I used the cake pattern to represent the graph. The graph seemed to fit nicely into the use case decribed in this paper. I know it may seem overkill, and now that I think about it, it clearly is.
Ok, I have a working game, but it was still missing something.. ah right, graphics. Unfortunately, I didn’t know how to draw, and I wasn’t interested in buying (or stealing) art on the web. I could have asked a designer friend, but it was my game and I wanted to do it myself. So, I learnt myself a GIMP for Great Good, and started drawing the different elements of my game. Finally, after quite a bit of struggle, I got to version 0.1 of Rat Trap:
It doesn’t look that pretty, but back then I was quite proud of it. There was one fixed-size map, randomly generated (that is, a few traps randomly positioned on the map), and you had to prevent the rat from exiting the map and reaching the delicious cheese.
I released that version — my first Android game! After getting some feedback, positive from friends, a bit more negative from strangers, I worked on an improved version. The core gameplay was relatively addictive, and I had a few ideas of how to integrate additional features to make the puzzle element richer. Additionally, I decided to add levels, in order to ease user’s progression.
As expected, I had made quite a few mistakes on that original version, one of them being to draw my art using raster graphics. I decided to redo all the art of the game, this time using a vector graphics editor. I would still be manipulating bitmap in the code, but having the source of the art in vectorial form would make it much easier to edit and animate before exporting to a raster image. I picked up Inkscape, which is a truly great software. Combining Inkscape with some more programming, I eventually completed version 1.0 of Rat Trap:
As you can see, there are now many more elements on the game screen. I expanded the game with a few additional kind of actions, such as using fake cheese to lure the rat or using a hammer to stun it for one turn. Also, there is now real cheese on the board. I drew each of the actions, buttons, cheeses, as well as the rat, in Inkscape. Having everything as a vector is very convenient to support multiple DPI, since one can simply rescale all the sprites to make them the right size for the different screen densities.
On the coding side, the extensions fit nicely into my original graph representation. Instead of marking each border node as goal in the graph, I could now just mark the few nodes that contained cheeses, and still use the shortest path algorithm. The tunnel mecanisms was implemented by simply adding additional edges between tunnel tiles. I had to create a level description language and a parser. Ideally I should have developed a level editor as well, but I went for the quick and dirty solution and used VIM as my level editor. The level description is a very simple, homebrew, plain text format.
Rat Trap is available on Google Play. I just released the latest version a few days ago. If you do try it, I would very much like to hear feedback from you! I really enjoyed programming and designing this game, and I’m looking forward to making other games in the future!