Key and Door

keys and doors

How to implement this Pattern in MakeCode

Step by Step instructions

In this simple application of the key and door pattern we will keep our current end goal, which in our starting template was the chest, and add in a new kind of sprite called a key which the player needs to collect before touching the end goal has an effect.

First add in a new tile colour to represent our key in your Tilemap plan of the first level. There are more detailed steps on how to do this in the add Static Enemy instructions. But in short create one tile to represent your location of your key.

To do this click on your tilemap to bring up the editor. Click on My Tiles > + (plus sign) and fill this new tile entirely with one colour. Then select that tile and place one tile in your level design where you want your key to be. This is shown in the screenshot below.

key and door 1

Follow the same kind of patter used in adding food to the level by adding a for element loop to look for the new coloured squares. This is explained in more detail in adding a static enemy pattern. Change the names of the variable and add in a new sprite type called Key. Change the image by drawing a key too. This is shown in the screenshot below. You can change the image of your end goal to a door also if you want to.

key and door

Add in a condtion listener that collect the key when the player touches it. We need to know if the player has the key so we will create a new variable called hasKey We will set this to 1 to show that the player has one key.

key and door

Then only progress if the player is touching the end goal and the hasKey variable has been change to one, as in the screenshot below.

key and door 4

Test your Changes and Next Steps

Test your game to check that your changes have the desired behaviour and that there are no side effects?

To check that you are making the most of this pattern you can ask yourself the following questions:

This Game Pattern is one of many allowing you to make improvements to your platform game and to learn coding and wider computing concepts. Find more on the Game Pattern page.