Use Scratch's senses to design a game

Bouncing Ball

By now, you might realize that Breakout is an adaptation of Pong, which is a common source of remixed projects in Scratch.

When you set the ball in motion, you need to consider the following list of things, at a minimum. Where does the ball start? What happens when the ball touches the side of the stage? What happens when the ball touches the paddle? When is the game over? I'll address each question starting with the ball's direction and starting position.

To begin, you can create a script that runs when the green flag is clicked. Then, add the point in direction and go to x: y: blocks from the Motion palette. In this example, I modified the value of the point in direction block to 135. I also changed the x coordinate to -200 and the y coordinate to 50 in the go to x:  y: block. As a result, the ball always has the same starting location and trajectory.

Because I will be adding more startup-related information to this script later, I recommend creating a second script to control the motion of the ball. The following code shows the two starting scripts for the ball:

The script that defines the motion is a common way to program Pong and, if you've ever experimented with Scratch before, you've likely seen this script or a close variation of it. Reading the script from the top down, you can see that the ball is always moving 10 steps and checking for the boundaries so it can bounce back into play. It's also constantly evaluating whether or not the ball is touching the paddle sprite via the if block.

If the ball does touch the paddle, it bounces the sprite back into play with the point in direction and the move blocks. The point in direction block determines the new direction of the sprite through a simple math statement: 180 – the current ball direction. The reporter block direction always knows which way the sprite is traveling.

If you leave the point in direction value a static value, such as 45, then the ball would always come off the paddle at 45 degrees, even if the ball approached the paddle at -135 degrees. That would be too unrealistic. The calculation used here ensures the ball always bounces off the paddle at either 45 or -45, because the ball is traveling at 135 or -135 degrees. Changing the 180 in the calculation will change the trajectory of the ball as it comes off the paddle.

Game Over

What about checking to see whether the game is over? How do you determine when the player loses? In Breakout, the game ends when the player misses the ball, so I'll use that as the game-over scenario. The simplest way to sense whether the player has missed the ball is to create a new sprite in the shape of a rectangle with a unique color that fills the entire width of the stage. You end up with a line that you position at the bottom of the stage. The game will end when the ball touches the line.

To build the game-over script, I'll add the wait until and stop all blocks from the Control palette to the ball's script that sets the starting direction and location. The wait until block needs a value to evaluate. From the Sensing palette, you can add the touching color block to wait until.

To change the color in the touching color block to match your line, click on the color square in the block. Your mouse cursor turns to a dropper. Touch the dropper to the "game over" line to set the color. Here is the revised script, which includes the game-over scenario:

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content