Open the sample pong game that's included with Scratch:
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.
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 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.