Bite Sized Game Development : Basic AI for top down racing games


Attached Files

The following files have been attached to this tutorial:



Download now 7.48 KB


Download now 66.39 KB


Download now 198.18 KB


6,997 visits, 11,028 views


This tutorial hasn't been translated.


In this tutorial, I'm going to attempt to create a simple top-down racing protoype. In this game, the other cars in the race will be played by the computer who will attempt to go around the track faster than the player.

We're going to build this up from scratch, starting with building the track and the game rules for one player, and then adding in the computer players near the end. The computer players will have relatively simple behaviour so this tutorial is good for beginners.

Lets get started!

Part 1 : Building the game area

Getting started on a racing game in construct 2 is quite simple, there are a couple of examples out there, and there's a car object behaviour which handles most of what we need to get going. First lets find some assets.

I have used two sprites from for this. The track sprite is from a small pack by TRBRY and the car sprite is made by sheikh_tuhin & qubodup. Remember the GOLDEN RULE if you use assets someone created and posted on an open source platform : Provide credit in your games and comment on the creator's work!

We'll also need some sort of sprite to give the 'solid' behaviour to act as a wall, and a background of some sort to give the game a little colour. Once our sprites are imported we can start using the track pieces to build our race course, which thanks to construct 2's editor should be relatively simple. Place the background, walls, car and track wherever works, but make sure you give the car sprite the 'car' and 'scroll to' behaviours, and the walls the 'solid' behaviour, and ensure you have the entire game area bordered by walls so theres no chance of the player 'escaping'.

The last thing we need to do to make this little prototype a bit of extra life is to make the car go faster when its on track, and slower when its off track. Ensure the track's collision polygons are correct, and use the events below to make the car's speed faster when its on the track, making sure you have all track sprites in a family :

Of course, you can adjust these values as you find appropriate. Heres a playable prototype of the track which has all the turns and straights in it I plan to use, and a few walls to crash into, for testing purposes :

Racer V0.1

Part 2 : Adding basic car AI

So we have a basic track the player can drive around, lets look at a way we can create a computer controlled car for the player to race against. Lets add another sprite in for the computer to control, with slightly different colours, and create a family between the two car sprites as they will share many of the same behaviours, however, make sure you disable the 'default controls' for the AI cars, otherwise the player will also control them!

Heres the big question : How do we get the computer to drive around the track? AI is always a tricky task for any game, and for each game theres plenty of options available to create a viable and fun compuer opponent to play against. Here's how I've gone about tackling this specific problem:

I've placed a waypoint sprite at each turn in the track. Each waypoint has an instance variable that states what number waypoint it is in the track. We are going to get the computer to drive their car towards each waypoint, moving them on to the next after they reach each one. When they complete a lap by reaching the last waypoint in the track, it will simply loop them back to the first waypoint again and they will start another lap.

Im going to create another sprite, a simple line, and put the origin point at one end of it. For every tick, place the line on the front of the AI car, and use the 'set angle towards' action to get it to point at the next waypoint (the value of which is kept as an instance variable in the AI car, and is increased every time the car passes over a waypoint), we can use this to determine what angle the AI car should be pointing towards. If the AI car's angle is not within 10 degrees of the target angle, we simulate control and make it turn until it does hit the target angle, otherwise, we simulate control 'accellerate'.

Like before, you can alter the amount of degrees the computer can be off before it corrects itself, and you can move the waypoints around to tweak the behaviour. Here is the current prototype of this early stage of AI in action, it shows all the waypoints and the guide line for the AI car :

Racer V0.2

Part 3 : A more complex track and better AI

Ok, we have a track and a computer opponent to race around it, but the track right now is a little dull. Lets change some track pieces and add some extra walls, but be sure to add a waypoint at every turn, and make sure the waypoints instance variables are numbered correctly. Also dont forget to update the 'total waypoints' variable!

Heres a screenshot of the new track :

Lets test our AI out on this new track, I wonder if you can guess what happens?

Use this link if the above gif does not animate :

Disaster! The AI car will make it round the track, but as we have only implemented turning right, when a left turn comes up, the car drives around in almost a full circle to get to its destination!

How can we fix this? We simply need to add a new pair of events in our event sheet that take place after we compute wether the car has to turn or not. The events in the screenshot below basically asks if the angle the car is facing on is to the left or to the right of the angle of the pointer, and then simulates control for the car dependant on the answer.

The car should now drive around the track, completing both left and right turns successfully. To fully test this AI works, im going to edit the track further, making half of the track slower with lots of turns, and the other half out of long straights. It might have some dodgy looking walls and track in places, but the result is linked below. The first link is with the waypoint markers and lines in place, the second link is without.

Racer V0.3 (guides visible)

Racer 0.3B (guides invisible)

Part 4 : Multiple AI opponents

Lets make the race more exciting! Instead of just having one opponent, lets have three! Create two additional car sprites, and two additional AI pointers for the cars to use. Create an instance variable for the AI Cars object called AICarNumber, and an instance variable for the pointers called assigned to car. We are going to use these instance variables to 'pair' each car with a pointer.

So how do we make sure the AI events pair the correct car with the correct pointer each tick? This is more simple than it looks, just use a for loop, with an added condition to select the right pointer, and make the rest of the AI a sub event of this event :

Thats pretty much it! The computer controlled racers will now race around the track and jostle for position, and if they crash or get thrown off course they will do their best to recover and get back on track.

Part 5 : Improvements and additional details

So, we have a decent track and three computer drivers that can race around the track and correct themselves if they are pushed off their racing line. The AI and some of the game mechanics could use some work, but for now I will save that for a more advanced tutorial.

If you would like to use what I have made currently, without any further improvements or additions, I have released the .capx file and the game in its current state on the arcade to be used as an example. When playing the game, you can hold space to see the AI helpers. The arcade link is here : AI Racer example

Before I could release this as a full game, I'd have to add some touches to make this into a real game, such as :

- A simple menu screen that starts the game up, and clicking start on the menu screen starts a countdown which gives the player time to get ready before the race starts.

- Sound effects for accelleration, braking, crashing, the start of race countdown and the finish.

- A way to keep hold of the lap count and display it during the race, so that we know how many laps have passed while the race is going on and when it is finished.

- A timer that shows how much time has passed since the race started.

- Win/Loss screen after race ends depending on wether the player or the computer has won.

- Play again button which either restarts the layout or resets everything to their original values and positions.

These additions should put the game into a releaseable state.

Thats the end of this tutorial! There's a good platform for expansion here, so I'll likely come back to this, perhaps to write a tutorial for improved AI. Hope this helps someone!

Please leave a comment or any feedback you have in the usual place, thanks!



Download now 7.48 KB


Download now 66.39 KB


Download now 198.18 KB

  • Order by
Want to leave a comment? Login or Register an account!
  • I am so Lost, I just started using construct so this is very new and confusing. Can you help me or translate a little?