Scratch Saturday: Platform Games 4
Last week 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!