Games I’ve Coded – Adventure Race

Finally, here’s the last of the embedded Nokia Series 40 “Games I’ve Coded”, Adventure Race (AR).

AR SplashNokia 5140i

Originally available on the Nokia 5140i, a beefed up successor to the 5100 that came with Sky Diver.  The 5140i was one of the newer breed of Series 40 2nd Edition devices, which included increased heap memory (up from 200k to 500k), bigger jar file size limits (up from 64k to 128k) and the MIDP 2.0 specification programming interfaces.  It still had a 128×128 pixel display with a pretty slow refresh, but we’d gotten used to that by now!

The device was aimed at outdoor type people, with a splash proof case, digital compass, thermometer and built in flash-light.  Nokia at the time were sponsoring and entering teams into Adventure Sports events, and wanted a game on their ‘sporty’ device to reflect this, so we (Iomo) proposed Adventure Race, and it was my job to code it.  From the outset, with the extra memory to fill, the scope of AR was bigger than my previous games, and this was the first one that I was going to code completely from scratch.  Almost none of the game’s ideas had been prototyped by someone else first, or coded for other devices, so this was my first completely ‘solo’ programming project.

The game concept involved bringing together 3 sports, cycling, running and kayaking, into ‘iron man’ like events, with some orienteering thrown in for good measure.  I’ll write about each sport game here including any coding high (and low) points that I can remember.  But first, here’s a video…

There’s another video at the end of the post if you’d like to see some more 🙂


The cycling game was based around rhythmic button presses to move the bike forward (pedalling), with interspersed downhill sections (free-wheeling) where the player was required to move left and right, or in and out of the screen, avoiding obstacles.  At the bottom of the hills the action back to the rhythmic pedalling again.  The correct rhythm was key to moving along at a decent pace, and avoiding obstacles so as to not crash got you the best time for the section.

The interesting bit of code that came out of this game was the algorithm to generate a terrain to cycle along, with hills and flats.  I found details on the net for midpoint displacement, and it was so simple, and exactly what I wanted for a 2D terrain that coding it was a no brainer.  The concept is simple:

  1. Take a straight horizontal line.
  2. Find the mid point, then displace that point vertically by a random amount.
  3. Then repeat for the two new line segments, and so on.

By varying the ranges of the random numbers, and the number of times you repeat the iteration, you end up with very realistic looking mountain ridge lines, it’s awesome.  Here’s the code I ended up with:

 course_line_segments = 1;
 course_map = new int[4];
 course_map[course_map_x1] = 0;
 course_map[course_map_h1] = 0;
 course_map[course_map_x2] = (256<<8);
 course_map[course_map_h2] = 0;

 int roughness = 32;     // Should be between 0 and 128
 int iterations = 4;     // 4 gives a good value
 int displace = 32768;   // +/-
 for(i = 0; i < iterations; i++) {
    new_course_map = new int[course_line_segments << 3]; // (course_line_segments*2) * 4

    // for each course line segment, we output 2 new segments
    for(int j = 0; j < course_line_segments; j++) {
       int x1 = course_map[(j<<2)+course_map_x1];
       int h1 = course_map[(j<<2)+course_map_h1];
       int x2 = course_map[(j<<2)+course_map_x2];
       int h2 = course_map[(j<<2)+course_map_h2];

       // first new segment
       new_course_map[(j<<3)+course_map_x1] = x1;
       new_course_map[(j<<3)+course_map_h1] = h1;
       new_course_map[(j<<3)+course_map_x2] = x1 + ((x2-x1)>>1);
       int rn = random(displace);
       int mid_height = (h2>h1?h2-h1:h1-h2)>>1;
       new_course_map[(j<<3)+course_map_h2] = mid_height + (rn - (displace>>1));

       // second new segment
       new_course_map[4+(j<<3)+course_map_x1] = new_course_map[(j<<3)+course_map_x2];
       new_course_map[4+(j<<3)+course_map_h1] = new_course_map[(j<<3)+course_map_h2];
       new_course_map[4+(j<<3)+course_map_x2] = x2;
       new_course_map[4+(j<<3)+course_map_h2] = h2;

    course_map = new_course_map;
    new_course_map = null;

    course_line_segments <<= 1;
    displace = (displace*(roughness+128))>>8;

I’ve created a simple demo using this code and packaged it up into a Java class that just displays a window on the screen 128×128 pixels in size, and continuously creates new displacement ridge lines.  It looks a bit like an audio monitor or something!  Change the iterations to 8 and you get something even more like the output from a speech synthesizer.

AdventureRace Cycling

Drawing the terrain in the game turned out to be pretty easy too.  After scaling the data to something appropriate (smoothing out steep slopes, that sort of thing), simply draw a rectangle on the right hand side of the screen, shift the screen buffer left by the width of the rectangle, then draw another one a bit taller or shorter, depending upon the current slope.  This approach is very easy to tune for performance too.  By drawing larger rectangles, you need fewer of them (which means drawing a screen is faster), however the trade off is that the terrain looks blockier.  If you look at the screenshot below, you can see all the rectangles for a screen coloured differently.  The best speed/visual trade off turned out to be about 24 rectangles or so by the looks of things.


Having sorted out the terrain for the cycling game, reusing it for the running was the obvious thing to do.  Rather than duplicate the rhythm mechanic though, we decided to go with a more resource management approach.  The runner has energy that gets used up the faster he runs, and the faster he runs uphill, running downhill uses less energy.  You directly control the speed of the runner, and have to manage his energy so he gets to the finish line in the best time.  Obviously sprinting flat out isn’t going to work because he’ll quickly get tired and slowly jog along knackered!  Since the running mechanic doesn’t involve constantly pressing keys, we reused the left/right dodge oncoming objects idea from the cycling game and made it a core part of the running, so the player can’t just sit back and watch.  There are also sprint sections where other runners come up and attempt to overtake you, and you must button bash to keep ahead of them.  For fairness, the sprint sections work independently of the energy levels, and there are also energy drinks you can collect on the way.

AdventureRace Running

From the code point of view, there is lots of reuse between the cycling and running games.  The colour palettes are changed, and the control schemes differ, but due to both games using the same core, the game specific bits are quite small.  Even combined, they are still smaller than the kayaking game!


The 3rd game in the sequence was a top down, vertically scrolling kayak game.  I’m pretty sure that this was the 3rd game I implemented too, sorting out the running and cycling first with their similar code, then moving onto the more involved kayaking.  I say more involved because there turned out to be several elements to this game that were (at the time) new to me and things I’d not done before.


As you might be able to see from the screenshot (and the earlier video), this game needed the following major elements:

  • Collision detection for both the water obstacles and the river bank edges.
  • A procedurally generated river bank.
  • A large number of animated sprites.

Collision Detection

The front of the kayak sprite is pointed, and as it’s moving down the river, simple bounding rectangle collision didn’t look all that great.  It was frustrating to get stuck on objects when the point of the kayak was clearly free.  I ended up creating a pixel perfect collision system based on pre-generated bit masks for all the sprites.  Using bit masks, and performing shifts and logical & operations after two sprites had failed a bounding rectangle collision test worked very well.  All I had to do was write a simple tool that took a sprite PNG file and generated mask bytes for each pixel row based on the non-transparent pixels.  Skimming edges of the river and the rocks felt nice after that.

Spline Curve River Bank

The best way to generate the river bank turned out to be using two natural cubic spline curves (one for each bank).  By generating the curve in sections as the river scrolled up the screen, only a few calculations needed to be performed each frame to keep things moving along.  Obviously the first screen had to all generated before display, but that was done during the loading phase.  To get the graduated blue to yellow effect, I simple scaled the x value of the spline calculation, so getting a decent looking ‘shallows’ effect.

I was really proud of that spline curve shoreline.  It’s segmented calculation and subsequent rendering using only (fast) horizontal lines worked really well.  I’d like to post some of the spline code, but it’s quite awkward to understand with all the fixed point maths, and the specialised segmented calculations.  If I get any requests to see it, and I can be bothered to tidy it into something I’d be prepared to post (like the midpoint displacement example above) then I might do it!

Animated Sprites

The kayak sprite itself has 52 frames of animation, the wake 24, rolling log 16 (+16 for it’s water splash), 6 frames for the weir, 9 leaping fish frames 15 different rock frames and an 8 frame finish line flag animation.  Not only is that a lot of pixels for one quarter of a 4 game pack that needs to fit into a 128k jar file, but that’s a lot of runtime heap when you needs 2 bytes per pixel. Here is the kayak sprite animation the artist created:

Kayaking Sprite Frames

It’s only pointing one direction since the Nokia UI was used to draw the sprite horizontally flipped when moving to the right.

I ended up compressing the sprites into a palettised format that squashed 3 or 4 pixels into a byte depending upon the number if colours in the image, then RLE compressing this data. Then, rather than loading the data into individual pixel buffers for each sprite, which would take up 2  * width * height bytes for each sprite, I actually decompressed each sprite frame into a single small buffer as it needed drawing, each render cycle.  Because the actual number of operations to decode a sprite wasn’t huge, and there weren’t very large numbers of sprites on the screen at any one time, the 5140i coped perfectly well.  This technique saved loads of heap memory and I allowed for a really richly animated game in total, not just for the kayaking section.


Finally, the ‘filler’ section was a bit of orienteering.  This involved seeing a sort of first person view from a position on a map, and you had to guess where you were.  The concept had already been done by iomo as a WAP game, and I found a screen grab!

Orienteering WAP

The code for this was a simple perspective projection of a 2D bitmap I borrowed from one of the other programmers at iomo.    Since this was done last, it ended up being a bit squeezed for development time and only 4 maps were included in the end, with some rotation for variation.  Still, it was a nice enough short diversion in between the other fairly long and involved sections of the game.


Whilst looking over the bits of my old project (remember this dates back to around mid 2003) I also happened upon probably the most reused bit of code I’ve ever wrote.  The ending sequence for AR was a podium animation with the player getting 1st, 2st or 3rd, and a failure screen where they were weeping for their loss.  In the background of the podium animation I added some simple particle fireworks, which you can see in this video:

That small particle routine has been reused in a considerable number of my games since then, and it’s still included in my very latest project, that’s being coded this minute.  Having also showed it to Mike Tucker, another iomo original programmer and all round awesome guy, he reused it all over the place too.  I wouldn’t be surprised if the same code is the basis for the particles you see in the very latest Flash stuff Mike does!

Finally finally, thanks for reading this far and as a treat here’s my original programmer art splash screen.  Much better than the professional artist created one if you ask me 🙂

Programmer Art

, , ,

2 Responses to Games I’ve Coded – Adventure Race

  1. MordanNo Gravatar October 15, 2010 at 2:49 pm #

    Thx for the great blog. Love reading it. I’m myself a Java hobbyist


  1. Fix My Bugs » Games I’ve Coded – Thief Deadly Shadows - January 22, 2011

    […] was Thief: Deadly Shadows. I’d finished off several Nokia ‘white label’ games (Adventure Race being the last), and the next project to come along was commissioned by Eidos Mobile, and based on […]

Leave a Reply

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

Powered by WordPress. Designed by WooThemes