Scratch Saturday: Platform Games 2
Last week, we came up with a plan to create our Donkey Kong game using Scratch. This week 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.