Skip to content

Please update your browser

Your browser version is not supported by this site. CS First may not work properly. Learn more

arrow_back Blocking (Part 2 of 2)

Choose an Add-On
play_circle_filled
It's How You Play the Game
Add a winning condition to your game.
arrow_forward
play_circle_filled
Danger Sprite
Add a sprite that causes you to lose points if you or your friend touches it.
arrow_forward
play_circle_filled
Designer
Learn how to design the sprites.
arrow_forward
play_circle_filled
Blocking (Part 1 of 2)
Force the players to jump over each other.
arrow_forward
play_circle_filled
Blocking (Part 2 of 2)
Force the players to jump over each other.
arrow_forward
play_circle_filled
Make Noise
Add sound to your project.
arrow_forward
play_circle_filled
Hello Friend
Make the sprites react when they touch each other.
arrow_forward
  • 1. Variables Are Everywhere!
  • 2. Let's Move!
  • 3. One Falling Object
  • 4. Many Falling Objects
  • 5. Keeping Score
  • 6. Shared Score
  • 7. Add-Ons
  • 8. Reflection
  • 9. Wrap-up: Work Together to Win
  • 10. Wrap-up: Share Your Project
  • 11. Wrap-up: Show Your Project
arrow_backward Back Next arrow_forward
Transcript

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.

Perfect!

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.

It works!

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.

Instructions
  1. Choose an Add-On, and click "watch" to learn how to build it.
  2. Once you finish one Add-On, try another one below the video!