Bike Game in Scratch

Just over a year ago, my son (then 6) wanted to create a computer game where you drive a bike round a world. I helped him with this, and we’ve worked intermittently but fairly often on it since then. There were quite a few details to get right, but he worked through them a bit at a time, and I think the result is pretty cool. We’ve finally clicked ‘share’ on it, so it’s public:

Here’s a video of a play-through:

(There are minor changes between the version in the video compared to the released version; see below.)

Game features

The game takes place in a large world (7½ screens wide and 10 screens high) which you bike around. Your goal is to get through the castle gate, for which you need the key. You have to find where the key is, and then work out how to get to it. Once you have the key, you need to work out how to get to the castle gate.

Much of the map is forbidden to you — water, slime, and mud. Some is dangerous in other ways — long grass can tangle in your bike chain, and broken glass can give you a puncture. You can fly over hazards by biking over a ramp, which sends you into the air, but if you go too fast, you might go too far and land in something dangerous.

Areas of the world are linked by bridges; one bridge is always passable, but to use others, you need the keys. If you get a puncture, you can repair it, as long as you have picked up a spare inner tube.

Scattered round the world are various other goodies to help you. There are pieces of wood, which, if you collect enough of them, allow you to build a bridge, and tools, with which you can build a ramp. There are some good puzzle elements about where you use these abilities, and which order you have to do things in.

Instead of having a large set of instructions at the start, pop-up messages give you information as you play the game, and the map itself has coded clues embedded in the ground to help you.

I think we made it so there are no short-cuts, but it’s possible some ingenious soul will think of something!

(In the final version as compared to the video: you fly only half as far in the air; you have to go even more slowly through long grass.)

Implementation

The world started off life as a hand-drawn map on a large piece of paper. We scanned it in, then used Gimp to draw a ‘clean’ version on top. We knew we’d be coding the different game elements with colours (to use Scratch’s ‘if touching colour x‘ block), so had to get colours consistent.

I then wrote some Python code to turn the resulting 3600×3600 bitmap into small tiles, using tjvr’s Kurt library. There were a few fiddly details of this, but nothing too complicated — Scratch won’t let you position a sprite ‘too far’ off the edge of the screen, and abutting sprites leave a slight join, so some overlap was required. There’s also a separate (slightly fragile) Python script for replacing the map tile sprites when, inevitably, changes to the whole-world bitmap were needed part-way through development. ‘How do I do scrolling’ is covered on the Scratch wiki but auto-generating the tiles and their scripts was easier than hand-creating them all. There is a Mario-Kart-style game with an elegant variant using a small number of sprites, each with a large number of costumes.

There are quite a lot of interactions between the game elements, for example the locked bridges, or the ability to build a ramp or repair a puncture. In-world objects have to adjust their on-screen position to appear stationary in the world; some objects come and go (wood and tools disappear when you pick them up; bridge locks disappear once you have the keys). All of this took a bit of thought.

On my old machine, doing all the collision checks (have you hit some water? have you hit some slime? have you started going up a ramp? etc.) on every game tick was too slow. Instead, we made it work on a 3-phase cycle, and check some collisions in phase-0; some in phase-1; and some in phase-2. The player won’t mind if they get away with briefly brushing against some slime. Even with this, it plays quite slowly, so the video is speeded up compared to how my old machine runs it.

Observations

Helping Zach build this game was good fun. I’m impressed with his persistence over the time it took (c.15% of his life), and it was great to see him getting the hang of it all. Scratch is a good environment for learning the ideas behind this style of programming, and it didn’t feel too clunky to use its block-based language for expressing algorithms.

The end product reminds me a bit of a game called Star Maze that I used to play on the BBC Micro back in the day (video from YouTube user ‘cpmisalive’), in that it’s a very large world which you explore, only seeing a small amount on-screen at a time. Many years ago, I briefly started writing something similar, but didn’t get beyond the scrolling. It was gratifying to finally be part of writing such a game.