particular code block sends its message and then allows the script in which it is
embedded to continue executing. Alternatively, the following script not only sends
a broadcast message but also waits until every script in the application, which has
been set up to execute when the message is sent, has finished executing:
Using the hat block, you can set up a script to execute whenever a specified
message is received.
Figure 9.5
Broadcast messages provide the capability for one script to notify other scripts that an event has occurred.
184
Chapter 9
n
Conditional and Repetitive Logic
N o t e
Using the three previous scripts, you could create a new application made up of two button
controls and the default
Cat
sprite. By assigning the first script to the first button sprite, the
second script to the second button sprite, and the third script to the
Cat
sprite, you can make the
Cat
sprite jump up and down on the stage any time you click on one of the button sprites.
Conditional Programming Logic
The next set of control code blocks provided by Scratch is shown in Figure 9.6. These
code blocks allow you to apply conditional programming logic to your scripts.
Using these code blocks, you can analyze data within your applications and make
decisions based on this analysis, resulting in the conditional execution of collec-
tions of code blocks. The key concept to understand when working with these types
of code blocks is that conditional logic involves an evaluation as to whether a
condition is true or not. If the condition being analyzed is true, then the code
blocks embedded within the control block are executed. However, if the condition
being analyzed proves false, the embedded code blocks are not executed.
The following script demonstrates how to use the first code block shown in
Figure 9.6 to set up a loop in which execution is controlled by a conditional test.
Each time the loop repeats, it checks the value assigned to a variable named
Counter
to see if it is equal to 0. If it is, the loop executes, plays an audio file,
pauses for two seconds, and then checks to see if it should execute again.
Figure 9.6
These five code blocks let you conditionally execute collections of code blocks.
This next example demonstrates how to conditionally execute the playback of an
audio file. When executed, this script examines the value assigned to a variable
named
Counter
to see if it is equal to 0, and if it is, the audio file is played.
Sometimes you may want to execute either of two sets of code blocks based on
the results of a tested condition. This can be accomplished using the third code
block shown in Figure 9.6.
Here, a conditional test is performed that checks to see if the direction that a
sprite is facing is 90 degrees. If it is, the direction that the sprite is pointing is
reversed. If you run the script repeatedly, the direction that the sprite is pointing
is continuously reversed.
This next example demonstrates how to use a control block that pauses script
execution and waits for a specified condition to become true.
Here, a script has been set up that, once run, checks on the value assigned to
Counter
to see if it is greater than 5. If it is, an audio file is played. If
Counter
is not
greater than 5, then the script pauses its execution, waiting until the value of
Counter
exceeds 5 before finishing its execution.
Finally, the last example demonstrates how to work with the last of the control
blocks shown in Figure 9.6. Here, a loop is set up to execute repeatedly until the
value assigned to
Counter
is set equal to 3, at which time the loops will stop
running. Each time the loop runs, it moves, or bounces, its associated sprite up
and down on the stage.
Conditional Programming Logic
185
Nesting Conditional Control Code Blocks
As powerful as the control blocks are that facilitate conditional execution, they
are limited to analyzing a single condition at a time. To develop more complex
programming logic, you can embed one control block within another, as
demonstrated in the following example:
Here, one control block has been embedded within another control block to
further analyze the value assigned to
Counter
. If necessary, you can embed
control blocks many levels deep. However, the deeper you go, the more difficult
your scripts will be to understand and maintain.
Preventing Endless Loops
Loops are extremely powerful tools, providing the capability to perform repe-
titive tasks with ease. However, if you are not careful when setting them up, you
can accidentally set up an endless loop. An
endless loop
is a loop that, because of a
logical error on the programmer’s part, never ends. For example, you might want
to set up a loop that plays an audio file five times. But suppose when setting up
the loop you made a mistake that prevented the loop from ever terminating, as
shown here.
Here, the intention was to set up a loop that would execute five times. The
loop has been set up to execute for as long as the value assigned to
Counter
is
186
Chapter 9
n
Conditional and Repetitive Logic
less than 5.
Counter
is assigned an initial value of 1, and its value is supposed to be
incremented by 1 each time the loop executes. However, instead of incrementing
the value of
Counter
by 1 at the end of the loop, the value of
Counter
is decremented
by a value of
1. As a result, the loop never terminates, forever repeating the
playback of the audio file. To prevent endless loops from occurring, you need to
take extra care when setting up loops and test your scripts thoroughly when
developing your applications.
Terminating Script Execution
The last two control blocks offered by Scratch are shown in Figure 9.7. These
code blocks programmatically halt script execution within your Scratch
applications.
Using the first of these two control blocks, you can halt the execution of the
scripts in which the code block is placed, as demonstrated in the following
example:
Here, the script checks to see if the value assigned to a variable named
Counter
is
equal to 3, and if it is, an audio file is played. If
Counter
is not equal to 3, then a
different audio file is played, and the script’s execution is halted. Halting a script
this way forces its immediate termination, even if the script contains additional
code blocks that have not been executed.
Using the second control block shown in Figure 9.7, you can not only halt the
execution of the current script, but you can also halt the execution of every
script in the application. For example, the following script executes a loop three
times and then halts the execution of every script in the application in which it
resides.
Terminating Script Execution
187
Figure 9.7
Using these code blocks, you can halt the execution of any or all scripts within an application.
Developing the Ball Chase Game
The rest of this chapter is dedicated to teaching you how to create your next
Scratch application, the Ball Chase game. This application makes heavy use of
different control blocks to control the movement of the ball and the cat that
chases it around the stage. In total, the application is made up of four sprites and
nine scripts. The object of the game is to try to prevent the cat from catching the
ball as it chases it around the stage. If you can keep the ball out of the cat’s reach
for 30 seconds, you win. Figure 9.8 shows how the game looks when first started.
To play, all you have to do is move the mouse-pointer around the stage, and
the ball will automatically follow. If the cat manages to catch the ball before
30 seconds is up, the game ends, as demonstrated in Figure 9.9.
188
Chapter 9
n
Conditional and Repetitive Logic
Figure 9.8
The object of the game is to prevent the cat from catching the ball.
Figure 9.9
The game ends if the cat catches the ball.
Figure 9.10 shows how the game looks when the player successfully manages to
evade the cat for the entire 30 seconds.
The development of this application will be accomplished by following a series of
steps, as outlined here:
1. Creating a new Scratch project.
2. Adding and removing sprites.
3. Adding variables needed by the application.
4. Adding an audio file to the application.
5. Adding a script to control ball movement.
6. Adding scripts that display game over messages.
7. Adding the scripts required to control and coordinate game play.
8. Saving and executing your work.
Step 1: Creating a New Scratch Project
The first step in the development of the Ball Chase game is to create a new Scratch
project. To do so, start Scratch, automatically creating a new Scratch project, or if
Scratch is already running, click on the New button located on the Scratch menu
bar.
Step 2: Adding and Removing Sprites
The Ball Chase game is made up of the default
Cat
sprite plus three other sprites
and a variable monitor, as shown in Figure 9.11.
Developing the Ball Chase Game
189
Figure 9.10
The player wins if the ball can be kept away from the cat for 30 seconds.
Since the default
Cat
sprite is not needed in this application, go ahead and
remove it. In its place you need to add a different sprite, representing a top-down
view of a different
Cat
sprite. To add this sprite, click on the Choose New Sprite
from File button. When the New Sprite window opens, drill down in to the
Animals folder and then select the
cat2
sprite and click on the OK button. By
default, the sprite is placed in the middle of the stage and faces in a 90-degree
direction. Leave this sprite in its default location in the middle of the stage,
change its direction to 0, and then change its assigned name to
Cat
. Next, add the
Ball
sprite by clicking on the Choose New Sprite from File button, drilling down
in to the Things folder, selecting the
beachball1
sprite, and then clicking on the
OK button. Place the
Ball
sprite on the top center of the stage and change its
assigned named to
Ball
.
To add the application’s remaining two sprites, which will be nothing more than
text strings saved as sprites, you must create them, which you can do using
Scratch’s built-in Paint Editor program. Both of these sprites consist of text
messages. For the first of these two sprites, open the Paint Editor by clicking on
the Paint New Sprite button. When the Paint Editor program opens, specify a
font type of
ComicSans
, a font color of
red
, and a font size of
18
. Type
Game Over,
press the Enter key, type
You lose!
into the Paint Editor, and then click on the
OK button. Using this same process, create a second sprite that says
Game Over!
You win.
Change the names assigned to these two sprites to
LosingMsg
and
WinningMsg
, respectively.
190
Chapter 9
n
Conditional and Repetitive Logic
Figure 9.11
An overview of the different parts of the Ball Chase game.
Step 3: Adding Variables Required by the Application
To execute, the Ball Chase game requires one variable as shown in Figure 9.12. To
add this variable to the application, click on the Variables button located at the
top of the blocks palette and then click on the Make a Variable button to define a
variable named
Elapsed Time
.
This variable will be used to display the amount of time remaining in the game.
Make sure that you leave the check box for this variable selected and that you
reposition the variable’s corresponding monitor to the upper right-hand corner
of the stage.
Step 4: Adding an Audio File to the Application
The Ball Chase game makes use of one sound effect, which simulates the meowing
of the cat as its chases the ball around the stage. To add this audio file, select the
Cat
sprite thumbnail in the sprite list and then click on the Sounds tab located at the
top of the scripts area. Next, click on the Import button to display the Import
Sound window, double-click on the Animal folder, select the Meow audio file, and
finally click on the OK button to finish adding the audio file to the sprite.
Step 5: Adding a Script to Control Ball Movement
The objective of the game is to try to keep the ball out of the reach of the cat for
30 seconds. The following script, which should be added to the
Ball
sprite, is
responsible for controlling the movement of the ball on the stage.
This script begins with a hat block. Next, a motion block is used to position the
ball in the upper middle portion of the stage. A looks block is then used to move
Developing the Ball Chase Game
191
Figure 9.12
The Ball Chase game requires one variable.
the sprite back one layer, ensuring that if the
Ball
sprite encounters the
Cat
sprite, the
Ball
sprite will be displayed under the
Cat
sprite instead of on top of it.
(You will learn about looks blocks in Chapter 10, ‘‘Changing the Way Sprites
Look and Behave.’’)
The rest of the script consists of a loop that repeatedly executes another motion
block. The motion block is responsible for moving the
Ball
sprite around the
stage to where the mouse-pointer is.
Step 6: Adding Scripts That Display Game Over Messages
You will add the script that is responsible for making the cat chase the ball
around the stage in the next section. Before doing so, add the following pair of
scripts to the
WinningMsg
sprite. These scripts are responsible for displaying and
hiding the game’s winning message.
The first of the two scripts shown above is responsible for hiding the display of
the sprite to which it has been added. The second script, on the other hand, is
responsible for displaying the sprite whenever a broadcast message of
You win
is
received. Note that this script includes a looks block that pushes the sprite to the
front of any other sprites that it may happen to overlap. This ensures that the
message is completely visible once displayed.
Once you have created and added these two scripts to the
WinningMsg
sprite, drag
and drop both of them onto the
LosingMsg
sprite and then edit the second script
so that it executes whenever a broadcast message of
You lose
is received.
Step 7: Adding Scripts Needed to Control
and Coordinate Game Play
To wrap up your work on the Ball Chase game, you need to add four scripts to
the
Cat
sprite. The first of these scripts is shown next and is responsible for
ensuring the cat chases the ball around the stage.
192
Chapter 9
n
Conditional and Repetitive Logic
This script begins by moving the
Cat
sprite to the center of the stage and
pointing it in its default upward direction. Next, it pauses for one second
and then enters into a loop, which repeatedly executes the embedded code
blocks. The first of these three code blocks points the cat sprite towards the
Ball
sprite. The second code block pauses the loop’s execution for .15 seconds,
after which the third block moves the
Cat
sprite 66 steps in the direction of the
Ball
sprite.
N o t e
The reason for imposing the .15 second delay in the script’s loop is to slow down things
enough to give the player a chance to keep the ball from the cat. If the little extra delay were
removed from the loop, the speed at which the cat moves would easily overcome even the
fastest player.
The second of the four scripts to be added to the
Cat
sprite is shown next. This
script is set to execute when the player starts the game by clicking on the green
flag button. The script begins by setting the value at which the audio is played
to 50% of the level of the computer’s current sound level. The rest of the script is
controlled by a loop that repeatedly runs two embedded code blocks. The first
code block pauses script execution for five seconds. The second code block plays
the Meow audio file. The result is that the cat will meow every five seconds as it
chases the ball around the stage.
The third script to be added to the
Cat
sprite is responsible for halting the
execution of all scripts in the application in the event that the cat manages to
touch the sprite during game play. The code blocks that make up this script are
shown here:
Developing the Ball Chase Game
193
This script is executed when the player starts the game by clicking on the green
flag button. The script’s overall execution is controlled by a loop. Within the
loop, a conditional test is performed that checks to see if the
Cat
sprite has made
contact with the
Ball
sprite. If this is the case, a broadcast message of
You lose
is
sent. Once this message has been received and processed by the other scripts in
the application, the last code block in the loop is executed, halting all script
execution.
The last script to be added to the
Cat
sprite is shown next. This script is res-
ponsible for keeping track of time as the application executes and for halting
game play after 30 seconds, should the player manage to keep the cat at bay for
that long.
When started, this script begins by resetting Scratch’s internal timer and then
assigning the current value of the timer (0.0) to a variable named
Elapsed Time
.
The rest of the script is controlled by a loop. Each time the loop executes, it
updates the value assigned to the
Elapsed Time
variable to reflect the timer’s
current value. Next, a check is made to see if the timer’s value has exceeded
30 seconds, and if it has, a broadcast message of
You win
is sent. Once processed
by the other scripts in the application, the execution of all scripts in the appli-
cation is halted. If, on the other hand, the timer’s value is less than 30 seconds, the
loop simply executes again. Accordingly, if the cat does not manage to catch
the ball within 30 seconds, thus ending the game, the fourth script will end the
game and declare the player to be the winner.
194
Chapter 9
n
Conditional and Repetitive Logic
Step 8: Saving and Executing Your Scratch Project
All right! Assuming you have followed along closely with each of the steps
presented in this chapter, your copy of the Ball Chase game should be ready for
testing. If you have not done so yet, save your new Scratch project. Once saved,
switch over to Presentation mode and execute the game. Remember that game
play begins when you click on the green flag button and that your object is to
keep the ball out of the cat’s reach for 30 seconds.
Summary
This chapter provided an overview of all of Scratch’s control blocks. You learned
how to use Scratch hat blocks and to pause and halt script execution. This
chapter also showed you how to set up different types of loops and to work with
all five of Scratch’s control blocks that support conditional programming logic.
You also learned how to control and coordinate script activity by sending and
receiving broadcast messages between sprites.
Summary
195
Do'stlaringiz bilan baham: |