Scratch Saturday: Platform Games 3
Last week we got down to some programming when we created our main character, Mario, for the creation of a Scratch version of Donkey Kong. This week 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. Next week, I’ll reveal my solution and we’ll be building the game up to rescue our princess.
-
Eimear
-
simonmlewis
-
RT @simonmlewis: Scratch Saturday: Making Games: Climbing ladders and gravity (Donkey Kong) #edchatie http://t.co/Mo6jfVSv
Thank you sooo much for this! Many thanks!
-
A pleasure! Glad you like it.














