Professional Documents
Culture Documents
Building A Platform Game With Scratch PDF
Building A Platform Game With Scratch PDF
platform game
By Paul Koning, 2013
Content
Foreword ................................................................................................................................................. 3
What are the requirements for this tutorial? ................................................................................. 3
Why is Scratch 2.0 used for this tutorial?........................................................................................ 3
Can I use Scratch 1.4 for this tutorial? ............................................................................................ 3
Lesson 1 A first attempt to jump, fall and land .................................................................................... 4
1.1 Falling ............................................................................................................................................ 4
1.2 Moving left and right ..................................................................................................................... 5
1.3 Jumping ......................................................................................................................................... 5
1.4 A platform...................................................................................................................................... 5
Lesson 2 Gravity is cool ........................................................................................................................ 7
2.1 Break it up! .................................................................................................................................... 7
2.2 Add gravity for better results ........................................................................................................ 8
2.3 Falling with gravity ........................................................................................................................ 8
2.3 How to jump with gravity? ............................................................................................................ 9
2.4 Dont jump in the air, please! ...................................................................................................... 10
Lesson 3 One fine landing................................................................................................................... 11
3.1 Image flipping for better landings ............................................................................................... 11
3.2 Dont stop when you jump up ..................................................................................................... 14
3.3 No more hanging around ............................................................................................................ 14
3.4 A thin line .................................................................................................................................... 15
3.5 Copy that? ................................................................................................................................... 16
There are a lot of different ways to make this work in Scratch. I will show different techniques, first
they are simple and later on they become more difficult. You will see that the more difficult
techniques will make a better game. There are probably other techniques that I dont describe that
work just as well or even better. In programming theres never just one solution. I hope that you not
only copy what I describe but that you experiment and find your own solutions!
1.1 Falling
We need a player in our game. You can use any sprite for a player but for now we will use a simple
blue square of 32 by 32 pixels. You can draw this in Scratch or a simple program like Paint in
Windows.
Our player!
We want our player to fall from top to bottom. This means that we have to decrease y. Theres no
floor in our first attempt but when our player is somewhere below we want it to stop falling. You
may optionally draw a floor if you find it useful.
At first we position the player in the center of the screen. After that we decrease y by 3 until the
player is at the bottom. I use -160 and not -170 for the bottom. This ensures that we wont see only
the half of our player when the bottom is reached.
1.3 Jumping
Our player falls down and it can be moved to the left and right. Its time to jump! Jumping means
that y should increase. But as y also is permanent decreasing we have increase y by a larger number
than the we use to let it fall down.
When up arrow is pressed y is increased by 8. But because it is also decreased by 3 earlier in this loop
the result will be an increase by 5.
Try it and youll see that the player jumps. Well its more some kind of flying instead of jumping but
for now its good enough. Remember this is just the first attempt!
1.4 A platform
Next were going to add a platform. We use a square as big as our player but a different color.
What do we want to happen if the player is on the platform? The player has to stop falling so the y
should not decrease anymore. We will check for a collision to see if the player has landed on a
platform.
Ok, we are done by now. Make a few copies of the platform sprite (we will use cloning later on) and
build a simple level. You just made a very simple platform game!
Its not a very good game at this moment, to be honest: you can land in a platform instead of
jumping upon them, when you keep pressing the up arrow you fly, it looks like the player floats when
he falls, Dont worry! We are going to fix these problems in the next lessons and will get better and
better!
1. You dont have to write the same code more than one time.
2. You can make your code more readable.
We are going to rewrite our first program using the following custom blocks:
playerFall
playerMoveLeftRight
playerJump
Use the new Scratch 2.0 function Make a Block for this. When you are finished this is the result:
Try this program. It should behave just like our program in lesson 1.
gravity
vspeed
Gravity pulls the player down, thats why it is a negative number. The variable vspeed indicates the
speed of the fall. When our program starts we set the gravity to -1 and vspeed to 0.
Try the program. You will notice that player will fall very fast a bit too fast to be honest. Thats why
we are going to limit vspeed to a maximum of -7 using an if block..
Give it a try. Is our player jumping? No when we want to jump our player is either on the floor or on
a platform. So when we want to jump we set vspeed to 10 but then vspeed is immediately set to 0 in
the custom block playerFall.
To fix this problem we are going to lift up the player a bit when we start to jump. Hes leaves the
platform or the bottom and because of that the vspeed will not be set to 0 by playerFall.
At this moment our player still falls in the platforms and in the floor. Thats why we lift our player by
the amount of 4. Further on we are going to change the way our player lands and improve our jump.
When the player is on the floor or a platform the vspeed is 0. By adding the condition that the
vspeed should be 0 we can assure that the player will only jump from the floor or a platform.
Now our player wont jump in the air anymore. However if you keep the up arrow key pressed it
will still jump in the air. This is because of the fact that when our player jumps vspeed will be 0 for a
short while. This is at the moment when the jump ends and the fall will start.
We can fix this by setting the vspeed to a point number when the jump starts. Now the vspeed will
only be 0 when the player is on the floor or a platform. We want some higher jumps as well so we
will change it to 14.9.
Our player now jumps and falls in a more natural way. Experiment with the gravity, the maximum
vspeed and the amount of vspeed when a player jumps and see how it changes the feel of your
game!
Thats all for this lesson. We improved the game but there its still far from perfect. Our player can
jump from below or beside into a platform and it often lands into a platform when he fall upon it.
Thats what we are going to take care of in lesson 3!
This happens because of the fact that our player has a maximum vspeed of -7. So at the maximum
speed our player takes steps down with a size of 7 pixels each. When, for example, y has a value of
-150 the next value will be -157. And what if there is a platform at -154? Then our player will fall in
this platform for 3 pixels deep. The game simply does not notice the collision earlier.
There are several solutions for this problem. Before we can implement our solution we have to
delete all platforms except for one. From now one we will use cloning to create more than one
platform. This will prevent that we have to change more than one platform when we make changes
to it.
As we said before, the player falls down with steps of maximum 7 pixels. This means that there is a
gap of 6 pixels between those steps. We need a kind of sensor that scans below our player if there is
a platform in this gap. To make such a sensor we will add an extra costume to the sprite of our
player. We add a rectangle to the bottom of our player. This rectangle will behave as a sensor.
Duplicate the costume of the player, rename it to PlayerDummy and add a red rectangle to it at the
bottom (32x6 pixels). Its very important that you duplicate the players costume in Scratch! This
assures that the center of the costume is correct. If the center of the costume shifts the sensor will
not be at the right place.
Now what are we going to do with this dummy costume? There are four steps we will execute each
game loop:
You might wonder if you are going to see a flickering player if we use this technique. Fortunately we
dont. Scratch only shows the last costume you switch to in a game loop. But you can use other
costumes to check for collisions!
Before we write the code there is one last thing we have to do. We are not going to use the y
position anymore to check if the player has reached the bottom. That will make things to
complicated when we use the new technique. Instead of that we add a new platform called floor that
has the shape of a big black rectangle.
Stop falling.
Try the program and you will see it behaves just it did before.
Now we add the image flipping. Lets repeat what we have got to do:
Falling slow
Falling normal
When the player falls slow then maximum of vspeed will be -1. At this speed the player will always
fall exactly 1 pixel deep into a platform.
Scratch tutorial Building a platform game (2013-03-27)
Written by Paul Koning, 2013 13
Try it and youll see that our player cant jump anymore! When we try to jump the red rectangle is
always in the black part of the platform. That means that vspeed will be set to -1 and that our player
will keep falling down. We will fix this in the next part of this lesson.
Now our player can jump again. Before he lands he slows down what insures he will not fall into
platforms too deep anymore. Later on we will improve this and our player will land exactly on top of
platforms. For now a constant 1 pixel deep is good enough.
There are two problems at this time that make the player get stuck into a platform:
1. We check the complete costume of our player while we should only check his feet.
2. We use very thick platforms so there is a big surface to get stuck into.
People land on their feet. Our player should land like this too. But until now we used a full blue
square to check for collisions. We can use image flipping again to fix this.
We only want to check the feet of our player for collisions, not the head or body. So in our
costume playerDummy we chop of all of the blue except for one thin line with a thickness of about 1
or 2 pixels. Make sure its not too thin! Like I said before: make sure not to change the center of the
costume!
Scratch has no block for to compare if a number is less or equal ( <= ) to 0 but we can use < 0.01 what
in our case is more or less the same. The variable vpeed will never be 0.01 so if we compare if its
smaller then 0.01 it will always be 0 or a negative number. We change our code in:
Now the player jumps through a platform and lands upon it. However, sometimes he still gets stuck
in a platform, for example when we hit the platform sideways or when we jump just high enough to
reach a platform. Thats because our platform is too thick. There is too much surface that gives a
collision at this time.
Try the game. Its now impossible to get stuck into a platform. What if you want the platforms to look
thicker? Just add a rectangle in a different color to it like this:
Imagine that youve created an enemy sprite. It has several costumes, variables like x, y and health
and code that describes how this enemy behaves. If you want more than one enemy you could copy
the sprite and that would work fine. But what if you have to change the costumes or the code of the
enemy? Then you have to change all the enemies. Or you have to delete all enemies except for one,
change it and then copy them again. Thats a lot of work You dont have to this if you use cloning.
There is only one sprite to change. All the clones will be exactly the same. Trust me.. this will be a
giant timesaver!
Weve only got one platform and we want more. So letss clone! Our platform has only 1 costume,
no code and no variables (it actually has variables like x position and y position but we dont use
them). Instead of cloning we could use the stamp function just as well. But later on we will add code
to our platforms: we want them to scroll so variables like y position have to change. Its better to
start with cloning now and to get used to it.
We will create a random level with 10 platforms. There is 1 original platform and there are 10 clones.
Add this code to the platform sprite:
Clone positions
Try the game. Every time you start it there will be a new level.
We can create clones at runtime and we can also delete them while our program runs. We use this to
demonstrate how we can create a new random level while the game is running. What do we have to
do for that? First we delete al clones and then we create a new random level with new clones.
We will broadcast a message named destroyPlatforms to tell all clones to destroy themselves. After
the message has been broadcasted and the clones are deleted we create our new random level. See
the next page for the code.
Thats all for this lesson. This is a pretty good base to create a platform game. We need some nice
sprites, sounds, enemies and objectives but our first game engine is up and running!