One prediction for 2013 I briefly referred to in my New Year’s Day article, was computer coding. I asked, would CoderDojo infiltrate schools? For those of you who haven’t heard of CoderDojo, it’s a club for young people to learn how to computer program and you can find more about it here. Over the last couple of years, a number of primary schools dipped their toes into the world of computer programming, including my own school. As far as I can tell, the vast majority of them used the programming language, Scratch.
Those of you who read this blog will know that I’ve been writing about Scratch for a good while now and have made a number of videos to teach the basics of the language. I have also written a number of maths games over the last two months, which can be played on an Interactive Whiteboard. I also gave some feedback as to how I created the programs.
Over the next number of weeks, I’m going to write a series of articles, which attempt to show you how to build a complete game from scratch, using Scratch. The idea of these articles is that they could be used in the classroom to teach Scratch to children and for them to learn the various components of a computer game. I’m also going to suggest ways that pupils could improve my ideas.
I’m going to base my first series of articles on the genre of the Platform game. There are several famous platform games out there but Super Mario is probably the most famous of all. I’m going to spend a number of weeks recreating a simple version of the very first Super Mario game, Donkey Kong, dating all the way back to 1981!
The idea of the game is that as Mario, you need to get from the bottom of the screen to the top to rescue the princess while avoiding a barrage of barrels. There were a number of levels to the game and you can check out this play-through video of the full game below.
Next week, I’ll be introducing some of the concepts of the game that we’ll be needing to learn in order to create this game. I’ll be focusing on the first level. For such a simple game, there’s a lot going on in it but there are some great programming skills to learn.
Welcome to our first lesson on how to create a platform game using Scratch. As indicated in the article last week, we’re going to be using Donkey Kong as our template. For all those waiting to dive straight into the programming, I’m afraid I have some bad news. Like every good writer, every good programmer starts with a plan and that’s where we’re going to begin.
Luckily, we don’t have to come up with an original idea for our program, because it’s already been made. We’re going to create a Scratch version of Donkey Kong’s first level, (see image below.)
As we said, while the game is quite simple, there’s a lot going on in it and that’s what we’re going to be looking at in this article. Once we have listed the different actions we need to program, we’ll start up Scratch and begin programming them in. Some of them will be really easy and others might need a bit of maths, but nothing your average 10 year old couldn’t do. In fact, if your average 10 year old can’t do it, we’ll find a way to simplify things even further where possible. OK, so what happens in Donkey Kong?
Firstly, we need Sprites. Sprites are the objects that appear on the screen so in this case, they are:
- Donkey Kong
- Burning Oil Can
- A Heart (when Mario rescues the princess)
That’s going to involve a lot of drawing! Luckily graphics weren’t that advanced in 1981 so we should make a good effort at that!
We’re also going to have to think about these sprites performing various actions. There are loads of these to consider so I’m not going to list every single one of them. Suffice to say, here is a flavour of the most important ones:
- Move left and right
- Move up and down ladders
- Jump over barrels
- Score points when jump over barrels
- Score points (and win game) when touch princess
- Die when touch barrel
- Move the barrels
- Make barrels reappear and disappear
- Make Donkey Kong die on success
- Lose lives
Next we’re going to start off with Mario and moving him around the screen. Until then, you might think about how you are going to create your Donkey Kong game. How about changing the sprites to your own characters? What’s the story of your game? Will it be the same? Will you be getting your “hero” to save a “princess” from the “villain” or will you have a twist to your tale?
We’re going to delve into the Scratch interface and create our first sprite. Before we do, I’m going to assume that you know what Scratch is and have a basic knowledge of what it looks like. I’m also going to assume you haven’t done any programming so feel free to skip over the easy bits.
The first thing we’ll do is get rid of the cat and the white background. We’ll replace the white background with a black one and we can right-click on the cat and delete him. We should be left with a black screen. To change the background, double-click on the Stage icon and edit the background, (fill it in black). We now need to create our sprite Mario (see right). I’m no artist, as you can see, but the painting program in Scratch is nice and simple and allows you to make basic images easily enough. If you want to create your own version of this game, why not make Mario into a different character. In my plan, I know that Mario does the following things:
- Walks left
- Walks right
- Goes up and down ladders
I need to create costumes for each of these moves. In order to create costumes, you’ll need to click on the Costumes tab (right) and begin adding your costumes. I’d suggest using the Copy function to make your life easier. In order to create a sense of animation, I’m creating 2 of each costume, (click on the image below for full size), to give Mario the appearance of movement. If you want to download my efforts of the 6 costumes, you can do so here.
Let’s get Mario moving. We’ll start with moving him right by pressing the right arrow.
We want the computer to constantly be awaiting the right key to be pressed so when we press the Green Flag (to start the game), we start the “Forever” block which keeps working until the game ends.
If the right arrow is pressed (sensing blocks), it will complete all five actions within the block:
- Switch to costume 1 – Mario turning right
- Move Mario 5 steps to the right
- Wait a little
- Switch to costume 2 – Mario facing right with right leg forward to appear walking
- Move another 5 steps.
I’m sure you can guess how to go about making Mario move left using costumes 3 and 4. To move left, Mario will need to move -5 steps each time. Going up and down ladders is going to be the same too but rather than using the move block (motion blocks), we’re going to change Mario’s “y position.” Pressing the up arrow will increase “y” and pressing the down arrow will decrease “y.” Here’s what the other blocks should look like.
Jumping is a little trickier to do as we’ll find out. There are two movements to a jump – moving up and moving down. We can do this easily enough by getting Mario to change his y-position up, pause, then change his y-position down and this will work, (see below) There’s plenty of other ways to do jumps so perhaps, you might be able to try some alternatives. I like using the “Glide” block (motion blocks.)
This will work well for the moment but as the game progresses we will see that it has limitations. Testing out our game so far, we have got the basic movements of our character, Mario. See the video below to see it in action.
Next time, we’re going to add in some platforms and a ladder and we’ll see why our jumping code is too restrictive and how we can stop Mario from going up and down the screen without the use of a ladder! In order to do this, we’ll be exploring some new blocks and using some logic.
We’re going to add in a couple of platforms and a ladder, (see below). Mario needs a surface to walk on and he also needs to be able to climb up and down the ladders. Last week, we allowed Mario the full benefit of walking on air and defying gravity by being able to climb up and down thin air so this week we’re going to make life more difficult for Mario.
To create the scene above, we’re going to need to create 3 new sprites (well two because we can copy the first one.) To create the platform, simply make a thin rectangular sprite than covers the full width of the screen. Call it Platform1 and then duplicate it to make Platform2. The ladder is just a series of unfilled squares and forms the other sprite, Ladder. Notice that I have used only one colour for both these sprites.
The first thing we want to do is limit Mario to only being able to climb up and down the ladder. We don’t want him to be able to climb up empty space so we’re going to have to put in a block that only allows Mario to climb the ladder when he’s actually touching it!
Our current code for going up looks like this:
We need to change one thing here. It’s not good enough to only press the “up arrow.” We also need Mario to be touching the ladder. Our code should be something along the lines of: IF (the up arrow is pressed) AND (we are touching the ladder) THEN move up. We are entering the world of logic. Programmers use logic to ensure that certain conditions are held before something happens and the terms used are AND, OR & NOT. While they are fairly easy to understand, if there are a lot of things going on, things can get complicated. To explain:
Looking at our example above, we have 2 conditions: pressing the up arrow (we’ll call this A) and touching the ladder (B). Using logic for our problem, let’s see what happens in the game.
- If A AND B then move up : This means that Mario must be touching the ladder AND the up arrow must be pressed. This is what we want to happen.
- If A OR B then move up: This means that only one of these two things need to happen. This will have the following effect. Once Mario touches the ladder, he will move up, whether you press the up key or not. That might not be so bad. However, even if you aren’t touching the ladder, pressing the up key will let Mario go up. We don’t want this.
- If NOT A then move up: This means if you are not pressing the up key, Mario will move up. You really don’t want to see that in action!
You can combine all these logic operators in complicated ways depending on the game. Here’s the updated code you’ll need.
If you run your program now, you’ll notice that you’ll only be able to move Mario up the ladder when he’s touching the ladder. You can make similar changes to the down arrow to complete this bit.
The only problem now is that if you are up the ladder and move left or right, Mario will be able to float in thin air. We need some gravity. Let’s take a look at how we can do this.
I’ve deliberately made the platform one colour and if Mario is obeying the laws of gravity, he needs a surface to land on. That surface just so happens to be this platform’s colour. What we need to do is make Mario fall downwards unless he is touching the green platform. Gravity happens forever so we’ll need to have this process working throughout the game. Here’s what I did.
For the moment there are only 2 things that will stop Mario falling to the ground – firstly, being on the ladder and secondly, being on the ground! My code uses a few bits of logic and well as the sense blocks, “touching colour” and “touching Sprite.” If Mario is NOT touching green AND Mario is NOT touching the ladder, then he needs to move down the screen. Putting this into action ensures that Mario obeys Newton’s law!
As with all solutions, often they bring more problems and now our Jumping function has problems. In our code for jumping, we got Mario to move up then down. Now we only need him to move up because gravity will do the rest. Let’s change the code (Left is old code, Right is new code)
It’s all looking good! Or is it? Things are never perfect and I’m going to leave you with a conundrum. If you move Mario up the ladder but not to the very top as shown in the image on the right, Mario will be able to “float” under the upper platform. The program allows this to happen because Mario is touching the green colour, albeit with his head. How can we stop this bug from happening? Is there something we can add to the code or is there some other way of doing it? Perhaps, we shouldn’t even be using the code I’ve suggested and maybe someone out there has a better solution.
In the last lesson, I left you with a problem. Our Mario was able to float as long as some part of his body was touching the green platform, resulting in the following problem, pictured below. We need to have some kind of way that gravity will still bring him down to earth and his head does not have magical powers to enable him to float.
You might notice in the screenshot above, that one idea I had was to change the colour of the platform slightly so there was a thin line where gravity could take its course. However, even if the top of Mario’s cap was touching the lighter green area, Mario would still float. My solution came to me as a pair of shoes. Take a look at my new Mario.
Check out his dark navy shoes! For each of Mario’s costumes, I added a line of dark blue pixels and added the following code to my program.
Now when the dark navy colour on Mario is not touching the green colour of my platform, gravity takes its course! At this stage, we now have a fully working start to the game and we now need to simply add more platforms and ladders. We’ll change the coding above where it says <touching ladder1> to touching the colour of the ladder so we can add as many ladders as we want. We’ll also make sure that only Mario’s feet are included in this so we’ll have a similar code to the platform.
Below is a screenshot of all the platforms and ladders in our game.
Now we’re ready to add our princess and let love blossom once Mario has reached her. We need to create two sprites – the princess and a love heart. The princess only has one costume as she doesn’t really move in the game. We’re going to give the heart 2 costumes – one solid heart and one broken heart. I’ve based my princess on the Donkey Kong game but you can make her be anyone. You can download mine here. You can also download my heart sprite here.
When we start the game, we don’t want the heart showing. To do this, we need to double-click on the heart to see its Scripts. It shouldn’t have any before we start. When we click on the Flag, we need it to disappear (or hide), like so on the right:
Now, we need to make it appear when Mario reaches the top of the screen to rescue the princess. For my program to recognise that Mario has rescued the princess, there are a couple of things I can do. I can make Mario touch off the princess – this will involve a similar script to touching the ladder in previous lessons. Another thing I can do is create an invisible sprite that Mario touches just to the left of where the heart should be and this is the one I’m going to show you how to do. I’m going to create a black sprite of a few pixels and place it between the top of the ladder where the heart is. When Mario touches the sprite, I’ll get the heart to appear and do some fancy stuff.
In order to place it on the screen, I’m temporarily changing the background of the game to white. I can then place my black sprite where I want it on screen as shown in the image below. I’ll change the screen back to normal once I’ve finished with a little bit of programming.
OK, so I want Mario to touch off the black sprite to show the heart. This is where we’re going to use a function called Broadcasting. When I touch the black sprite, I’m going to release a Broadcast called ShowHeart. Let’s see it in action.
So, while the game is running (forever), if Mario touches the BlackSprite, we will broadcast ShowHeart. Now we need our Heart sprite to wait for this broadcast and when it received it, it should show up on screen. To do this, we need to double-click on the heart and find the “When I receive” block. Here is the simple block we need.
Now, once we change the Stage background back to black, we’re ready to go. Next week, I’m going to start off by making the heart do more complex things other than simply showing and hiding. I want the heart to pound and go bright then break as the princess is towed away by Donkey Kong. I’ll be using lots of the “looks” blocks so have a look at them and see what you can do with them!
Five lessons into our game, we have set up a number of items in our game. We have created our main character, Mario who can now move around the screen properly. We also have a number of platforms and ladders for him to do this. Last time we created the princess and a love heart for when Mario reaches her. I thought we’d take a closer look at the heart and have a look at some of the lesser used functions in Scratch to make the occasion more romantic.
When the heart comes on screen, it is because of a Broadcast command, which simply shows the heart. I’d like to make the heart pound a little bit. To break this down into programming-speak, I’d like to make the heart grow bigger then smaller a few times. I’m going to need to use a loop to make something repeat.
First of all, I want to make the heart grow so I’m going to make a bit of an animation. I’m going to make the heart grow by 40 pixels but in ten steps of 4. This will give it the appearance of growing. It will also need to decrease back to its normal size so I’ll decrease it by 40 pixels in ten steps of 4, (see right).
Now I need to make the heart beat a few times – let’s say 5 – for the moment. I need to wrap the increasing and decreasing blocks up in another repeat block – repeat 5. This will make the heart beat 5 times.
Ok, so that was easy enough. Now I’m going to add a splash of extra colour to the heart. I’m going to use the block: <Change Colour Effect by x>. This block changes the colour of your sprite to a different colour. Scratch allows 200 different colour effects so you can have a lot of fun here. In my example, I’m having the colour effect change by 10, for every increase and for every decrease. The effect of this is the heart will go through a spectrum of colour changes and end up back to red.
Right, so we’ve done a few things to the heart at this stage and I’m going to play around a little bit more with the heart, just for the fun of it and so you can see some of the results you can have with it. I’m going to play around with the mosaic effect, which, as its name suggests turns your sprite into a mosaic version of itself. I made the mosaic change by 3 then -3 to show lots of love hearts! You can play around with some of the other effects like fish eye lens, whirl and pixelate. The ghost effect is worth mentioning because it makes your sprite fade. If you find your sprite has gone crazy, the block, <clear graphic effects>, will get your sprite back to normal!
In Donkey Kong, unfortunately, life isn’t love hearts for long as our gorilla grabs the princess at the end leaving poor old Mario heartbroken. The love heart sprite has a second costume, which is a broken heart. After the theatrics of the love hearts throbbing and going through its graphic effects, it’s going to fade to nothing and hide.
We need to add more code now and I’m going to change the costume to a broken heart then use the ghost effect mentioned before to slowly make it fade. It’s easy to change the costume. Simply create a block underneath the code to change to the next costume. Once that’s out of the way, we’re going to slowly make the heart fade over 2 seconds. A fully faded sprite has a change in effect of 100 so we need to fade our heart at a rate of 50 per second. I’m going one step further by fading the heart by 5 every tenth of a second. This means I have 20 repeats of a ghost effect of 5, (20 x 5 = 100). I also want the computer to wait a tenth of a second each time (1 tenth of a second x 20 = 2 seconds). Next, I want to change it back to the previous costume, (no-one will see it), then hide it completely, (in case!) You might think that that’s the job done but have a think about what will happen the next time we run the code.
As the sprite has been set to fade completely, when we run it again in future levels, if we don’t change the code, there will be nothing to see so we’ll need to add one extra block at the beginning – <clear graphic effects>. Pop this after the <show> block and you’re done!
Next time, Donkey Kong will enter the arena. He’s going to make life hard for Mario! Have a go of messing around more with the graphic effects but also try moving the heart around the place or even get Mario and the princess to have a smooch!
Enter Kong! We’re introducing a new sprite to the game – the one who gives his name to the famous series – Donkey Kong! You can download the sprite here. I designed my Donkey Kong Sprite based on the original sprite from the game and attempted to copy how he looked. One thing I noticed in the game from the animation was one of Donkey Kong’s feet was slightly raised higher than the other. When I was designing his second costume, I copied the first one and then mirrored it. When you flip between the two costumes it makes Donkey Kong look like he’s stomping.
I’m going to make a piece of code to do this. Whenever Donkey Kong is about to throw a barrel, he’s going to stomp for a couple of seconds in a temper. If you remember our code for our love heart last week, this is very similar. Donkey Kong is going to stomp 10 times in 2 seconds. The code is very easy. It’s a simple repetition of moving to his next costume. We’ll leave this code to the side for the time being.
OK, that’s Donkey Kong introduced to the game. Before we get him to start throwing barrels, there’s a couple of things I’m going to do. Firstly, I’m going to create another sprite, which is a barrel of burning oil that will stand at the bottom platform. Not only will this be to be in line with what the game looks like, it will also serve a couple of functions, which we’ll see later. For now, it’s just going to burn! I’m going to show you how I made it using the image editor.
Now we need to make a copy of the bin and gradually add fire to the sprite. I’m just going to the paintbrush tool to do it. I’m going to make about 5 images building the fire up. I’m then going to copy the images to make the fire go down. Next, I’m going to create a function that will make the fire burn all through the game. The function is exactly the same as the one above except instead of the block <repeat 10>, I’m using <forever>, because the fire is going to burn no matter what, (unless, of course, I decide to stop it at any time.)
Until now, this game has no end as Mario is immortal. We’re going to need to find some ways to make him die. Since we haven’t got any barrels yet, the only other way he can die is if Donkey Kong touches him. I’m also going to set you a challenge. The code for Mario dying when he touches Donkey Kong is easy to set up and we’ll also be able to use a new block.
In this example, the whole game is going to end if Mario touches Donkey Kong. We’ll be changing this later as we’ll be adding in scores and lives but for the moment the game is simply going to end (see left). If you run the program like this, if Mario touches Kong, the game just stops and it may confuse people. We could add a sound like a screech, (we’ll be adding sound later), but what about people who don’t have sound or want to play the game without sound? We’re going to have to get Mario to look like he’s dying. I’m going to get him to bounce backwards and lie flat on the ground. It’s useful to note that Mario will only approach Donkey Kong from the left so keep this in mind.
To get Mario to bounce backwards, I’m going to have to get him to move up and left then down and left. Once he’s hit the ground, I’m going to rotate him into a “dead” position. Let’s see the code in action.
First of all I want to make Mario rotate anti-clockwise by 90 degrees gradually. I’m going to do this by rotating 4.5 degrees, 20 times (4.5 x 20 = 90). I’m then going to make Mario move left by 2 pixels then up by 4 then left again by 3. This will make Mario move a total of 100 pixels left and 80 pixels upwards. Once that ordeal is over, Mario should find himself lying down mid air. We now exit the loop and stop the program. However, before the program ends, we have to wait a few seconds to give gravity a chance to do its thing and let Mario fall to the ground. I’ve given 2 seconds. The final block is to stop everything.
Later, we’ll be getting rid of this final block as we’ll be adding extra lives to Mario. Until then, my challenge to you is to make a more elaborate death for Mario. Why not use some of the image effects from the previous lesson – pixelating will be a good one! In the next lesson, we’re going to introduce the other thing that can kill Mario – the barrels that Donkey Kong rolls down the screen.
Last lesson we created Donkey Kong and gave Mario his first opportunity to die in the game. This time we’re going to introduce our barrels, which Donkey Kong is going to throw every so often. To keep the game as simple as possible, the barrels are going to roll down the screen, hit the fire barrel, disappear, then start again. The first thing we need to do is create the barrel. I’m just going to make a brownish circle but you can make it more realistic than that.
When the game begins, the barrel needs to be hidden. The first barrel I’m going to make is going to be thrown directly onto the lowest platform. Otherwise the game will be too easy for Mario to go up the screen. I’ll need Donkey Kong to stomp first so I’ll send out a broadcast called stomp. When Donkey Kong receives this message, he’s going to stomp. If you remember our code that we put aside last week, we’re going to put this in action now. (Make Donkey Kong change his costume 10 times)
Now we need to plan the barrel’s further movements – essentially to get to the barrel. The code below is explained but you might find a better solution to make this barrel work.
When the flag is clicked, we need the barrel to be in Donkey Kong’s hand then we need to make it disappear. Next we need Donkey Kong to stomp (as outlined above) and then we need to show the barrel and send it to the bottom platform in position 172, -155. I’m using the block <glide> as it makes it smoother and I’m only giving it half a second to speed things up.
Once it lands on this co-ordinate, I’ve calculated that it is 410 pixels away from the barrel so I get it to move 10 pixels to the left every tenth of a second and I repeat this 41 times.
My plan now is to make it disappear when it touches the barrel (see if you can remember how to do this) and then let this action happen every so many seconds.
I also want to get Mario to die if the barrel touches him. I’m going to recycle some code I used in the last lesson to do this.
Last week, I set up some code to make Mario die when touching Donkey Kong. I’m going to create a new Broadcast function called “die-left” and drag the code there. “Die-left” will make Mario die to the left – as in jump left and fall over. “Die-right” is something you can try and make for a situation where you want Mario to die when hit by something coming from the left. This will be dependent on what direction the barrel is traveling so I’m going to have to create a variable that states what direction Mario must fall. Lets have a look at the code that we need for all this to happen.
Eek! That’s a lot of code. Let’s break it down. First thing, I’ve taken the code from last week from touching Donkey Kong and moved it to a function (or broadcast) called “die-left.” Whenever, Mario receives the broadcast, <die-left>, Mario will follow this load of code. I’ve also created the opposite broadcast, <die-right>, which makes Mario leap to his death in the opposite direction. How do I know which direction Mario must die, well it’s going to depend on the direction that the obstacle is coming from. In our game, a barrel could be coming from the left or the right so I’ve created a variable called barrelDirection and it can be given the value “left” or “right.” Each time the barrel moves, this variable is updated accordingly so Mario knows which way to die if he’s hit!
At this stage, if we run the program, we have the bones of a decent game here. The only problem I have is that when Mario does get hit by the barrel, he continues getting hit so we need some way to stop this. My choice is to make Mario invincible for this time but there may be other ways. It’s also going to make me need another variable called “invincible.”
Now, when Mario is hit, he only gets killed once!
The final piece of the puzzle for this lesson is to make the barrel disappear when it reaches the fire then get it to start again. This needs a few of steps:
- make the barrel disappear when it touches the fire – <hide>
- wait a while
- get Donkey Kong to stomp
- send the barrel again
I have again taken the code from the initial Donkey Kong throw and turned it into a broadcast (or function) that is called five seconds after the barrel hits the fire.
All I need to do now is add more barrels to the game and we’re almost there so why not give that a go! Before you do that, I wonder if you’ve noticed that every time you play the game, sometimes things don’t happen. For example, sometimes Mario is still invincible when you start a game again. In the next lesson we’re going to tackle a programming function called initialising variables. In best practice, one is supposed to do this at the very start of any game. We’re being naughty and waiting until lesson 8 so I’ll see you there!
We’re taking a pause in this lesson to talk about variables and initialising them at the start of a game. As you probably have figured out at this stage, when you press the Green Flag on any Scratch game, the game begins. Before a game begins, we need to set everything up. For example, we need Mario to be standing in the start position and we need no barrels on the screen. So far, in our game, Mario could be in a variety of situations:
- He could be lying on the ground and we need him upstanding
- He could be anywhere on the screen and we need him by the fire
- He could be invincible and we need him to be mortal again
- He could be facing upwards and we need him facing to the right
All this can be done with the following block of code. Notice one thing though. I have decided to allow Mario be invincible for the first couple of seconds of the game in case something hits him while he’s getting back to his starting place, although this should be unlikely.
At this stage, we’ll just need to make the blackLine sprite be visible on the screen and we’ll make sure that the princess and Donkey Kong are both in their correct positions. We’ll use the same block for this <When flag clicked> followed by the appropriate code.
As the game gets more complicated, that is, when we start adding code for scoring and lives lost, this will become even more important so it’s good to start simple. Think about other aspects of your game that you’re working on to see if there’s anything else you need to initialise. In the next lesson, we’ll continue building barrels and trying to kill of Mario.
Last time we took a bit of a pause to create variables and this week we’re going to take a closer step to completing our Donkey Kong game by adding the rest of the barrels. In lesson 7, we created our first and second barrel. I’m going to add three more barrels to the game to make it harder. I’m also going to make a couple of further changes to the code to stop the first and second barrel from coming back on screen as there is too much potential for barrels to be too close together when thrown by Donkey Kong.
Let’s make 3 new barrels by duplicating any of our barrels and we’ll give it the following code:
This code is for Barrel3, which moves from Donkey Kong all the way down to the bottom of the screen. When it hits the fire, it starts again in one second. We are going to duplicate this for Barrel 4 and Barrel 5 so it has almost the same code. We’ll just need to create a couple of new Broadcasts for each barrel.
I’m now going to change the code for Barrel 1 and 2. In Barrel 1, I had the first barrel dropping every 10 seconds or so. This sometimes made barrels be too close together on screen, making it impossible for Mario to avoid them and therefore making the game impossible. I’ve changed the code so that when the first barrel hits the fire, Donkey Kong is broadcast to throw Barrel 3. I’ve done something similar with Barrel 2, except, I’ve asked it to broadcase Throw Barrel 4, wait 5 seconds then throw barrel 5.
I now have a fairly complete game at this stage. If I click on the Green Flag to play I should get something like the video below.
Next time, we’re going to add in scoring, lives and some other bits and pieces to enhance the game play.
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 Broadcast <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.
Last Update: August 8, 2017