Use Scratch's senses to design a game

Increasing Ball Speed

Because I limited the number of objects included in the Fantasy Breakout game, I need to create a challenge for the player. One challenging aspect is to make the ball pick up speed after every four hits of the paddle.

To increase the speed of the ball, the script needs a way to determine how many times the ball hits the paddle. You already did the programming to know when those hits occur. So, the next step will be to record those hits and then determine whether you need to increase the speed.

To prepare for this step, you'll need to create two new variables named hits and speed. From the Variable palette (Data palette in Scratch 2.0), click on the Make a Variable button to open the Variable Name dialog. Then, enter a meaningful name, such as speed. You can assign the variable to all sprites or just the ball. In this game, no other sprites need to know the speed, so you can make it for this sprite only. After you click OK to create the variable, Scratch makes four new Variable blocks available: set to, change by, show variable, and hide variable.

Next, create the hits variable. In the initial game design, the ball is the only sprite that will use hits, but that doesn't have to be the case. Therefore, you might want to make that variable available to all sprites (i.e., a global variable).

If you're going to be changing the values of your two variables as the game plays, you will need to reset their starting values. Add two set to blocks to the script that sets the ball's starting location and direction. You can insert the blocks right after the when flag clicked block, so it's the first thing that happens. Scratch will remember the variable values across sessions, so it's necessary to reset your variables at the beginning of each game.

The set to block has a drop-down list of all variables that are available to the sprite. Select speed in the first instance block and select hits in the second. In my example, I set the initial speed to 8 and hits to 0. If the ball appears choppy as it moves across the stage when you run this game on the Raspberry Pi, try increasing the starting value of the speed variable from 8 to 16.

The speed reporter block in the Variables palette will become the value for the move block in the other ball script (the one that defines motion). Do that by dragging the speed block from the Variable palette into the move block. Remember that this script has two move blocks.

Next, add the change by block at the end of the if block. Select the hits variable and change the value to 1. Now, every time the ball touches the paddle, the hits variable will increase by one. You can see the progress of the scripts here:

Now that you're recording each hit, you need to calculate when to increase the ball speed. To determine whether hits is an increment of 4, you can use the mod block to check the remainder of hits/4. I'll show how to build the script and then review it.

To begin, add a second if block from the Control palette to the end of the if touching paddle check, so that you have nested if statements, meaning the script only checks for the second condition if the first condition is true and after all the blocks in the first condition run.

From the Operators palette, add the equals block and drag the mod block into the left side of the equals block. Enter 0 to the right of the equals sign. Enter 4 in the right side of the mod block. To the left side of the mod, add the hits reporter block from the Variables palette. Now you have a statement that checks to see whether hits mod 4 is 0.

When the result is zero, you want to change the value of the speed variable, so add the change by block from the Variables palette to the if block that evaluates mod. The higher you make the value in the change by block, the faster the ball will go.

In plain English, mod evaluates the remainder of a division problem. The block checks to see whether hits/4 has a remainder of 0. If so, the check evaluates to true, meaning you have an increment of four, and the speed variable is increased. Here is the script with the mod check:

Breaking the Characters

The ball and paddle are working well right now. The last piece of the game is to program what happens when the sprite and the ball collide. On the basis of what I've covered so far, you should be able to program what happens when a sprite touches the ball.

Here's the general sequence of events for the fantasy sprites as I defined them: Go to a random position on the stage and show the sprite. Continually check to see whether the sprite is touching the ball. If the sprite and the ball touch, then hide the sprite, change the score by one, and stop the script. Stopping the script is important, because it stops the sprite from moving. If you get stuck, check out Figure 1 for a code example.

Figure 1: Sample collision solution.

You only need to create this script for one of the fantasy sprites initially. After the script is working the way you like, you can duplicate the script. The duplicate option is available when right-clicking on the stack of blocks. After you select duplicate, you can drag the new script to another sprite. Repeat this step for each of the fantasy sprites you add.

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