BBC micro:bit – Car Racing Game

In this challenge you will recreate a fully working game using the BBC micro:bit. You can complete the code online on the BBC micro:bit website.

First check this video to see how this game will work:

Building the game step by step:


Step 1: Initialising the game
yellow-carLet’s initialise a few variables (score, gameOn) as well as our main sprite, the player’s car.

This code will then loop indefinitely while the game carries on. At the end once the game will be over it will display a game over message with the final score.
micro-bit-car-racing-step-1

Step 2: Controlling the main sprite
We are now going to add two event handlers to respond to the user interaction, when the user presses the button A or B. We will use code to move the car to the left or to the right by changing its X coordinate.
micro-bit-car-racing-step-2
Step 3: Adding the first car
The first car called car0 will be positioned at the top of the screen on lane 0. It will then move downwards (by changing the Y coordinate of the car) till it reaches the bottom of the grid. Once at the bottom of the screen we can check if it is colliding with the player’s car. If not we reposition the car at the top of the screen and give it a randomised delay.

The purpose of the randomised delay is to make sure that all 5 cars do not all come down at the same time.
micro-bit-car-racing-step-3

Step 4: Duplicating the code for each car
For this final step we can copy the code from step 3 and paste it for the other 4 cars (car1 to car4).
micro-bit-car-racing-step-4
That’s it the code is complete. You can test the game.

Extension Task


Option 1:
Tweak this code so that the game cars go faster and faster as the user’s score increases.

Option 2:
Change this game into a “Catch the fruit” game, where instead of avoiding the falling sprites the player should try to catch them and score a point each time they do so.

Did you like this challenge?

Click on a star to rate it!

Average rating 2.7 / 5. Vote count: 39

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: