In this exercise, we'll create a simple game that uses both Boolean and numerical operators.
The game will involve a sprite trying to collect items while avoiding obstacles.
Step 1: Setting Up the Game
1 Create a new Scratch project.
2 Choose a backdrop for your game.
3 Add a sprite for the player, one for the collectible item, and one for the obstacle.
Step 2: Programming the Player Movement
1 Add the following script to control the player's movement:
when green flag clicked
forever
if <key [right arrow] pressed?> then
change x by (5)
end
if <key [left arrow] pressed?> then
change x by (-5)
end
if <<key [up arrow] pressed?> and <(y position) < (170)>> then
change y by (5)
end
if <<key [down arrow] pressed?> and <(y position) > (-170)>> then
change y by (-5)
end
end
Step 3: Programming the Collectible Item
1 Add the following script to the collectible item sprite:
when green flag clicked
forever
go to x: (pick random (-240) to (240)) y: (pick random (-180) to (180))
wait until <touching [Player v]?>
change [score v] by (1)
end
Step 4: Programming the Obstacle
1 Add the following script to the obstacle sprite:
when green flag clicked
forever
move (10) steps
if <touching [edge v]?> then
turn (180) degrees
end
if <touching [Player v]?> then
stop [all v]
end
end
Step 5: Adding a Score and Timer
1 Create variables for "score" and "time".
2 Add the following script to control the timer:
when green flag clicked
set [time v] to (30)
repeat until <(time) = (0)>
wait (1) seconds
change [time v] by (-1)
end
stop [all v]
Congratulations! You've created a simple game using Boolean and numerical operators. The player
moves using arrow keys (Boolean operators check for key presses), collects items to increase the
score (numerical addition), and must avoid the obstacle while racing against the timer
(numerical comparison for time running out).
Try playing the demo above to see how the game works. You can move the player using the arrow
buttons, try to collect the items, and avoid the moving obstacle!