Now you are going to add code so that some of the doors in your game world are locked, and the player must find the key to open them and get to the next room.
Switch to the key
sprite. Click on show
in the Scripts menu so that the sprite appears on the Stage.
Edit the
sprite’s costume so that it is blue.key
Switch your Stage backdrop to room 3, and place the
sprite somewhere difficult to reach!key
Add code to the
sprite to make it only visible in room 3.key
Create a new list called inventory
to store the items your player
sprite collects.
The code you need to add for collecting the key is very similar to the code for collecting coins. The difference is that you add the key to the inventory
.
Add code to your Stage to empty your inventory at the start of the game.
Test out your game to check whether you can collect the
sprite and add it to your inventory.key
Now add the locked door. Select the
sprite and click on door-blue
in the Scripts menu, and then position the sprite across the gap between the two walls.show
Add code to the
sprite so that it is only visible in room 3.door-blue
Add code to the door-blue
sprite so that, when the key is in the
, the sprite inventory
hides
to allow your player
sprite to pass.
Test out your game and see if you can collect the blue key to open the door!