Created June 8th, 2014. Built on the Raspberry Pi.
This is Devon’s favorite game so far and the very first one we created. The inspiration for this game started with this tutorial on wikihow How to Create a Racing Game in Scratch.
If you’re just getting started with building things in Scratch, you should definitely go through that tutorial first. Once we had the basic game in pace, we started adding improvements to it like checkpoints to prevent cheating (my wife figured out you can just drive on the grass to complete a lap quickly) and bombs which can be launched at other drivers.
Here are the sprites we ended up with:
The stage code sets up our variables and turns off the cars when someone wins.
For the car, we created two different costumes so it can blow up if hit with a bomb.
The code for the car is pretty complex and I probably added too many “When Clicked” handlers. The basic movement is controlled by the right and left arrows (or the “A” and “D” keys). We also move the car forward 2 steps if it is touching the white track and only one step otherwise. We have tests for if the checkpoints have been hit and controls for using a power boost (which moves the car ahead 40 spaces) or launching bombs. Figuring out the bombs was a bit tricky because I had to determine the final x and y position of the bomb using some trigonometry.
Since we know the angle we’re going to throw the bomb (the direction the car is facing) and we know the distance we want to throw the bomb (the “bomb throw distance” variable), to get the ratio of the X value we need the Sine function. Sine = Opposite divided by the Hypotenuse, S(angle) = O/H. To get the O value, we need multiply both sides by H for O = S(angle) * H. For every degree we add, the X value will be slightly less. For the Y value, we use Tangent which is the Opposite over the Hypotenuse.
It was fun trying to explain these concepts to my five year old. It would be accurate to say he got a little bored at this point.
When Corinne figured out how to cheat in order to beat her five-year-old son (sad, isn’t?), we had to add checkpoints. If you don’t go through each checkpoint, the lap counter won’t increase.
Later we added the bombs which broadcasts a message the cars listen for and change their costume to as needed.
Devon discovered a bug that you can still launch a bomb even if your car has blown up. As with all the bugs we find testing out the games, I wanted to fix it but he wouldn’t let me. He’s used this bug more than a few times now to blow me up even after he has blown up. 🙂
The code shown here is just for one car’s needs. You’ll have to duplicate them for the other car and change the variables and broadcast messages accordingly.
We had fun building out the car sprites also. The editor takes a little getting used to, but if you zoom in far enough and set the brush size small enough, you can do some fun detail work like this explosion costume.
Here’s a video of us playing the game with some of Devon’s neighborhood friends (prior to adding the bombs).
Here’s the full Scratch file for Race Game with Bombs so you can download it and play yourself.