Skip to content

Please update your browser

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

3. Aim in the Right Direction

Transcript

In this video, you'll program the ball sprite to rotate when the user presses the arrow keys.

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 turn left if it senses the user pressing the Left key.

From the Control Menu, place an If block around the Turn Left block.

Then, from the Sensing Menu, place a Key Press block inside the If block.

From the drop down, select Left Arrow key.

Click the code, then press the Left Arrow key.

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, nothing happened.

To program the ball sprite to keep asking if the user is pressing the Left Arrow key, 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 Space Bar and the ball sprite moves in the new 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 block around the Turn Right block.

Next, from the Sensing Menu, place a Key Press block inside the If block.

Then, click the drop down and select Right Arrow key.

Finally, place this If block inside the Forever Loop and 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 moves until it touches an 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 user is pressing the Left Arrow key, the Right Arrow key, or the Space Bar and whether the ball sprite is touching the edge.

This is similar to the way you use your senses.

You're constantly checking for and receiving input from your eyes, ears, nose and skin, and you do things based on that input.

Wrap this step up by adding a One Flag Click Event block on top of the Code Stack.

Here's the game plan.

Make the ball sprite turn left and right when the arrow keys are pressed using If Key Pressed, Turn Left and Turn Right blocks.

Then, add a When Flag Clicked block on top of the Block Stack.

The next video, we'll program the receivers to catch the ball.

arrow_backward Back
Next arrow_forward
Instructions
  1. Make the ball sprite turn left and right when those keys are pressed using "if," "key pressed," "turn left," and "turn right" blocks.
  2. Add a "when flag clicked" block to the top of the code.