In this video, you will program the ball sprite to rotate when the user presses the arrowkeys. Then, the user will be able to aim the ball!
First, make the ball sprite turn left when the left arrow key is pressed. To do this,from the “motion” menu, drag out a “turn left” block. The ball sprite should turnleft *if* it senses the user pressing the left key. From the “control” menu, placean “if” block around the “turn left block.” Then, from the sensing menu, placea “key pressed” block inside the "if" block. From the dropdown, select “left arrowkey.” Click the code, then press the left arrowkey. Nothing happens. That’s because this code only asks once if the arrow key is pressed.
Since you weren’t pressing the left arrow key exactly when you ran the code, nothinghappened. To program the ball sprite to keep asking if the user is pressing the left arrowkey, put this "if" block below your first "if" block *inside* the "forever" loop.
Click the "forever" loop again to run the program.
Now press the left arrow key. The ball sprite turns left! Press the spacebar, and the ballsprite moves in that new direction until it touches the edge of the screen. This is lookinggood! Now that you know this code works, you canuse similar code to turn the ball sprite right. From the motion menu, drag out a “turn right”block. Then, from the control menu, place an“if” block around the “turn right”block. Next, from the sensing menu place a “key pressed” block inside the “if”block, then click the dropdown and select “right arrow key.” Finally, place this"if" block inside the forever loop, and test the code by clicking on it again. When youpress the left arrow, the ball sprite turns left. When you press the right arrow, theball sprite turns right. When you press the spacebar, the ball sprite moves until it touchesan edge, and the loop keeps checking for each of these conditions over and over again.
The ball sprite has to sense a lot of things in its environment to work - whether the useris pressing the left arrow key, the right arrow key, or the spacebar, and whether theball sprite is touching the edge. This is similar to the way you use your senses! You'reconstantly checking for and receiving input from your eyes, ears, nose, and skin, andyou do things based on that input. Wrap this step up by adding a “when flagclicked” event block on top of the code stack.
Here's the game plan: Make the ball sprite turn left and right whenthe arrow keys are pressed using “if” “key pressed” “turn left” and “turnright” blocks. Then, add a “when flag clicked” blockto the top of the block stack. The next video will program the receiversto catch the ball.