Scratch Gravity and Jumping

ScratchNormal

This lessons teaches you how to make a character jump and have gravity pull them back down to the ground in Scratch.

1

Create a new Scratch project

Create a new Scratch project and delete the cat sprite.

2

Add the Dinosaur4 sprite from the library.

Change it's size to 50% in the Sprite properties and then add the following code to the dino sprite:

when green flag clicked set rotation style [left-right v] // this is important for when we make it move left and right later go to x [0] y [140] // place it at the top center at the start go to [front v] layer // make it appear over the ground sprite


3

Draw the 'ground' sprite

Use the sprite editor to draw a 'ground' sprite. Use the Rectangle tool to draw a green box and drag it so it covers the bottom half of the stage area.

Rename this sprite to 'ground' in the sprite properties.


4

Create a 'gravity' variable and make the dino fall

Create a variable called 'gravity' and then add the below code. This code makes the dino fall if it's not touching the ground.

when green flag clicked set rotation style [left-right v] go to x [0] y [140] go to [front v] layer // add the new code under here set [gravity v] to (0) forever if < not < touching (ground v) ?> > then change y by (gravity) change [gravity v] by (-1) end end


5

Make the dino jump

We're going to program the space bar key to make the dino jump but the dino should only be able to jump if it's touching the ground (you can't jump in mid-air!!).

Add the following code to the dino:

when green flag clicked forever if < < key (space v) pressed? > and < touching (ground v) ? > > then set [gravity v] to (10) // make them go up change y by (10) // move it so it's not touching the ground end end


6

Move left and right

Next we're going to program the left and right arrows on your keyboard to move the dino. Add the following code to the dino sprite:

when green flag clicked forever if < key (left arrow v) pressed? > then point in direction (-90) move (10) steps end if < key (right arrow v) pressed? > then point in direction (90) move (10) steps end end


7

Change the costume on jump

When the dino is in the air we'll change it's costume. Add the 'switch costume' block here and set it to 'dinosaur4-d':

when green flag clicked forever if < < key (space v) pressed? > and < touching (ground v) ? > > then set [gravity v] to (10) change y by (10) switch costume to (dinosaur4-d v) // add this new block here end end

And when it's back down on the ground we'll change the costume back again. Add the new 'if touching ground then' group of blocks at the bottom but still inside the 'forever' block:

when green flag clicked set rotation style [left-right v] go to x [0] y [140] go to [front v] layer set [gravity v] to (0) forever if < not < touching (ground v) ?> > then change y by (gravity) change [gravity v] by (-1) end if < touching (ground v) > then switch costume to (dinosaur4-a v) end end


End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu