arrow_back Blocking (Part 2 of 2)
In this video, you will program the androids to jump so they can get over each others’ heads when they duck.
First, program the blue android to jump, so it can get over its friend.
Click on the blue android.
In Scratch, jumping is the same as moving up.
Scratch sprites move on a coordinate plane.
Positive y coordinates stand for spots on the top half of the plane, so you can use them to make the sprite jump.
From the “motion” menu, drag out a “change y by” block.
Click on the code to test it.
The sprite moves up!
To make the sprite jump when the player presses the up key, go to the “events” menu, drag out a “when key pressed” block, and place it above of the “change y by” block.
But now the sprite only goes up!
To fix this,drag the android back down to the ground.
To make the android go back down automatically, drag out another “change y by” block, and connect it to the first one.
On a coordinate plane, positive y coordinates are on the top, while negative ones are on the bottom.
So, changing “y” by a negative number will make the sprite go down.
. Type -10 in this block.
Click the up arrow.
It seems like nothing happens!
The code runs so quickly, you don’t have time to see the android jump up before it goes back down.
To fix this, make the android wait to go back down until the player is no longer pressing the up arrow.
From the “control” menu, place the “wait until” block between the “change y by” blocks.
There’s a space here just like in a conditional block.
The boolean condition the block will wait for is that the player is not touching the up arrow.
From the “operators” menu, place a “not” block inside the “wait until” block.
From the “sensing” menu, place a “key pressed” block inside the “not” block.
Click on the dropdown, and select “up arrow.”
Test the code by clicking on the up arrow.
the blue android will jump.
It will wait until the up arrow key is not pressed, then it will go back down.
Next, make the blue android duck, so the red android can jump over it.
This code is the reverse of the jumping code.
Right click on the jumping code, and select duplicate.
Then, change the keys.
From the dropdown menu in the event, change “up arrow” to “down arrow.”
Then, change the value in the first “change y by” block to a negative number, and change the second to a positive number.
This new code reads “when the down arrow key is pressed, move down, wait until the down arrow key is not pressed, then move up.”
Test it by clicking the down arrow.
Next, make the yellow android also jump and duck.
This code will be similar to the code you just wrote for the blue android.
Drag the code for jumping and ducking over the yellow android.
Click on the yellow android.
Change the up arrows to “w” and the down arrows to “s.”
Test the code.
The yellow android should duck when you press the “s” key and jump when you press the “w” key.
Tinker with the numbers in your code so the androids can jump over each other.
Maybe they need to jump higher or duck lower.
Computer scientists rarely get things to work exactly right on the first try.
They use courage and persistence as they tinker to get the best results.
Now, it's your turn!
Program the sprites to jump and duck using the “change y by,” “wait until,” “not,” and “key pressed” blocks.
Tinker with your code until one sprite can jump over the other sprite if it ducks.