Blog Post

Making Donkey Kong in Scratch

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 first ever Donkey Kong game (Image from Nintendo)

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.

Lesson 1

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.)

Image from Nintendo

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

    Donkey Kong

  • Princess
  • Mario
  • Burning Oil Can
  • Barrels
  • Ladders
  • Platforms
  • Scores
  • 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?

Lesson 2

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
  • Jumps

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.

Let’s go through each block.

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.

Lesson 3

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.

Lesson 4

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!

Lesson 5

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!

Lesson 6

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.

Our Donkey Kong Sprite

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.

Start with a rectangle.

Add an ellipse on the top to create the top of the bin and do a bit of artwork.

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.

Lesson 7

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
  • repeat

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!

Lesson 8

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.

Lesson 9

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.

Lesson 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 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  

Total 0 Votes:

How can we improve this post?

+ = Verify Human or Spambot ?

Ask Us A Question

You will get a notification email when Knowledgebase answerd/updated!

+ = Verify Human or Spambot ?

Comments (7)

  1. Peter Lydon 5th January 2013 at 6:27 pm Reply

    Not quite the same thing but I started a Code Club in my school last October. We haven’t looked at scratch formally but some kids are working on a variety of programming languages including Python, Ruby and Markup languages (HTML, CSS etc). One student is trying out AppInventor for Android. I recently acquired a RaspberryPi so this presents some interesting opportunities for the kids also. looking forward to your series.

    1. simonmlewis 5th January 2013 at 7:57 pm Reply

      Thanks for the comments, Peter. I recently tweeted something on Raspberry pi in education so might be helpful. Great to hear about those languages at second level

  2. IT Support 10th January 2013 at 8:57 pm Reply

    I’ve actually miss this segment of yours and I am looking forward learning new scratch programs through your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Minister Bruton launches 2018 Digital Plan for Schools

Somehow, I missed this announcement last week even so it contains 80 actions and

Digital Schools of Distinction Booster Programme – Diary Entry 2

Scoil Cliodhna CNS – Our Digital Journey As it Happens – Part 2 Our teachers

Digital Schools of Distinction Booster Programme – Diary Entry 1

Scoil Cliodhna CNS – Our Digital Journey As it Happens – Part 1 Our school

ICT in Education conference 2018

From the conference organisers: The Programme for the ICT in Education conference is now available.  The conference will

Digital Schools of Distinction Booster Programme

The following is a series of guest posts from a school involved in the

Final Thoughts from INTO Presidential Candidates

Gregor Kerr Members have a choice. They can vote for more of the same, for

Do the candidates have opinion on other initiatives?

Apart from the few government initiatives mentioned in the last few days, what do

On coding in schools

Gregor Kerr How many times has the Minister announced Coding as his big new idea?

On the New Primary Curriculum

John Boyle Our 1999 Curriculum is now one of the oldest in Europe. I welcome

On Cosán: INTO Presidential Interview

Gregor Kerr As a member of the Teaching Council I will be watching this one

On Droichead

Readers of will know of my own stance on Droichead, which I’ve been

On JobBridge

Gregor Kerr I opposed its introduction. I supported NQTs when they looked for a directive

On Aistear

We ask the candidates some short questions on various initiatives brought in by government

On Diversity – John Boyle

Last Update: August 8, 2017   Share :  

Section 37(1) – Gregor Kerr

Last Update: August 8, 2017   Share :  

Section 37(1) – John Boyle

Last Update: August 8, 2017   Share :  

Which cut do you think will be the hardest to reverse?

There were so many cuts made to our profession in the last decade, it’s

Is the INTO too hierarchal?

Last Update: August 8, 2017   Share :  

Is the union too hierarchal?

One criticism I have of the union is that it is far too hierarchal.

If the DES allowed you to reverse one cut they imposed, what would it be?

Over the last decade, the government made several cuts to the education budget. Very

Gregor Kerr on Pay Restoration

Last Update: August 8, 2017   Share :  

John Boyle on Pay Restoration

Last Update: August 8, 2017   Share :  

What do you think is the INTO’s biggest challenge in the next few years?

In our series of questions to the INTO President candidates, we wonder what the biggest

How will you attract teachers to engage with the union?

People often complain that fewer teachers are attending union meetings and engaging with the

INTO Influence – John Boyle

Last Update: August 8, 2017   Share :  

Influence of the INTO – Gregor Kerr

Last Update: August 8, 2017   Share :  

Do you think the INTO is as influential as it always has been?

We asked both candidates for the INTO President position some questions to see where they

Gregor Kerr – Question 1

Last Update: August 8, 2017   Share :  

John Boyle – Question 1

Last Update: August 8, 2017   Share :  

Why do you want to be president?

Last Update: August 8, 2017   Share :  

INTO Presidential Election: Interviews

For the first time in many years, the INTO will be having an election

What you might not know about the ETB Community National School model

With the news today that Richard Bruton wants the ETB to lead an “identification

Who will succeed Tomás O'Ruairc on the Teaching Council?

It’s hard to believe that 5 years have passed since Tomás Ó Ruairc has been Predictions for 2017

Every year, I try to predict what the big stories will be in primary

Safe Christmas Playlist

Christmas is impossible to ignore no matter what type of school you teach in.

What next for ERBE?

As part of the government’s Pluralism and Patronage Forum, which is supposed to ensure the

Diversity in Primary Teaching

The Teaching Council hold a conference every year called Féilte. One of the topics

Empathy and ClassDojo

ClassDojo are currently running a fantastic series on empathy and it got me thinking about

Teddy Bears’ Picnic Playlist

Are you having a Teddy Bears’ Picnic in your school? Here’s a little playlist

Roald Dahl Playlist

“Sometimes you have to be a little bit naughty.” So says the song in

Drum Solos Playlist

This playlist focus on drums, specifically, drum solos or famous beats. It’s amazing how we

Happy Playlist

Are you looking for songs about happiness? These songs are a selection from the

2010 to Now Playlist

Here are some songs from 2010 to today that are appropriate for primary school

Playground Playlist

If you want to play some tunes in the Playground to liven up break

Different points to enter Teacher Training College

RTE revealed that DCU’s newly formed Teacher Training course, which amalgamated 3 previous colleges of

Twitter and Droichead

There was a lot of talk about Droichead on social media over the last

Education Equality: Gathering for Change March

I was delighted and honoured to have had the opportunity to speak at the

What is the biggest issue for Community National Schools?

The Community National School model is being lauded as the ideal solution for primary education

What Vehicle will get us over the Bridge?

With the overwhelming majority of teachers voting to not cooperate with Droichead in its

Droichead: The People have Spoken

Yesterday, the INTO revealed the results of the ballot regarding non-cooperation with the Droichead

What’s the difference between the CNS model and the Catholic model?

Another government, another disappointing day for education. Jan O’Sullivan has left her mark and I

Are the Teaching Council acting ethically?

Right now, teachers are being asked to vote in a ballot as to whether

Why Ticking “No Religion” Shouldn’t Matter in Schools

For people without a religion, the census always draws up conversation. Mostly, it’s to do

10 Suggestions for the Teaching Council re: Droichead

I have been giving out about Droichead for quite some time so thought it might

What do Droichead, Irish Water and the Junior Cert Reform have in common?

In my opinion, the answer to the above question is the same. I know

ClassDojo Cartoons

I discovered something new on the ClassDojo website today – a whole bunch of

Another Alternative to Droichead

If you ask any principal in Ireland what the biggest threat to our education

Droichead: The Film Script

The story of Droichead has been worthy of a film. It’s got a cast

#dojoChatEU: Learning from mistakes in the classroom

Every week, I delve through ClassDojo’s Twitter chat and answer the questions that were

Were the Teaching Council Elections truly Democratic?

The Teaching Council elections took place last week and I was happy to see that

Teaching Council Election Results

Because the Teaching Council elections were done online, results were out very quickly. There

#dojoChatEU: Fostering Positive Relationships

Every week I answer the questions posed in ClassDojo’s weekly chat, #dojoChatEU. It happens every

How to make denominational schools more inclusive

Around 96% of Irish primary schools are under the patronage of a religious body,

#dojochatEU: Peer learning

Every week I take the questions from ClassDojo’s weekly chat on Twitter, #dojochatEU. This

Primary Schools: Inclusive or Tolerant?

As most people know, there are a number of parents in Ireland who have

Infographic of Survey on Inclusiveness

I developed an infographic based on the data received from my survey on how

#dojochatEU: Has technology a place in education?

Every week I respond to the ClassDojo #dojochatEU Twitter conversation, which happens every Wednesday

ClassDojo and Principals

ClassDojo have just released a way for principals and other school leaders to get

How inclusive are Catholic Schools?

Recently, The Irish Independent carried an article with the headline, Catholic Schools are as inclusive

#GE16 and Primary Education: Religion in Schools

As we come up to the General Election, I’m looking at the various parties

Is it ethical to publish League Tables?

The Irish Independent published, what they called, a definitive league table of the best schools

#DojoChatEU: New Year’s Resolutions

Every week, ClassDojo run a Twitter chat on a Wednesday evening. After a nice winter

The Mathematics of Patronage and Pluralism

Between 96% and 98% of Irish primary schools are denominational in patronage, which means

ClassDojo Comes to Ireland

For all you ClassDojo users out there, the people behind ClassDojo are coming to

2016 Predictions

As we come to the last day of year, it is traditional at to make predictions

What will removing Rule 68 actually do?

One of the rules of National Schools that has been causing much debate over

#dojoChatEU – Reimagine Learning

This week’s #dojoChatEU was titled Reimagine Learning, the theme of BETT 2016. The chat organised

Multidenominational: A long word, long-abused

The education landscape used to be an easy one to maneuver. Between the 1920s

#DojoChatEU – Collaborative Learning

Every week, I write a post based on ClassDojo’s regular Twitter chat on Wednesday

Can the new NCCA Ethics Curriculum work in Ireland?

It always strikes me as odd the stories about primary education that get noticed

#dojoChatEU: Breaking down the classroom wall

Every week I look at the ClassDojo chat and answer the various questions on

Why Pluralism in Education Doesn’t Work

I was listening to a debate on the radio about the Angelus last week.

#dojoChatEU – Global Issues and Topical Events

Every week I summarise the #classDojoEU conversation on Twitter with my own thoughts on the

#dojochatEU: Growth Mindset

Every week, I try to tune in to ClassDojo’s weekly chat on Twitter. I

Thoughts on the Digital Strategy

The Digital Strategy for Schools 2015-2020 is the latest attempt by the government to

#ClassDojoEU: Resilience

This week was the first time in ages that I was able to take

#dojoChatEU: Future-Proofing our Learners

This week’s ClassDojo chat was called Future Proofing our Learners. As usual, I’ll be

ClassDojo Chat: Work/Life Balance

Every week, ClassDojo have a chat on Twitter under the hashtag #dojochateu. The chat

Should we be Celebrating the 1916 Rising?

Earlier this week, two army officials came to my school to present us with

An Alternative to Droichead

Following my blog post on Droichead last week, I thought I might expand on my

Droichead: A bridge too far

Droichead is a new model of induction and probation for newly qualified teachers, which

5 Ways to use ClassDojo’s ClassStory

ClassDojo, everyone’s favourite behaviour tracker, has released a new feature for the start of

Burren National School: A symbol of the divestment process

Today, Educate Together announced that the proposed new Educate Together school in Castlebar was

Where do unbaptised children fit in?

I’ve been following the recent discussions about unbaptised children accessing primary education over the last

ClassDojo releases ClassStory

As many teachers know, parents can log into their child’s ClassDojo account and see

Groups Come to ClassDojo

ClassDojo are releasing a number of updates over the summer. For teachers in Ireland,

#DojoChatEU Transition Days

This week’s DojoChatEU was called Transition Days and focused on moving from one class

Who are the role models in primary schools?

Last weekend, I was part of a panel at the Excited Festival of Learning

Why We’re Voting Yes to Marriage Equality

Commenting on referenda is not something that we generally do at but the

#DojoChatEU – Technology in the Classroom

I managed to attend a second #DojoChatEU in a row last week, moderated by

#DojochatEU – Developing a PLN

#DojochatEU is a Twitter hashtag for an event that happens every Wednesday from 8:30pm

Should schools be allowed to discriminate?

The headline grabber from the INTO congress was Jan O’Sullivan’s announcement that new enrollment

A ClassDojo Shop

Lots of classrooms are using ClassDojo as a behaviour management tool and it’s great

ClassDojo Ambassador

I have been aware of ClassDojo for a number of years and nominated the

Denominational Schools and the Upcoming Marriage Referendum

The upcoming referendum on marriage equality has sparked some conversation among teachers who work

Why we don’t need to start teaching coding in primary schools

Ciaran Cannon is one of the most innovative politicians I know and he is

Primary Schools and Broadband: The biggest issue in education today?

Following on from a conversation on the wonderful CESI list, I thought I would

What books would you recommend for a primary school teacher?

Facebook is awash with challenges from ice-buckets to listing how you’re grateful for the

A short guide to religion in primary schools

Over the last number of years, the population of Ireland has changed. One of the

10 Tips for getting Substitute Work

I received an email today asking for some advice on getting substitute work in

Why we should care about Ireland’s Human Rights Record in Education

Last week, the Irish government was given a major slap on the wrist for

Fitness to teach is all about motivating teachers to love their job-Part 2

Self-actualisation and flow In my first post on performance-related pay, I spoke about the process

Fitness to teach is all about motivating teachers to love their job-Part 1

Managing the performance When the Teaching Council brought up the Fitness to Teach legislation in the most

Cutting the cost of school uniforms? Why not cut them altogether?

Irish parents are to be balloted on school uniforms in the coming weeks in

How to save the Irish language

One of the most popular conversations I have with non-teachers is about the Irish

Everything I know about Féilte

Féilte is a festival organised by the Teaching Council for World Teachers’ Day on

What did the Teaching Council ever do for us?

One of the questions that is often asked amongst teachers is what do the

How not to get a job interview

Teaching jobs are getting more and more scarce every year in Ireland. The days

Do we need Teaching Council 2.0?

It hasn’t been the best couple of months for the Teaching Council. Established six

It's oh so quiet..New draft document on Inspectorate handing probation to Principals

Email I got an email today from the Teaching Council and I almost deleted it

5 Tips for Teacher Job Applications

The next month is going to be one of the toughest for any teacher

Douglas Adams, The Fourth R and Learning

I was happy to see that technology was on the agenda at the annual

12 reasons to have Computerised Roll Books

Imagine it’s 11:30am on June 30th.  What are you doing?  Apart from trying to

What have the Teaching Council ever done for us?

Have recently discovered Inside Education on 103.2 Dublin City FM. Simon introduced me to

Why Blogging is Best – a digiteach article

I was asked to write a follow-up article about my workshop at this year’s

Ask Us A Question

You will get a notification email when Knowledgebase answerd/updated!

+ = Verify Human or Spambot ?