Time for action - open the sample pong project

Open the sample pong game that's included with Scratch:

  1. In Scratch, click the File | Open menu to display the Open Project dialog box.
  2. Click on the Examples button. Then open the Games folder and select Pong. Click OK to load the project.
  3. Play the game by clicking on the flag. Move the mouse left or right to control the paddle and hit the ball.
  4. Move the mouse up and down the stage. The paddle doesn't move.
  5. When you've had enough, stop the game.
  6. Let's get some more information about what the ball is doing. From the Motion palette, click on the checkbox next to direction. This displays a new report block on the stage that shows the ball's direction.
    Time for action - open the sample pong project
  7. Play the game again and note how the ball direction value changes as the ball travels across the stage. When you're done, let the ball hit the red stripe.
  8. Let's take a look at the paddle's script by clicking on the paddle from the sprites list.
  9. We can also monitor the paddle's x coordinate as you move it across the stage. From the Motion palette, click the checkbox next to the x position block. A new block displays on the stage that reports the paddle's current x position.
    Time for action - open the sample pong project
  10. Now, let's take a look at scripts that are associated with the ball by selecting the ball from the sprites list. See the following screenshot:
    Time for action - open the sample pong project
  11. Play another game. Watch the monitor blocks report the direction of the ball and the location of the paddle in real time.

What just happened?

If real table tennis was this easy, I might enjoy it more! Let's analyze our project from the time we clicked the flag.

The mission was simple: Keep the ball from landing on the red stripe to keep the game alive. As we noted, our game is fairly simple. We have a paddle and a ball. The paddle moves are based on our mouse's actions. As we drag the mouse to the left, so went the paddle. However, the paddle didn't respond to up and down mouse movements.

We tracked the ball's direction and the paddle's x coordinate by enabling the direction and x position blocks, respectively. Clicking on the checkbox next to each block placed a real-time readout of the block's value on the stage.

In Chapter 5, we learned how to find the coordinates of the mouse via the mouse x and mouse y values. In the pong game, we learned how to use the mouse x value to control the movement of the paddle. The script reads the x coordinate of the mouse and sets it to the x coordinate of the paddle. The x coordinate of the mouse became the x coordinate of the paddle sprite.

The direction change was accomplished with the point in direction block that executed when the ball touched the paddle. The script calculated the new direction by subtracting the ball's direction from the constant value of 180.

As the ball bounced off the paddle, the script put a twist on the direction change by turning the ball by a random number of degrees between -20 and 20.

We'll look at the project scripts in more detail as we customize them.

Dynamic interaction

The mouse x and mouse y blocks enable us to use the X and Y coordinates of the mouse as direct input values into a script. We can use the values to move a sprite around the stage or change a graphical effect based on the mouse x or mouse y values.

Since the mouse x and mouse y values are numeric, we can use them as the input for any block that accepts a numeric value. The mouse x and mouse y blocks hold variable data.

Variables

Variables store numbers and information. Let's think of a variable you probably care very deeply about: your checking account balance. Each time you deposit or withdraw money, the balance changes, and the bank calculates a new balance. When you want to know how much money you have available, you log in to your online bank account and check the balance.

We use variables to store values that can change as our program runs. The value assigned to a variable is often the output of a previous programming command or calculation that we use as an input for a future calculation.

Consider our bank account. The bank outputs our balance. When we review the family budget, we take the bank balance into consideration as we pay our bills and make various decisions based on the budget needs and bank balance.

Variables generally make our programming life easier, whereas constant values give us less flexibility and can complicate our code. The value of a constant never changes. If we want to change a calculation that uses a constant, we have to go into the scripts, find the value, and change it. The numbers in your scripts, such as 10 or the year of your birthday, represent constant values.

We'll work more with variable data in the upcoming chapters. Let's get back to pong.

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

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