In this step, you will add your first puzzle, which will be to push the button a certain number of times.
When the game starts, the button needs to stay in the same place, and always be visible on the front layer.
Add the following blocks to the button sprite.
The button will need to be pushed a number of times for the puzzle to be completed. For this, you will need a variable
to store the number of pushes.
Create a new variable
and call it button pressed
.
At the start of the game, button pressed
should be 0
.
Add the following blocks to the button sprite.
A repeat until
block is a loop that keeps repeating until a certain condition is met.
Choose: How many times will the button need to be pressed to solve the puzzle? In this example, it will need to be pressed 5
times, but you can choose a different number.
Add a repeat until
loop, and set its condition to be when button pressed
is equal
to
.5
Now, the player needs to be able to push the button. They should only be able to press it when the character is close to the button though!
Add blocks to detect if the character is close to the button when the button sprite is clicked.
If the character is close, and the button is pressed, then the
variable can be increased. If the character is not close, the puzzle should reset; the player needs to push the button five times in a row, before trying any other puzzles.button pressed
Tip: In Scratch, the distance between any two sprites is calculated from the centres of the sprites. This means that large sprites can look as if they are touching, but their centres may still be far apart.
Add code to change the value of the variable button pressed
.
Test: Run your project and move the character close to the button. As you click on the button, the
variable should increase. You can adjust the value of button pressed
distance to Monet
up or down, until you find a number that makes sense to you.
You can use the join
block to say
to the player how many times the button has been pressed.
Place a join
block inside another one. Then add in the text you want, and the
variable, all inside a button pressed
block.say
For example:
Tip: Make sure you add spaces in the text in your
block.join
The loop will end when the button has been pressed 5
times, then the last block in the script will be run. This can tell the player that the task is complete.
Use a
block to tell the player the task has been completed.say
Test: Run your project and move the character close to the button. When you click on the button five times, the task should be complete.