Scratch Saturday: Platform Games 10
We’re coming near to the end of our Donkey Kong game and now we’re going to be adding in some final touches. One of the first things we need to do is make Donkey Kong recapture the princess when Mario reaches her. In my example, I’m just going to move Donkey Kong to the princess and restart the level. I aim to make the game faster and faster as Mario goes up the levels. To do this, all I need to do is use the block <glide> when Mario touches the invisible black line.
To restart the level requires a new Broadcast called newLevel. When each sprite receives this broadcast, it resets it. Below is an example of when Mario receives the Broadbast <newlevel>
I’m going to create a variable called <Level>, which simply records what level we are at. In the Stage script area, when we click the Green Flag, it sets Level to 1. When we finish the level, we change level by 1.
The code above is set for Donkey Kong and translates as: When Mario touches off the black line, it broadcasts ShowHeart. When ShowHeart is broadcast, Donkey Kong should do the following: <Stomp>, glide towards the princess, wait 3 seconds, change the variable <level> by 1, broadcast <NewLevel> to get all sprites to reset then reset Donkey Kong to his rightful place.
Now we’re going to speed the game up. We’re going to use the variable, <level>, and we’re going to use it to control the speed of the barrels. If we check the speed of the barrels, they are moving at a rate of 10 pixels every 0.1 seconds. What I’m going to do, is make the barrels move quicker as the levels go higher by decreasing the amount of time the barrels move. I’m going to divide 0.1 by the variable <level> and see what happens.
Now that we have levels working, let’s get some scores. I’m going to give Mario points for as long as he is alive and some bonus points when he reaches the princess. I’m going to create a variable scoringOn, to give Mario 10 points for every second he is alive, (see below). Once he is dead or reaches the end of a level, we’ll make scoringOn stop. scoringOn will have two values – true or false. If it’s true, Mario starts scoring; if it’s false, Mario stops scoring. When Mario reaches the princess, he’s going to get himself 1,000 bonus points. I’ll use the Stage to initialise Mario’s score to zero when we press the Green Flag. This means we’ll need a variable called <score>, which we’ll set to zero.
When Mario touches off a barrel or Donkey Kong, startScoring is going to be set to false so we add this block into the three places where Mario can die. I’m also going to add a command that starts the scoring again, once variables are reset. Below is the addition to the code I wrote when Mario reaches the princess.
The final piece of the jigsaw is to give Mario more than one chance to die. Currently, if Mario gets hit by a barrel or by Donkey Kong, the game ends. We need to create a new variable called <lives> and initialise it to a value of 3. As before, I’ll put this in the same code where I have put the code for the stage. It should now look like this.
Now we’ll need to change some code for when Mario dies. We need to minus 1 life away when Mario dies, then start things again. See the change to the code for <dieRight> – the left is the original code and the right is the new code.
Now all that’s left to do is play the game! There a number of ways you can alter your Donkey Kong game. How about the following ideas?
- Change the scene for each level – how about changing the way things move or adding in extra platforms?
- Power Ups – things that Mario can get to speed him up, slow him down, defy gravity, etc.
- Give extra lives – if Mario gets a certain score, the variable <lives> increases.
- Ladders that appear and disappear – can hidden ladders make Mario get to the princess quicker?
- Different sized/shaped barrels – make it harder for Mario to jump over them.
You can play the game online below and if you’d like to download it to play around with, you can download all the sprites and code here. I’m going to start a new series on classic games soon and I’m going to be tackling one of the first ever shoot ‘em ups, Space Invaders. Until then, enjoy Donkey Kong and please share any ways you have improved or completely changed this game.