Mathematics in Games Programming

Welcome to our Car Race Game made in Scratch.

Click the Green Flag to start and then choose one of the three race tracks to use.

Use the following keys to drive around the track:

Use the Up Arrow to accelerate forwards, and the Down arrow to reverse.

Use the Left and Right Arrows for steering, and Space Bar for brakes.

(The faster you go the tighter the steering, so be careful not to oversteer at higher speeds).

If you hit the grass, the car slows down dramatically.

The car has a limiter that slows it back down if maximum speed is exceeded.

The car is hard to steer once the speed gets up, and stays at a constant speed until you either brake or hit the grass.

Concentrate mainly on steering with the left arrow key, and only tap the up key to go faster as you get better on each lap.

It is best to drive the car towards the middle or the outside of the track. Keep practicing until you achieve mastery.

Try holding down on the up arrow, and when the car gets too fast, you should see a pair of tyre skids appear which slow the car down.


We made this game here at Passy World, and students in our IT class get to make this game as part of their computer programming course.

In this lesson we discuss the Algebra and Mathematics contained in the game.

Computer Programming, especially Games Programming, involves lots of mathematics, and so a good grounding in math is essential for anyone wanting to become a computer programmer.


The Mathematics in This Game

The Car Race Time Trial Game contains lots of Mathematics in it, even though it is a fairly “simple” game.

The object of the game is to drive the car around the track as fast as possible, and see what your fastest lap time is over a 10 lap race.

There is plenty of Algebra involved with controlling the car, counting the laps, timing the laps, and producing dual tyre skids.


Controlling the Car

The main variable we use is “speed”. We could have written an Algebra “s” for this variable name, but to make the script code easier to read we have called the variable “speed”.

The following script code contains the parts which control the driving of the car.

Computer Game Math 1
Image Copyright 2012 by Passy’s World

Note the mathematics done on the speed within the arrow controls.

We do the Algebra “New speed = speed + 0.4″ for every time the up arrow accelerator is pushed.

The car can also slow down and reverse, using the down arrow, where we use negative numbers and do “”New speed = speed + -0.4″

For the turning the car we use rotation angles, but we need to multiply the angle by whatever the current value of the “speed” variable is so that the car turns tighter at faster speeds.

If we go off the track and touch the green grass at all, our car slows down, by dividing the speed by a number bigger than 1, in this case 1.1.

The Algebra is: “New Speed = Speed / 1.1″


Counting the Laps

This is quite simple, all we need to do is set up a variable called “Lap” and at the start set it to zero.

Then everytime we reach the finish line do the Algebra “Lap” = “Lap + 1″

The script code below shows this, as well as some more complicated Algebra for working out how long it took us to complete that lap.

Computer Game Math 2
Image Copyright 2012 by Passy’s World


Timing the Laps

The screen shot shown in the previous section also includes Algebra to work out the Lap Time each time we complete a lap.

This is tricky because Scratch has a continual clock running called a “Timer”. So we need to record the time when we start the lap, and also record the time on the clock when we finish the lap.

To work out the Lap Time we do the Algebra: “Lap Time = Time on clock timer now – Time on clock timer when we started the lap”.

Eg. If the timer was at 12 seconds when we started a lap, and 18 seconds when we finished the lap, then our Lap Time is 18 – 12 = 6 seconds.

For the next lap the timer does not reset, and will start on 18 seconds, and if at the end of the next lap was at 28 seconds, then our next lap time will be 28 – 18 = 10 seconds.


Working Out the Fastest Lap Time

Computer Game Math 3
Image Copyright 2012 by Passy’s World

Keeping track of the current fastest lap time involves some fairly tricky Algebra in the Game script code, as shown above.

It turns out that we need to use three variables: “Lap Time”, “Best”, and “Previous Best”.

“Best” means the quickest, and needs to be our lowest “Lap Time” so far in the race.

We use greater than and less than inequalities checking to see if the “Lap Time” for the lap we just completed is better than our Previous Best”.

We then have Algebra to shuffle around the values so that the “Best” variable always has as its value our quickest lap time so far.


Producing Dual Tyre Skids

Computer game Math 5
Image Copyright 2012 by Passy’s World

Producing dual tyre skids proved to be the most challenging part of making the car game.

Skids are shown on the track for three seconds everytime the car goes over its top speed of “7”, and automatically applies its brakes.

A different pair of skids are also shown whenever we hit the space bar and apply the brakes.

All skid marks are cleared from the track at the end of each lap.

Making skids involves using (x,y) coordinates, Algebra, and Trigonometry.

The tyre skid needs to go in the opposite direction that the car is going in, so it would make sense to make the angle of the skid to be 180.

However we actually need to make the angle less than this, so that we can splay the skid direction out at an angle, and then use Sin and Cos Trigonometry to move the (x,y) coordinates of the skid to a position at the back of the car that will look like it came from the tyres.

For this reason, we need to have two separate skids that we run at the same time: a Left Skid and a Right Skid.

Let’s take a look at how we make the left tyre skid.

Consider this diagram of the car.

Computer Game Trigonometry 1
Image Copyright 2012 by Passy’s World

The (x,y) position of the car is located at the center of the car.

If we put the skid at a full -180 to the front of the car, then we will only get one tyre skid coming from the very middle of the back of the car.

If we set the skid direction as being -150, rather than -180 then we get the diagram shown above.

Angle A is the internal angle in our triangle, which will be 60 degrees, (because the other angle of the triangle next to white dot is 180 – 150 = 30 degrees).

The yellow dot is where we want the (x,y) coordinates of the left tyre skid to be, relative to the white dot where the car center is.

To get the yellow dot’s coordinates, we do some Trigonometry as follows :

x of skid = the x of the car + (sinA x the blue hypotenuse)

y of skid = the x of the car + (CosA x the blue hypotenuse)

The green line marked SinA in our car diagram will be negative because we are in the second quadrant of the Cartesian Plane.

The pink line marked CosA will be positive in this second quadrant.

This will have the effect of moving the white dot of the car, across to the yellow dot where we want the skid to be.

The Car’s X coordinate will have the green line length subtracted from it, and the y coordinate of the car’s center will have the length of the pink line added to it.

Because Sin and Cos will have positive or negative values depending on which of the four quadrants the Angle of the skid is, this translation of coordinates works for the full 360 degrees of direction that the car can drive in.

This is very neat use of the Trigonometry of the standard 360 Degree Unit Circle.


We write the prrocessing into the Scratch Code with the variable “skidDir” to be the angular direction of the skid at any time.

We then do the Trig Calculation in a very long piece of code at the bottom of this script.

Computer Game Math 7
Image Copyright 2012 by Passy’s World

Here are the bottom of screen blue code for the coordinates magnified to be a bit clearer:

Computer Game Trigonometry 2
Image Copyright 2012 by Passy’s World


Computer Game Trigonometry 1
Image Copyright 2012 by Passy’s World

The Right Tyre skid is made exactly the same way, except that we set the “skidDir” angle to be the car’s angle +150, rather than -150.


Mathematics of Games Programming

Screens of Computer Games
Image Source:

If you want to take a detailed look into the Mathematics of Games Programming, then the following website has plenty of documents and PowerPoints about this subject:


Related Items

Algebra Expressions
Algebra Terms Coefficients Variables and Constants.
Algebra Substitution Using Positive Numbers.
Algebra Substitution Using Negative Numbers.
Basic Powers Exponents and Indices.
Substitution Using Powers Exponents and Indices.
Identifying and Combining Like Terms.
Basic Algebra Multiplication.
Algebra Exponents Multiplication.
Real World Algebra Formulas
Survivor Algebra – Class Activity

If you enjoyed this post, why not get a free subscription to our website.
You can then receive notifications of new pages directly to your email address.

Go to the subscribe area on the right hand sidebar, fill in your email address and then click the “Subscribe” button.

To find out exactly how free subscription works, click the following link:

How Free Subscription Works

If you would like to submit an idea for an article, or be a guest writer on our blog, then please email us at the hotmail address shown below.

Email address image

Feel free to link to any of our Lessons, share them on social networking sites, or use them on Learning Management Systems in Schools.


This entry was posted in Algebra, Math in the Real World, Trigonometry and tagged , , , , , , , , . Bookmark the permalink.

One Response to Mathematics in Games Programming

  1. Pingback: Mathematics of the Melbourne Cup | Passy's World of Mathematics

Leave a Reply