Introduction: Math Racing Game in Scratch
Watch the video tutorial here: How to make Math Racing Game in Scratch | Racing game in Scratch – Kodex Academy
Math can be fun when combined with gaming! In this tutorial, originally presented by Kodex Academy and Omaansh Aggarwal in the video “How to make Math Racing Game in Scratch | Racing game in Scratch”, you’ll learn to build a Math Racing Game in Scratch. Players solve math problems to move their character forward; wrong answers benefit the opponent. It’s a race of speed, accuracy, and fun.
By following this guide, you’ll understand both the underlying code and be able to extend the game with your own ideas. Whether you are a teacher, student, or hobbyist, this is ideal for learning scratch programming, creating engaging math quiz games, and making scratch games tutorial-style with both educational and entertainment value.
What You’ll Learn
- Basic setup: sprites, backdrop, variables
- Math operation logic (addition, subtraction, multiplication, division)
- Answer checking and branching (correct/wrong responses)
- Visual feedback: sprites moving, “You Win / You Lose” messages
- Sounds and effects
- Enhancement ideas: difficulty levels, timer, leaderboards, power-ups, design polish
Math Racing Game in Scratch: Step-by-Step Guide to Build the Game
Below is a complete walkthrough with code blocks and tips.
1. Setup Sprites & Backdrops
Sprites:
- Choose two runner sprites (e.g. a dog and a cat)
- Create or import a backdrop for the racing track
- Create “You Win” and “You Lose” text sprites (or costumes / backdrops) for end-state feedback
Backdrop:
- Name your main backdrop:
Race Track
- You may also create two extra backdrops (or costumes) for end screens:
You Win
,You Lose
2. Initial Positioning & Variables
When flag clicked:
- Switch backdrop to
Race Track
- Position runners:
when green flag clicked
go to x: -199.9 y: 38 // runner1 (dog)
go to x: -183 y: 49 // runner2 (cat)
- Create two variables:
FirstNumber
SecondNumber
Make them visible on stage so the player sees the question.
3. Math Question Generation & Game Loop
Use a forever
loop to continuously generate math problems:
when green flag clicked
forever
set [FirstNumber v] to (pick random 1 to 100)
set [SecondNumber v] to (pick random 1 to 100)
ask (join (join (FirstNumber) " + ") (SecondNumber)) and wait
if <(answer) = ((FirstNumber) + (SecondNumber))> then
broadcast [CorrectAnswer v]
else
broadcast [WrongAnswer v]
end
end
- The
ask
block presents the question - Compare the user’s
answer
with the sum (or other operation depending on mode)
4. Reactions: Correct Answer vs Wrong Answer
On correct answer (CorrectAnswer
broadcast):
- Move the corresponding runner forward a set number of steps
- Play a sound
- Say “Your answer is right” for some time
- Check if the runner has reached or touched finish line color or condition → then broadcast “You Win”
On wrong answer (WrongAnswer
broadcast):
- Say “Your answer is wrong”
- Broadcast “You Lose”
Here’s an example for one runner (dog):
when I receive [CorrectAnswer v]
play sound [Back v] until done
move (60) steps
say [Your answer is right] for (2) seconds
if <touching color [red] ?> then
broadcast [YouWin v]
end
And similar logic for wrong answer or for the other sprite.
5. Win / Lose Feedback & Game End
- When receive
YouWin
:- Play a flourish sound (e.g. “Fairy Dust”)
- Switch backdrop to
You Win
- Say or show “You Win” message
- Stop all scripts
- When receive
YouLose
:- Play a sad / lose-type sound (e.g. “Meow” or similar)
- Switch backdrop to
You Lose
- Show message
- Stop all
6. Multiple Operations: Addition, Subtraction, Multiplication, Division
To increase the challenge:
- Duplicate the “generate question → check answer” logic
- Instead of always using
+
, use-
,×
,÷
Division trick: To avoid non-integers / decimals, you can set up questions so that the result is whole:
set [SecondNumber v] to (pick random 1 to 10)
set [FirstNumber v] to ((SecondNumber) * (pick random 1 to 10))
ask (join (join (FirstNumber) " ÷ ") (SecondNumber)) and wait
if <(answer) = ((FirstNumber) / (SecondNumber))> then
// correct
else
// wrong
end
Code Summary (Blocks)
Below is a high-level pseudo-Scratch style summary combining all pieces:
when green flag clicked
switch backdrop to [Race Track]
go to x: -199.9 y: 38 // Dog
go to x: -183 y: 49 // Cat
forever
choose operation from [+ - × ÷] (random or user-selectable)
if operation = ÷ then
set [SecondNumber] to (pick random 1 to 10)
set [FirstNumber] to (SecondNumber * (pick random 1 to 10))
else
set [FirstNumber] to (pick random 1 to 100)
set [SecondNumber] to (pick random 1 to 100)
end
ask (join (join (FirstNumber) " " operation) (SecondNumber)) and wait
if <answer = correctResult> then
broadcast [CorrectAnswer]
else
broadcast [WrongAnswer]
end
end
when I receive [CorrectAnswer]
play sound [Back]
move (60) steps
say [Your answer is right] for (2) seconds
if <touching color [finishLineColor]> then
broadcast [YouWin]
end
when I receive [WrongAnswer]
say [Your answer is wrong] for (2) seconds
broadcast [YouLose]
when I receive [YouWin]
play sound [Fairy Dust]
switch backdrop to [You Win]
say [You Win] for (1) second
stop [all]
when I receive [YouLose]
play sound [Meow]
switch backdrop to [You Lose]
say [You Lose] for (1) second
stop [all]
Enhancement Features (With Code Ideas)
To make your game more engaging and polished, here are enhancements you can add. Each idea includes suggested code blocks or pseudo‑Scratch setup.
Difficulty Levels
- Easy: only addition
- Medium: addition + subtraction
- Hard: all four operations
Setup: Upon start, ask user to choose level (via ask
or menu sprites). Based on choice, limit operations.
when green flag clicked
ask [Choose difficulty: 1 (Easy), 2 (Medium), 3 (Hard)] and wait
if <answer = 1> then
set [Ops v] to [ + ]
else if <answer = 2> then
set [Ops v] to [ + - ]
else
set [Ops v] to [ + - × ÷ ]
end
// then in the loop pick random from Ops
Timer / Time Limit
- Add a timer variable that counts down
- Force game over when time reaches zero
when green flag clicked
set [Timer v] to [60] // seconds
forever
wait (1) sec
change [Timer v] by (-1)
if <(Timer) = 0> then
broadcast [TimeUp]
stop [all]
end
end
Score / Points System
- Instead of just “win/lose,” keep a score of correct answers, maybe bonus for streaks
- Display score on screen
set [Score v] to [0]
when I receive [CorrectAnswer]
change [Score v] by (10)
Two‑player Mode or AI Opponent
- Player vs computer: AI automatically gets some answers right at lower/higher probabilities
- Two players: split screen or alternating turns
when broadcast [NewQuestion]
if <pick random 1 to 100 > 70> then
// AI gives wrong answer
broadcast [WrongAnswerAI]
else
broadcast [CorrectAnswerAI]
end
Animations & Visuals
- Animate runners’ sprites (costume changes as they move)
- Add finish line sprite or use color detection for finish line
- Use particle effects, sound effects to make wins/losses more satisfying
Lives / Mistakes Limit
- Give player e.g. 3 chances wrong answers before losing
set [Lives v] to [3]
when I receive [WrongAnswer]
change [Lives v] by (-1)
if <(Lives) = 0> then
broadcast [YouLose]
end
Leaderboard or High‑Score Tracking
- Use
cloud variables
if shared on Scratch website to save high scores - Or local high score variable per game session
Randomized Sprites / Themes
- Let user pick different sprite pairs (cars, animals)
- Change track background themes (desert track, forest track etc.)
Edge Cases & Common Issues in Scratch Math Racing Game
When building interactive projects like the Math Racing Game in Scratch, it’s normal to run into bugs or edge cases. Below are some realistic scenarios that Scratch users (especially beginners) may face, along with clear solutions for each.
1. Game Doesn’t Ask New Questions After One Round
Problem: After answering a single question, the game stops instead of continuing.
Cause: The question logic (using the ask
block) isn’t placed inside a forever
loop.
Solution: Wrap your question and answer-checking logic in a forever
loop so the game continues asking until someone wins.
forever
set [firstNumber v] to (pick random (1) to (100))
set [secondNumber v] to (pick random (1) to (100))
ask (join (firstNumber) (join [ + ] (secondNumber)))
// Handle answer checking
end
2. Division Questions Give Decimal or Confusing Results
Problem: Players receive questions like “27 ÷ 8”, which don’t result in whole numbers.
Cause: Random number generation doesn’t ensure that the dividend is divisible by the divisor.
Solution: Make sure to construct division questions with a clean quotient:
set [secondNumber v] to (pick random (1) to (10))
set [firstNumber v] to ((secondNumber) * (pick random (1) to (10)))
Now firstNumber ÷ secondNumber
will always give an integer.
3. Wrong Sprite Moves for Correct/Wrong Answer
Problem: Sometimes the opponent moves forward even when the player gives the right answer—or both sprites move together.
Cause: Both sprites are responding to the same broadcast.
Solution: Make sure the player sprite responds only to “CorrectAnswer” (or “Write Answer”) and the opponent only responds to “Wrong Answer”.
when I receive [CorrectAnswer]
move (60) steps
play sound [WinSound v]
when I receive [WrongAnswer]
move (60) steps
play sound [Meow v]
4. Sprite Reaches Finish Line but No Win Message Appears
Problem: The sprite crosses the finish line but the game doesn’t end or show “You Win”.
Cause: There’s no check to detect when the sprite reaches the goal.
Solution: Add a condition to check if the sprite’s x-position is greater than the finish line value.
if <(x position) > [200]> then
broadcast [YouWin]
stop [all]
end
You can also use touching color [red]
if you’re detecting a finish line by color.
5. Game Doesn’t Stop After Winning or Losing
Problem: Even after the player wins or loses, the game continues asking questions.
Cause: You forgot to stop the game or other scripts after the outcome.
Solution: Use the stop [all]
block after broadcasting the win or lose condition.
when I receive [YouWin]
show
say [🎉 You Win!] for (2) secs
play sound [Fairy Dust v]
stop [all]
6. Sound Effects Not Playing
Problem: You’ve added sound blocks, but no sound plays during the game.
Cause: The sound isn’t added to the sprite, or the block isn’t triggered properly.
Solution: Go to the Sounds tab of the sprite and upload or choose sounds like “Meow”, “Fairy Dust”, or “Pop”. Then use:
play sound [Fairy Dust v] until done
Make sure the sound is added to the same sprite where the code is running.
7. Sprites Overlap or Don’t Stay in Their Lane
Problem: Both runners are running on top of each other or look cluttered.
Cause: They are placed at the same y-position or too close to each other.
Solution: Give each sprite its own track by adjusting their Y positions slightly:
Dog: go to x: (-199) y: (38)
Cat: go to x: (-199) y: (10)
8. Non-Numeric Input Breaks the Game
Problem: User enters text (e.g., “ten” instead of “10”) or leaves the input blank, and the game doesn’t respond properly.
Cause: Scratch treats all answers as strings, which can cause logic errors in math checks.
Solution: Add a check to validate numeric input.
if <not <(answer) is a number>> then
say [Please enter a number!] for (2) secs
else
// proceed with checking the answer
end
9. Game Ends Too Fast
Problem: Game finishes in just a few questions, making it less engaging.
Cause: Sprites move too many steps per correct answer, or the finish line is too close.
Solution:
- Reduce movement per question (e.g., from 60 to 40 steps).
- Move the finish line further (e.g.,
x = 240
instead ofx = 200
). - Add a “laps” system or a counter for more gameplay.
10. Broadcasts Not Triggering Anything
Problem: You’ve added broadcasts like “YouWin” or “WrongAnswer”, but nothing happens.
Cause: Mismatch in broadcast message spelling or casing. Scratch is case-sensitive!
Solution: Always copy-paste your broadcast message name. Ensure all scripts use the same exact name:
broadcast [WrongAnswer]
when I receive [WrongAnswer]
when I receive [wronganswer]
(this won’t work)
11. Both Sprites Reach the Finish Line Simultaneously
Problem: It’s a tie! Both players finish the race at the same time.
Cause: There’s no prioritization or “locking” once a winner is declared.
Solution: As soon as one player wins, use stop all
to freeze the game. That way, the second sprite doesn’t keep moving.
12. Game Doesn’t Reset After Replay
Problem: When clicking the green flag, the sprites don’t go back to starting positions.
Cause: You haven’t added reset code at the start.
Solution: Add this at the beginning of each sprite’s script:
when green flag clicked
go to x: (-199) y: (38) // starting position
hide [You Win / You Lose text]
Also reset any variables and switch backdrop to the race track.
Conclusion: Build, Learn, and Race Your Way to Smarter Coding with Scratch!
Creating a Math Racing Game in Scratch is not just about making a fun digital experience — it’s about blending education and entertainment in a way that enhances both coding skills and mathematical thinking.
Throughout this tutorial, you’ve learned:
- ✅ How to make a racing game in Scratch with animated runners
- ✅ How to make a math quiz game in Scratch using variables and logic
- ✅ How to combine sound effects, broadcasts, and animations to build a complete interactive game
- ✅ How to troubleshoot common edge cases and bugs
- ✅ How to enhance your game with division logic, dynamic questions, and win/loss conditions
Whether you’re a student, teacher, or beginner coder, this project is an excellent example of game-based learning using Scratch programming. It’s also perfect for classrooms, coding clubs, or STEM competitions.
Call to Action
- Don’t forget to check out the full video tutorial: How to make Math Racing Game in Scratch | Racing game in Scratch – Kodex Academy
- Like, comment & share the video
- Visit kodexacademy.com
- subscribe to the Kodex Academy YouTube channel for deeper Scratch content.
Happy coding with Kodex Academy! 🚀
Learn More with Kodex Academy
At Kodex Academy, we’re passionate about helping students learn coding in creative ways. This project teaches more than Scratch—it empowers young minds to build tools that work in the real world.
Explore more:
Stay updated with new content, free tutorials, and coding challenges!
- 🌐 Website: https://kodexacademy.com
- 🌐 Website: https://games.kodexacademy.com
- 💬 WhatsApp Channel: Join Now
- 💼 LinkedIn: Kodex Academy
- 📸 Instagram: @kodex_academy
- 𝕏 Twitter: @Kodex_Academy
- 📢 Telegram: Join Our Channel
- 🔗 Patreon: patreon.com/KodexAcademy
Further Reading & Links
- Scratch Wiki Motion Blocks: https://en.scratch-wiki.info/wiki/Motion_Blocks
- Scratch Programming for Beginners: https://scratch.mit.edu/projects/editor
- Scratch Animation Guide: https://en.scratch-wiki.info/wiki/Animating