🏠
Section 1.

Coding Gravity

We are going to add code to the game so that the sprite falls downwards when it isn't standing on anything.

Declare a variable called "GRAVITY".
Check 'for all sprites', because everything falls with the same acceleration.
Do you remember that variables like this are called 'Global' variables?
Coders often use all capital letters for global variables.

In our setup block set GRAVITY to -1.5.
It is a negative number because things fall downwards!

Now is a good time to decide where you want the sprite to be when the game starts. We need it to be quite high up on the stage so that we can test the 'falling down' code we are writing.

Drag your sprite there and look at its position.
Then use a go to x: ... y: ... block in your setup block.

Section 2.

We also need to keep track of your sprite's speed up and down the stage.
This direction is called the sprite's "y position".
Create a variable called "speed y".
Check 'for this sprite only'.
Use lower case letters because it is a private variable.

In the setup block we now set speed y to 0.
'speed y' will be a positive number if the sprite is going up the stage.
It will be a negative number when the sprite is going down the stage.
When it is 0 the sprite won't go up or down, although it might go sideways!

Section 3.

We are going to create a new block which will move the sprite down the stage.

Create a block called 'move down', with input 'steps'.
Check the 'Run without screen refresh' box.

The code in this block is very similar to the 'move left' block.
Except that we are changing the sprite's y position.
And setting 'speed y' to zero when we bump into an obstacle.

This code calls itself: we are using recursion again.

Section 4.

To use all our new variables and code we need to do three things:

  1. At the very bottom of our main loop we put:
    change speed y by GRAVITY
    This is because, whatever else happens, gravity will always be pulling the sprite down the stage.

  2. We also use our new 'move down' block.
    We want to call this block whenever 'speed y' is negative.
    So we put it inside an 'if' block.

  3. The input to the 'move down' block is the number of steps we want the sprite to take. This is always a positve number. But speed y is negative. To convert it to the right positive number we use the '[abs] of ...' block.

What the 'abs' block does:

The 'abs' block changes a negative number to a positive one.
Examples: it would change -3 to +3, -8 would change to +8.

If you test your game now, your sprite should fall down the screen.
It should stop when it gets to the bottom.

If everything is working, you can go on to the next section.
If not, find out what is wrong and fix it!
This is our code -> test -> debug cycle!

Section 5.

Jumping up

Next we will code a block to move our sprite up.
This is very similar to the move down block.
The only differences are:

Section 6.

Compare the four 'move' blocks we have written.
Look how similar they are!
It is very useful to notice things like this.
You will learn to see patterns in the code.
Good patterns can be used over and over again.

Section 7.

Jumping

We want to make the sprite jump when we press the up arrow key.
It should only be able to jump when it is standing on something.
You can't jump if you are in mid-air!

We need a new variable to say how fast you can jump upwards.
Let's call it 'jump speed'. Check 'for this sprite only'.
In the setup block set it to 12.

We need a new block.
Call the block 'jump'.

Now we can use the 'if key up arrow key pressed' control block.
Then, test if we are standing on something,
jumping is allowed if we are,
not allowed if we aren't.

So, how is jumping made possible?
Do you see how we set speed y to a positive number?
This means that, for a short time, the sprite will go up the stage.
Except that GRAVITY will keep subtracting from speed y...
until speed y is negative and the sprite falls down again.

This is exactly what happens when you jump!

Section 8.

Of course, none of the jumping code will run unless we use our new block.
So we add some more code to the main loop.
We add code for when 'speed y' is greater than 0.
We add a call to the 'jump' block.

Section 9.

We have got to a really good place in writing a platform game.
You can have some fun adding more plaforms to your 'platform 1' sprite.
Keep trying the game out to see if everything works properly.

The square sprite should move left and right when you use the arrow keys.
It should fall down the stage when it isn't on a platform.
It should jump up when you use the up arrow key.

You can use the up arrow key and the left arrow key at the same time.
This should jump to the left.

You can use the up arrow key and the right arrow key at the same time.
This should jump to the right.

Make sure you have saved all your work!

Save to the cloud....

then save to the local drive too.

🏠