You are currently using an older browser. Please update to the latest browser to ensure features and pages work properly. Learn more
3. Aim in the Right Direction
In this video, you'll program the ball spriteto rotate when the user presses the arrow keys.
Then, the user will be able to aim the ball.
First, make the ball sprite turn leftwhen the Left Arrow key is pressed.
To do this, from the Motion Menu,drag out a Turn Left block.
The ball sprite should turn left if it sensesthe user pressing the Left key.
From the Control Menu, place anIf block around the Turn Left block.
Then, from the Sensing Menu, place aKey Press block inside the If block.
From the drop down, select Left Arrow key.
Click the code, then press the Left Arrow key.
That's because this code only asksonce if the arrow key is pressed.
Since you weren't pressing the Left Arrow keyexactly when you ran the code, nothing happened.
To program the ball sprite to keep askingif the user is pressing the Left Arrow key,put this If block below your first If blockinside 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 Space Bar and the ball sprite moves in thenew direction until it touches the edge of the screen.
This is looking good.
Now that you know your code works,you can use 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 blockaround the Turn Right block.
Next, from the Sensing Menu, place aKey Press block inside the If block.
Then, click the drop down and select Right Arrow key.
Finally, place this If block inside the Forever Loopand test the code by clicking on it again.
When you press the Left Arrow, the ball sprite turns left.
When you press the Right Arrow, the ball sprite turns right.
When you press the Space Bar, the ball sprite movesuntil it touches an edge and the loop keeps checkingfor each of these conditions over and over again.
The ball sprite has to sense a lot of thingsin its environment to work, whether the useris pressing the Left Arrow key, the Right Arrow key,or the Space Bar and whether the ball spriteis touching the edge.
This is similar to the way you use your senses.
You're constantly checking for and receiving inputfrom your eyes, ears, nose and skin,and you do things based on that input.
Wrap this step up by adding a One Flag Click Eventblock on top of the Code Stack.
Here's the game plan.
Make the ball sprite turn left and rightwhen the arrow keys are pressedusing If Key Pressed, Turn Left and Turn Right blocks.
Then, add a When Flag Clicked blockon top of the Block Stack.
The next video, we'll program the receiversto catch the ball.
Make the ball sprite turn left and right when those keys are pressed using "if," "key pressed," "turn left," and "turn right" blocks.
Add a "when flag clicked" block to the top of the code.