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 movementsthat you’d like. To start, select a sprite to control withthe 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 valueson the stage. Click to run it. Great, the sprite moves right. Drag out another “changex 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 valuesrepresent spots between the top and bottom edges of the stage. Click one of the blocksto test. A positive y value moves the sprite up. Change a block to a negative value, andclick. The sprite moved down. In this example, the sprite jumps up and comesback down. To do this, connect the “change y” blocks, and place a “wait” blockfrom the control menu between them. Click to run it. The sprite moves up, waits, thencomes 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 thekeys are pressed. Each movement code stack should run if a keyis pressed. Click control, and add an “if” block toeach 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 tomove the sprite right. Changing x by a negative value will move the sprite left. Choose akey 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 threeif 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 “changex” 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 ontop of the sprite, then change the key press conditions.
Finally, tell the user how to use your program. Click "see project page," and add instructionsfor which keys to press. Click "see inside" to return to your code.
Now, it’s your turn! Program different movements using “changex” 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 programusing “forever” and “when flag clicked" blocks.
Välj ett tillägg
Add sound effects to your story.
Sun Ray Animation
Draw and animate a sun sprite.
Sink the Ship
Program a sinking ship in your story.
Program a Second Scene for your Story.
Draw and program clouds that move.
Program one or more sprites to move when keys on the keyboard are pressed.