Skip to main content

SpaceRat - an exercise in game programming

Almost entirely throughout my life, I’ve had access to computers. In most of that time I’ve wanted to write computer games. Most of my attempts have been aborted early on, before even getting code written. Stacks and stacks of paper have been used to sketch characters and scenery. One day in 2001 I decided to put together a small Space Invaders-like game named SpaceRat.


SpaceRat began as a set of graphics I had created for a friend’s clone of Space Invaders. I had drawn a “hero” space ship and some enemy ships. There were a number of enemy sprites that I did not use in SpaceRat, simple because they did not fit the look I was going for.


Starting off with pre-made graphics gave me the push I needed and gave me the opportunity to jump straight into programming the game. I was still in university, but was then employed in a co-operative education placement for discreet, a division of AutoDesk. Health problems prevented me from actually working at the time, and so to judge my ability to return to work, I decided that writing a game would be a good gauge.


Since I had been working at co-operative placements for my third year, and been a programmer before I had even entered university, I was able to develop a certain affinity for object-oriented design and clean code. It was with that premise that I set down the basic classes for the game’s mechanics. While the view and model aspects are not entirely factored out, the size of the codebase made that sort of thing overkill.


The game mechanics were simple from the start; rectangular bounding boxes for collision detection and a very simple physical simulation. You could say that it’s a trivial game engine, and you’d be right. I kept it simple so that I could concentrate on getting the entire project completed. While there is definitely room for expansion, with many tasks sitting in a TODO file, the basic engine is complete.


My favourite part of the completed project has always been the star field in the background. Many games with one simply scroll a static set of uniformly-sized stars at a set speed. I was never happy with that, and so I went for stars that varied in size, colour and speed. The result was a star field that gave a sense of depth and realism that just isn’t possible with a bunch of single-pixel stars all moving at the same speed. When I rewrote the engine to support Quartz, Apple’s Mac OS X 2D drawing API, the star field was able to take on an even better look.


I had originally written the code in Mac OS 9 using Carbon-compliant APIs. At the time I was using Metrowerks CodeWarrior, as it was the IDE I was most comfortable with. While the application worked natively under Mac OS X, there was no chance of porting it to x86 without a change of IDE and modifying the API used to draw the graphics and play sound.


The first hurdle I chose to jump was the XCode transition. This was pretty painless, since I had not used any Mac OS 9 specific APIs. The application would no longer be compatible with Mac OS 9, however, but this didn’t bother me at all. I was no longer using Mac OS 9, and many people were already making the move to Mac OS X.


The next step was to modernize the graphics. I had been using QuickDraw, Apple’s venerable drawing API. This was one of the APIs Apple was quick to drop in the move to x86, as it would have been quite difficult to port to a new architecture and didn’t support the modern features Quartz does. Given my simple rendering methods, it was almost trivial. I just had to flip the coordinate system and deal with the sprites as images files rather than icon resources. It also gave me the impetus to move from an integer based physics model to a floating point model. This allowed for smoother movement and smaller velocities than I had started with. The star field improved tremendously with that change alone.


The Quartz transition was good practice for me and gave me insight into Mac OS X’s graphics model. It’s a massive improvement over QuickDraw, which had still been based on a pixel-level resolution and didn’t support anti-aliasing.


One massive sticking point which has blocked me from making the complete x86 transition is my use of Apple’s Sound Manager. It too was dropped in the x86 Carbon transition, and I never took the time to find a fully suitable replacement. In any case, my sound engine had always been sub-par. When I do get around to making the change I’ll probably add 3D sound to give a more immersive experience.


The actual game is fun to play, but after the first level the levels repeat with no sense of real progress. There’s no high-score system, no real level structure and only one type of enemy. Definitely an area that’s seriously lacking panache. I’ve been lazy here, since my original goal had been reached ages ago. There’s certainly room for improvement, but I had never created graphics for new characters beyond what I already had when I started.


While the game itself is not all that fun to play, it does provide some entertainment value. It was certainly an excellent project for learning, and might be of use to someone out there to see how to make a simple game. I have left some of the QuickDraw code in the project, for comparison to the Quartz rendering methods. One good example is SRCgImageSprite versus SRIconSprite.


I can always hope someone will find the code useful, and perhaps the game a little fun. With luck, someone else will come along and fill in the gaps. I’m making the latest code available under the BSD license, so feel free to make modifications.

Comments

Anonymous said…
Bloody good post. I am clueless about this stuff but I have this notion that I could at some level get to grips with it (code etc) and am tentatively exploring options in an entirely arseways manner; but I'm good at that.
This post is one of the best descriptive and sans-bulllshit stories on code that I have to date come across. You have a gift that allows you get your message out clearly and concisely. A lot of writers could do worse than follow your lead; if only!
Well done you.

Popular posts from this blog

Am I Jonesing for the Internet?

I’m feeling a little agitated and jittery today. My internet access is down due to some nasty snow and wind. Are the two related? They might be. I know I’m certainly missing my twitter friends and feeling less in touch with the world. How long is this weather going to hold? I can’t look that up. Sure, I could pull out a radio and listen in, if I had one. I might somewhere, but I’m at the mercy of the broadcaster to decide when to report the weather and how much of it to report. Some argue that internet access should be a basic human right. Does this point of view hold water? I suppose it could be argued that since the internet allows us to draw together into a larger community that it is an essential part of improving the human condition. Its use in political organizing and to connect dissidents in repressive regimes can certainly help make the case for it as a basic human right. Is the jitteriness really from not having the internet? My doctor did just increase my dose of modafi

What Kind of Games?

I started programming when I was young, with the hopes of writing video games. I think a lot of kids start that way. When you like something, or someone, you try to emulate what you’re seeing. But how has that early dream turned out? They tell writers to write what they know. It’s good advice. How can you write about life in the Serengeti without have someone to give you a first hand account or having been there yourself? You can always use your imagination, and that’s all you can really do when writing fantasy or science fiction. It works for writing video games. How can you expect to write a genre you don’t immerse yourself in? These days I spend most of my gaming time playing casual games. I’m busy doing other things, and don’t want to spend long stretches just sitting at the console or computer. Recently I read an article about the kind of video games the most people tend to flock to. Typically they’re games that are relatively simple and involve sorting things in some way. It

Piet - an esoteric programming language

There’s a certain group of programmers out there that like to come up with programming languages just for the fun of it. Some of them have profanity as their names , and some are based on internet memes . Whatever the case may be, some individual out there enjoyed thinking up the language, and many of these languages are actually useable. One esoteric language that stands out, for me, at least, is Piet , created by David Morgan-Mar. Based on the idea of making programs that look like abstract art, Piet allows the programmer to express their software in the form of coloured blocks. Numbers are represented by blocks of pixels containing a pixel count equal to the number itself. Operations are performed by changes in hue or darkness. As an example, here is a Piet program I wrote to output the string “Hello World”. This image is in fact the entirety of the program, and can be run in any of the Piet interpreters out there. Other examples of Hello World programs are available on David’s si