In this video, you’ll make the athlete sprite move when the user presses the arrow keys.
You’ll create two conditionals: one to move the sprite left, and one to move it right.
From the control menu, drag out two "if" blocks, and snap them together.
Go to the sensing menu, and drag a "key pressed" block into each conditional. For the firstone, select "left arrow" in the dropdown. Select "right arrow" for the second one.
Next, from the "motion" menu, place a “point in direction” block into each of the conditionals.
The first conditional moves the sprite left, so select negative ninety in the dropdown.
In the second, select ninety. To test it, hold down either the left or right arrow key,and click the block stack. Try to get the sprite to turn around!
As you can see from the test, pressing the left arrow flips the sprite upside down. Fortunately,Scratch has a block that will make the sprite turn side to side, but not upside down. Fromthe motion menu, drag out the “set rotation style” block, and place it at the top ofthis code stack. Select “left-right” from the dropdown menu. Test again. Make sure tohold the key while you click the code. The sprite stays right-side up. Nice job!
Next, make the sprite move in the direction it's pointing. Place a "move" block undereach conditional. Make this code keep running as long as a keyis being pressed. Snap a “forever” loop around the block stack.
Click this to test it. Press the left and right arrow keys, and watch the athlete moveleft and right. Pretty sweet. Finally, use a “when flag clicked” eventto start the athlete's code. Snap it on top of the stack, then click the flag to start the game.
Here’s the game plan: Make the athlete sprite move left and rightwhen the left and right arrow keys are pressed using “if” blocks and “key pressed”events. Then, add a “set rotation style” blockto keep the sprite from flipping upside-down. Make the sprite move in the direction it’sfacing with a “move” block. Use a “forever” loop to make the athletekeep moving as long as a key is pressed. Start the game with a “when flag clicked” event.