Time for action - paddle meets ball

When the ball contacts the paddle, let's make our paddle say, "Nice Shot!"

  1. Select the paddle sprite.
  2. Add a when flag clicked block from the Control palette.
  3. From the Control palette, add a forever if block.
  4. From the Looks palette, add a say for 2 secs block to the forever if block.
  5. Change Hello! to something fun, like Nice shot! Change 2 seconds to 1 second so our message doesn't display too long.
  6. From the Sensing palette, drag the touching block into the input value of the forever if block.
  7. From the drop-down list in the touching block, select ball.
  8. Click the flag to play the game.
Time for action - paddle meets ball

What just happened?

Hey, that was a nice shot! Did you think our paddle was a bit pompous to dish out the self-compliment each time the ball and paddle collided?

As long as the ball stays in play, the paddle repeats its message after each shot because we added the forever if touching ball block. Our keyword was if.

Let's slow down our program and take a closer look.

Start single stepping

Scratch includes a start single stepping setting that significantly slows down the program and allows us to see the code as the program runs. Let's use the start single stepping option to examine our forever if block.

  1. From the Edit menu, select the start single stepping option.
    Start single steppingforever if block, usingpaddle, in contact with ball
  2. Click the flag. The game starts in slow motion.
  3. Select the paddle from the sprites list so you can observe the scripts. The touching ball condition on the forever if block flashes a green color.
  4. Press the right and left arrow keys, and watch the move blocks flash. The scripts are also highlighted in a white border.
  5. To return the game to normal speed, select stop single stepping from the Extras menu.

What just happened?

Scratch was designed to teach. When we slowed down the program with the start single stepping option, we told Scratch to show us each step of the script as it executes. We were able to see each step of our program, block by block.

The forever if block made the script constantly check if the paddle was touching the ball. Only when the two sprites touched did the say Nice Shot! for 1 secs block flash.

If we were to replace the forever if touching ball block with an if touching ball, the script would make only one check when we clicked the green flag. Go ahead and give it a try. I'll wait.

Conditional statements

Conditional statements are an integral part of our everyday, non-programming thought process in that we evaluate our environmental inputs and take actions based on the results. Here's an example: You want to cross the street. If there are no oncoming cars, you cross the street. If there are oncoming cars, you wait and check again.

If we reframe our example of crossing the street into a programming statement using one of Scratch's conditional statements, we might say the following:

  1. If there are no cars coming, cross the street.
  2. If there are cars coming, do not cross the street; or else, cross the street.
  3. You'll wait until there are no cars coming, and then cross the street.
  4. When you receive a walk signal, cross the street.

The conditional blocks control when and if something happens, so we find them in the Control palette.

Pop quiz

  1. To analyze the project's script one block at a time, you would use which of the following features?
    • Block-by-block analyzer
    • Single stepping
    • Watch the Scratch scripts really carefully
  2. Which of the following blocks represent a conditional statement?
    • Move 10 steps
    • Forever
    • Forever if
    • Repeat 10

What's the score?

How do we know how good we are if our game doesn't keep score? The next step in our project is to add points to our score each time we hit the ball and to display the value on the stage.

Before we begin, turn off any remaining monitors from the stage because we no longer need them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.238.205