Guided Tutorial: Creating a Simple Game with Conditionals
In this tutorial, we'll create a simple game where a sprite tries to catch falling objects. We'll use conditionals to control the sprite's movement and detect collisions.
Step 1: Setting Up the Stage
1. Create a new Scratch project.
2. Choose a backdrop for your game. You can use the default backdrop or choose a new one from the library.
Step 2: Creating the Player Sprite
1. Choose a sprite for the player. This will be the character that moves to catch falling objects.
2. Add the following script to control the player's movement:
when green flag clicked
forever
if <key [left arrow] pressed?> then
change x by -10
end
if <key [right arrow] pressed?> then
change x by 10
end
if <touching [edge v]?> then
if <(x position) > (0)> then
set x to (240)
else
set x to (-240)
end
end
end
Step 3: Creating the Falling Object
1. Add a new sprite that will be the falling object. You can use any sprite you like.
2. Add the following script to control the falling object:
when green flag clicked
forever
go to x: (pick random (-240) to (240)) y: (180)
repeat until <touching [edge v]?>
change y by -5
if <touching [Player v]?> then
change [score v] by (1)
play sound (pop)
wait (0.1) seconds
end
end
end
Step 4: Adding a Score
1. Create a variable called "score" for all sprites.
2. Add the following script to initialize the score:
when green flag clicked
set [score v] to (0)
Congratulations! You've created a simple game using conditionals. The player sprite moves left and right based on key presses, and the falling object resets its position when it touches the edge or the player. The score increases each time the player catches the falling object.
Try playing the demo above to see how the game works. You can move the player left and right using the buttons, and try to catch the falling object to increase your score!