Skip to content

Please update your browser

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

Transcript

In this add-on, program one or more sprites to move when keys on the keyboard are pressed.

This example programs a sprite to move left and right and to jump, but program any movements that you’d like. To start, select a sprite to control with the keyboard. Next, program the sprite to move. Click motion, and drag out a “change x by” block. In Scratch, x represents the left and right values on the stage. Click to run it. Great, the sprite moves right. Drag out another “change x by” block, and make the value negative. Click to test. The sprite moves left.

To make the sprite jump up and down, use 2 “change y by” blocks. In Scratch, y values represent spots between the top and bottom edges of the stage. Click one of the blocks to test. A positive y value moves the sprite up. Change a block to a negative value, and click. The sprite moved down. In this example, the sprite jumps up and comes back down. To do this, connect the “change y” blocks, and place a “wait” block from the control menu between them. Click to run it. The sprite moves up, waits, then comes back down. Change the value in the “wait” block to make the sprite jump faster or slower.

Now that the sprite has left, right, and jump movements, program this code to run when the keys are pressed. Each movement code stack should run if a key is pressed. Click control, and add an “if” block to each movement block stack. Then, click sensing, and add a “key pressed” block to each. Changing x by a positive value will move the sprite right. Select a key to move the sprite right. Changing x by a negative value will move the sprite left. Choose a key to move the sprite left. Then, select a key to move the sprite up and down.

To run this code for the entire program, add a forever loop from control around the three if statements. Run this code at the start of the program by adding a “when flag clicked” event. Click the flag to test. Press the arrow keys, and… awesome! The sprite moves left, right, and jumps! Tinker with the values in the “change x” and “change y” blocks to change the movement of your sprite.

To program the other character sprite or the boat, copy over the code by dragging it on top of the sprite, then change the key press conditions.

Finally, tell the user how to use your program. Click "see project page," and add instructions for which keys to press. Click "see inside" to return to your code.

Now, it’s your turn! Program different movements using “change x” and “change y” blocks. Program each movement code stack to run using “if" and “key pressed” blocks. Program the code to run for the entire program using “forever” and “when flag clicked" blocks.

Choose an Add-On
play_circle_filled
Sea Sounds
Add sound effects to your story.
arrow_forward
play_circle_filled
Sun Ray Animation
Draw and animate a sun sprite.
arrow_forward
play_circle_filled
Sink the Ship
Program a sinking ship in your story.
arrow_forward
play_circle_filled
Second Scene
Program a Second Scene for your Story.
arrow_forward
play_circle_filled
Clouds
Draw and program clouds that move.
arrow_forward
play_circle_filled
Gamify
Program one or more sprites to move when keys on the keyboard are pressed.
arrow_forward
arrow_backward Back
Next arrow_forward
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!