Class-5 Computer-Gaming In Scratch (Notes)
Class-5 Computer-Gaming In Scratch (Notes)
SUBJECT- COMPUTER
CLASS 5
GAMING IN SCRATCH (Notes)
You can also use Scratch to design compelling games. To do so, it is necessary to be
familiar with certain Scratch blocks. Let us explore the different categories of blocks used
to develop games.
➢ CONTROL BLOCKS
The Control blocks let you control the flow of execution of steps in a script. You can use
conditional and iterative statements (often called loops) in your script with the help of the
Control blocks. These blocks provide you a lot of capabilities, such as event-driven
programming, creating loops, execution of control logic, and many more. The uses of
some of the most common Control blocks are discussed below:
• Using if-then Block
The if-then block performs a set of actions if the given
condition is true. The condition given here is a Boolean
expression that returns either true or false. For example,
on executing the script given in Figure, if the mouse
pointer touches the sprite; the sprite will change its
direction and colour.
• Using if-then-else Block
The if-then-else block is an extension of the if-then
block. Consider the script given in Figure 7.2 to
understand the use of the if-then-else block. In the
script, the block under the if-then block is executed
if the given condition evaluates to true; otherwise,
the block under the else block is executed.
• Using repeat<count> Block
The repeat <count> block is used to iterate or repeat
a set of commands a specified number of times.
For example, the script given in Figure shows that
the blocks inside the repeat block will be executed
four times.
Class 5, Computer, Gaming in Scratch (Notes), Page 1 of 6 PTO
• Using repeat until Block
The repeat until block is used in the same manner
as the repeat <count> block, except that it checks for
the given condition. The block of script is repeated
until the given condition results to true. As soon as
the condition becomes true, the script stops repeating.
The script given in Figure explains the working of the repeat until block.
• Using forever Block
The forever block is used to repeat a set of commands
forever, i.e., till infinity. In other words, if this block
is used, a set of commands will be executed without
stopping. Observe the script given in Figure
• Nesting of Blocks
You can put various blocks inside each other to perform
certain tasks. This is called nesting of blocks.
For example, you can place the repeat block inside the
forever block or the if-then block inside the repeat block.
The script given in Figure 7.6 describes the nesting of
blocks.
❖ USING ASK AND ANSWER BLOCKS
The Sensing block category has light blue colour-coded blocks, which are used to sense
the keyboard input while executing the script.
The block prompts the user to type input using the keyboard and the
block stores the input entered by the user.
The question appears in a voice bubble on the screen. The program waits until the user
types in a response by either pressing the Enter key or clicking on the check mark
Let us use these blocks for creating a game.
Creating Quiz Game
While playing games, you must have observed that the score of the player increases or the
timer of the game decreases continuously. The game gets over when the value of the
timer becomes zero. You can create these effects in Scratch projects by updating the
variables repeatedly using the Control blocks, such as forever, repeat, and repeat until.
Class 5, Computer, Gaming in Scratch (Notes), Page 2 of 5 PTO
❖ RULES FOR THE GAME
The rules for creating the quiz game are listed below:
1. 1. The user is asked a question and the prompt for writing the answer will be displayed on
the screen.
2. If the user types in the correct answer, the value of the Score variable will increase by 10.
Also, a message showing 'Correct' will be displayed.
3. If the answer of the question is wrong, then 'Try again!' message will be displayed.
❖ CREATING THE GAME
1. Create a new project in Scratch. Add a backdrop and sprite as shown in Figure 7.7.
2. Click on the Variables block category. Click on the Make a Variable button. The New
Variable dialog box opens.
3. Type ‘Score’ in the New variable name text box. Select either For all sprites or For this
sprite only option and click on OK.
4. The Score variable is displayed on the stage. Now, create the script as shown in Figure7.8
Figure 7.10 Script for the Starfish Figure: 7.11 Script for the Shark Sprite Figure: 7.12 Script for the Crab
Sprite Sprite
➢ Now, select the Shark sprite and add the script as shown in Figure 7.11.
➢ You can clearly see that in the script, the when I receive() block is used. This ensures
that the broadcast message sent by the Starfish sprite is received by the Shark sprite.
➢ The Shark sprite will act after getting the broadcast message, messagel. It will move
towards the Fish sprite and after reaching it, the message, "I caught the fish!", is
displayed.
➢ Add the scripts for the Crab and Fish sprites to make your story more interactive as
shown in Figures 7.12 and 7.13.
➢ Now, click on the Go (Green Flag) button to execute the Figure 7.13: Script for the Fish
Sprite Scratch project and observe the actions of the sprites.