SCRATCH GOLD II
SCRATCH GOLD I
2 of 3

6.12 Doors and keys

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 key sprite’s costume so that it is blue.

Switch your Stage backdrop to room 3, and place the key sprite somewhere difficult to reach!

screenshot

Add code to the key sprite to make it only visible in room 3.

Create a new list called inventory to store the items your player sprite collects.

  • Click on Make a List under Variables.
Make a list
  • Type in the name of your list. You can choose whether you would like your list to be available to all sprites, or to only a specific sprite. Click OK.
List name
  • Once you have created the list, it will be displayed on the stage, or you can untick the list in the Scripts tab to hide it.
List show/hide
  • Click the + at the bottom of the list to add items, and click the cross next to an item to delete it.
List show/hide
  • New blocks will appear and allow you to use your new list in your project.
List blocks

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.

key

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 key sprite and add it to your inventory.

Now add the locked door. Select the door-blue sprite and click on show in the Scripts menu, and then position the sprite across the gap between the two walls.

screenshot

Add code to the door-blue sprite so that it is only visible in room 3.

Add code to the door-blue sprite so that, when the key is in the inventory, the sprite hides to allow your player sprite to pass.

door

Test out your game and see if you can collect the blue key to open the door!

×
×

Cart