This particular lesson is a great one to follow on from the last few weeks with Space Invaders (part one and two here) as we are making a very different game on the surface, but under the hood, we have very similar mechanics for the class to look at and transfer into another game.
I start this week by drawing on the whiteboard the level layout for Space Invaders, and then the 3D Car Racer game and we discuss what is the same to what we have done before. This worked really well as they could see that certain elements (The car and the spaceship) were practically identical in terms of movement, and that they could copy their code from the one to the other.
Other elements that they could reuse or remix:
- The restricted area from whack a ghost, for the traffic coming down the road
- Any of the score/highscore/timers they have used before
I start the class like last time with a premade game that is shared with the sprites and background preloaded to speed up the process (you can also use it too, I have updated it since last time). I did this by using some free sprites from an itch.io site which is linked here.
What is included is a background which includes a red bar at the bottom to enable WHEN TOUCHING COLOUR for IF statements for passing traffic along with two traffic vehicles (car and bus) and then a player car. The player car has all eight points of driving direction, to allow animation of turning left and right, but also to “spin out” when they hit traffic and crash.
This lesson, we focussed on getting the car moving left and right but maintaining within the sides of the road. This is what they copy from Space Invaders, and they pick up quickly. One thing to watch out for is the confusion between > and < in the operators, and they end up driving opposite to the button presses.
The next task is to take the traffic, and create a clone loop to generate new clones and delete them when we “drive” past them. In actuality it is the traffic that move on the vertical plane and we are static.
We borrow the idea from Whack a Ghost here. We know that the horizon line on this background is Y85. This means that we can use code that says WHEN I START AS A CLONE and use the GOTO X Y block and say Y85. This means they will always appear on the horizon. We then, want to make sure that the traffic only appears on the road, but somewhere on the horizon line. To do this, we add a RANDOM number operator to the X side of the GOTO, and say between -100 to 100. This means that from the left to the right of the horizon line, a car will spawn. test it out with a WHEN FLAG CLICKED and attached to this a FOREVER loop, with a CREATE A CLONE OF MYSELF and a WAIT command. Now on the WHEN I START AS A CLONE, add the GOTO X RANDOM and Y85 and watch as the cars fill the horizon.
To get the cars to move and go down the screen, we are going to lean on our ideas from Space Invaders. We are going to add a REPEAT UNTIL loop below the GOTO XY on the WHEN I START AS A CLONE, and this will contain a simple bit of code that says REPEAT UNTIL TOUCHING RED (where red is the colour at the bottom of the background) and inside a CHANGE Y block to determine the speed of the traffic. (you can make this change by having the changing value in a VARIABLE and have it increase over time. This is what I plan to do next week).
These pieces of code will create a loop of clones of cars appearing and moving down the screen, deleting the clone and then starting again. You as the player can dodge the cars.
Next week, we will be adding:
- What happens when you hit traffic
- A timer, and a score – where you get a point every time you pass a car
- a powerup for more time
- additional animation (turn left and right, mud flying up when you drive on the edges near the grass)
Feel free to use the template above, I have shared it to be used – let me know if you use it and share it with me on twitter!
R