This week we are going to create a top down car racing game.
My game looks like:
We can use the drawing tools to make the background road and add some grass. Maybe you might add some extra parts to your backdrop to make it more interesting?
Another example:
Notice how both roads have a red line to mark the finish? We are going to use this red line in our code later.
Our Car
Let’s draw a new car to drive around the road. I’ve used the square fill tool and created squares to make it look like a car.
When I’ve drawn the car I’ve made it face the right hand side.
If we brainstorm some of the parts of the code that we need to write, we might come up with things like:
- Make the car move
- Test if we hit the grass
- Test if we are at the finish line
- Keep track of the time the user took to drive through the track.
Make The Car Move
We want to use the arrow keys to make our car move. Let’s lay out the key press hat blocks so they match the movement we would like to code:
Our Up arrow, will just move the car forward a certain number of steps. Whereas the left and right arrow will turn the car.
We might find that our car doesn’t move straight. If this is the case then we can add the following block and attach it to the green flag hat block.
Test out the movement of our car.
We really need our car to start in the same spot each time. That will make it a little fairer when we start to time how long it takes a player to get through the maze.
Place your car at the starting point and then drag the go to x and y block under the green flag hat block.
Now every time we press the green flag button, the car will be reset.
Off Road Check
If our car goes off the road we want to reset the car back to the beginning of the course. We always want to be checking to see if the car is touching the grass, so we will be using a forever block.
We use the If block to check to see if we touch the green grass. If we find that we are touching the grass, we move the car back to the start and tell the use that its bad luck for them.
Check For The Win
Now that we can move the player back to the starting position when they hit the green grass, we need to check to see if we are touching the red finish line. Let’s add another check to the forever loop to see if we are touching the finish line colour.
Keeping Track of Time
We need to be able to see how long it takes our player to get to the red finish line. We need to add a variable to keep track of the time.
Now we need to do a few things:
- Reset the time when the game is reset
- Keep adding to the time until the car hits the red line
- Wait 1 second, add 1 to the time variable.
Now when we play the game we will find that the score will continue to increase until the player reaches the red line.
We can also make the timer a little more precise, by changing the amount that the time variable is increased. Try changing to 0.1.
High Score List
Now that we know when the player hits the red line and we also know how long it took them, we can start to create a high score list.
Lists are like variables, except they can store more than one value. We create them just the same as a variable:
The list will be shown on the screen, it’s going to get in our way as we try to drive around the screen. Let’s add a block that will hide this list until we need it later on.
Once we get to the finish point we can ask the user for their name and then add it to the high score list.
Here we ask the user for their name and then build up a string that looks like:
<user name> : <score>
We do this by using two join blocks:
This lets us add the name and score to the list all in one go.
Test your game out and see if you can get the best high score.