DEV Community

MASTER CODING ORGANISATION
MASTER CODING ORGANISATION

Posted on

How I Built a Multi-Track Car Racing Game in Scratch

How I Built a Multi-Track Car Racing Game in Scratch

Building a racing game in Scratch starts with a simple idea: make a car move around a track. But once you add speed control, collisions, multiple tracks, different car designs, and random track selection, the project becomes much more interesting.

For this project, I created CAR RACING THROUGH TRACKS!!!, a Scratch racing game where players can drive around three different tracks, change the car's appearance, and interact with different areas of the track.

๐ŸŽฎ Play CAR RACING THROUGH TRACKS!!!

๐ŸŽฎ What I Built

The game includes:

๐Ÿš— Player-controlled car
๐Ÿ›ฃ๏ธ Three different tracks
โšก Variable-based speed system
๐Ÿ’ฅ Collision detection
๐ŸŒณ Track-specific scenery
๐Ÿ”€ Random track switching
๐ŸŽจ Four car appearances
โŒจ๏ธ Keyboard controls

Controls
Key Action
โ†‘ Up Arrow Move forward
โ†“ Down Arrow Move backward
โ† Left Arrow Turn left
โ†’ Right Arrow Turn right
Space Switch to a random track
1โ€“4 Change car appearance

๐Ÿงฉ How the Project Is Organized
The project uses a few main elements:

car โ€” the player-controlled vehicle
collider โ€” used for collision detection
TREES โ€” contains scenery for the different tracks
Three backdrops โ€” Track 1, Track 2, and Track 3

Instead of creating a completely separate game for every track, I reused the same car and changed its position, direction, backdrop, and environment when the track changes.

๐Ÿš— Programming the Car
The car is the main gameplay sprite.

When the game starts, it moves to its starting position, points in the correct direction, and receives an initial speed value.

when green flag clicked
go to x: -179 y: -88
point in direction 0
set [speed] to 2
Enter fullscreen mode Exit fullscreen mode

The important part here is the speed variable.

Instead of putting a fixed movement value into every movement block, I can change the variable during gameplay.

โฌ†๏ธ Moving Forward
The Up Arrow moves the car forward.

if <key [up arrow] pressed?> then
    move (speed) steps

Enter fullscreen mode Exit fullscreen mode

Because the movement uses the speed variable, the car doesn't always have to move at the same speed.

For example:

speed = 2

makes the car move normally, while:

speed = 0.5

makes it move much more slowly.

โฌ‡๏ธ Reversing
The Down Arrow lets the player reverse.

if <key [down arrow] pressed?> then
    move (-1) steps
Enter fullscreen mode Exit fullscreen mode

The negative value makes the car move in the opposite direction.

โ†ฉ๏ธ Turning
The left and right arrow keys control the car's rotation.

if <key [right arrow] pressed?> then
    turn clockwise (5) degrees

if <key [left arrow] pressed?> then
    turn counterclockwise (5) degrees
Enter fullscreen mode Exit fullscreen mode

These controls run continuously, allowing the player to drive and turn at the same time.

โšก Creating a Speed System
One of the things I wanted was for the car to behave differently depending on where it is on the track.

I used color detection to change the speed:

if <touching color ... or touching color ...> then
    set speed to 0.5
else
    set speed to 2
Enter fullscreen mode Exit fullscreen mode

When the car is on the normal driving area, the speed remains at 2.

When it touches one of the specified colors, the speed changes to 0.5.

This gives the track different types of areas instead of making the entire track behave exactly the same way.

๐Ÿ’ฅ Adding Collision Detection
The project also uses a separate sprite called collider.

The basic collision logic is:

forever
    if <touching [collider]?> then
        set speed to 0
Enter fullscreen mode Exit fullscreen mode

When the car touches the collider, its speed becomes zero.

This provides a simple way to make certain parts of the track behave like obstacles or boundaries.

๐Ÿ›ฃ๏ธ Adding Three Tracks
The game has three different tracks:

Track 1
Track 2
Track 3

Each track has a different layout, so the car needs a different starting position and direction.

For Track 1:

when backdrop switches to [track 1]
go to x: -179 y: -88
point in direction 0

Enter fullscreen mode Exit fullscreen mode

For Track 2:

when backdrop switches to [track 2]
go to x: 179 y: 88
point in direction 180
Enter fullscreen mode Exit fullscreen mode

Track 3 also has its own starting position and direction.

This is important because simply changing the backdrop isn't enough. If the car stayed in exactly the same position after changing tracks, it could appear in the wrong place.

๐ŸŒณ Changing the Track Environment
The TREES sprite uses different costumes for the different tracks.

For example:

when backdrop switches to [track 1]
switch costume to [costume1]
Enter fullscreen mode Exit fullscreen mode

And another track can use:

when backdrop switches to [track 2]
switch costume to [costume2]
Enter fullscreen mode Exit fullscreen mode

The same idea is used for Track 3.

Using costumes this way means I can reuse one sprite instead of creating a separate tree sprite for every track.

๐Ÿ”€ Random Track Selection
I also wanted a quick way to change tracks.

Pressing Space selects a random track:

when [space] key pressed
switch backdrop to (pick random 1 to 3)
Enter fullscreen mode Exit fullscreen mode

The pick random 1 to 3 block chooses one of the three available tracks.

So every time Space is pressed, the player can be taken to a randomly selected track.

๐ŸŽจ Adding Different Car Designs
The game has four different car appearances.

Instead of creating four separate car sprites, I used multiple costumes for the same car sprite.

if <key [1] pressed?> then
    switch costume to [car_green_1]

if <key [2] pressed?> then
    switch costume to [car_red_1]

if <key [3] pressed?> then
    switch costume to [car_blue_1]

if <key [4] pressed?> then
    switch costume to [car_black_1]
Enter fullscreen mode Exit fullscreen mode

This lets the player change the car's appearance while staying in the same game.

It's also a useful example of how Scratch costumes can give one sprite multiple visual variations.

๐Ÿง  Bringing Everything Together
At this point, several systems are working together:

Keyboard input controls the car.
The speed variable controls movement speed.
Color detection changes speed in certain areas.
The collider can stop the car.
Backdrop switching changes the track.
Track-specific scripts reposition the car.
Costumes change the car and scenery.
Random selection chooses a track.

None of these systems is especially complicated by itself.

The interesting part was getting them all to work together as one game.

๐Ÿ› Things I Had to Think About
A multi-track game needs more planning than a single-track project.

For every track, I need to consider:

Where should the car start?
Which direction should it face?
Which scenery should appear?
Which areas should slow the car?
Where should collision detection happen?

Adding another track would also mean adding another backdrop and updating the relevant scripts.

Planning these things ahead of time can make debugging much easier.

๐Ÿง  What I Learned
This project gave me a chance to combine several Scratch concepts.

Variables

The speed variable controls how quickly the car moves and can be changed during gameplay.

Keyboard Input

Keyboard events control movement, track switching, and car customization.

Conditions

if blocks allow the game to react to the player's actions and the environment.

Collision Detection

touching blocks can detect when the car reaches particular objects or areas.

Backdrop Events

when backdrop switches to allows sprites to react when the player changes tracks.

Costumes

Multiple costumes make it possible to change the appearance of sprites without creating completely separate sprites.

Random Numbers

pick random 1 to 3 makes the track selection unpredictable.

๐Ÿงฐ Public Assets
I also created a separate Scratch project for the public assets used with the racing project.

Open the CAR RACING Public Assets project

Keeping the assets separate gives the project another place for resources without mixing everything into the main game.

๐Ÿš€ What I'd Add Next
There are still plenty of ways I could expand the game:

๐Ÿ† Lap system โ€” Count completed laps.
โฑ๏ธ Race timer โ€” Measure completion time.
๐Ÿฅ‡ Best time โ€” Keep track of the fastest run.
๐Ÿค– Opponent cars โ€” Add computer-controlled racers.
๐Ÿšฆ Starting countdown โ€” Add a 3, 2, 1, GO! sequence.
๐Ÿ”Š Sound effects โ€” Add engine and collision sounds.
๐Ÿ More tracks โ€” Create additional layouts.
๐ŸŽš๏ธ Difficulty levels โ€” Make different levels of challenge.
๐ŸŽฎ Try the Game

If you want to see how all of these systems work together, you can play the finished project on Scratch:

๐ŸŽฎ CAR RACING THROUGH TRACKS!!!
I also created a video tutorial showing the project:

โ–ถ๏ธ Watch the YouTube Tutorial
For the more detailed step-by-step version, you can also link your Blogger tutorial and Instructables guide here once they're published.

๐Ÿ’ก Final Thoughts
This project started with a simple idea: make a car that can race around a Scratch track.

As I worked on it, I added multiple tracks, keyboard controls, speed changes, collision detection, different car costumes, and random track selection.

One thing I like about Scratch is that you don't need to build the entire game at once.

You can start with one mechanic โ€” such as making a car move โ€” and gradually build additional systems around it.

That's what made this project useful for me as a learning experience:

Start small, make it work, and then keep adding features.

๐Ÿ”— Project Links
๐ŸŽฎ Main Game: https://scratch.mit.edu/projects/1327140147/
๐Ÿงฐ Public Assets: https://scratch.mit.edu/projects/1327132133/
๐Ÿ“บ YouTube Tutorial: https://youtu.be/WFNtTTSVjKE?si=I2_TwUUFva5VP0KQ

Scratch is a project of the Scratch Foundation. This article documents an independently created Scratch project.

Top comments (0)