Dungeon Game

Step 1
We're going to build a dungeon game!

Before starting this lesson, you should:

Step 2
Stand at the start

We're going to teleport the player to the entrance of the temple. To find the correct coordinates, get on your server and enter the temple. Make sure you are facing the direction you want the player to.

Look slightly up, at the top of the fountain

Step 3
Find the Dungeon Entrance coordinates

Press F3 to see the Minecraft debug info. We need the XYZ coordinates of where you are standing right now, as well as the Pitch and Yaw.

  • Pitch: The angle up and down of where you are looking
  • Yaw: The angle side-to-side of where you are looking
Step 4
Define the starting variables

Make the starting variables. We'll need playerOne later.

Step 5
Make a function to start the game

We're going to make an export function to start the game. All it does right now is make the player whoever calls the function, say that the dungeon has started, and teleport that person to the entrance.

Check Your Work
Step 6
Test your startDungeon() function

Save, deploy, reload and move away from your Temple.

Then, call

/js startDungeon()

in the game to see if it teleports you to the correct spot.

Step 7
Make an export function to stop the dungeon game

Make another export function called stopDungeon().

The only thing it needs to do right now is set the dungeonStarted variable back to false.

Step 8
Carve out the door to the first room

We didn't build the doors to the rooms into our drone code, so you'll need to add them now.

Once you are teleported into the temple, turn to your left, and punch out a door. This is the entrance to the first room.


Once you enter the room, turn to your right, and punch out a door to the next room. This will be the first room's exit.

You're probably going to want to add some torches. It's dark in here!

Step 9
Find 5 locations in the room

Get out a pen and paper!

We need five different location values to do everything we need to do in the room. We'll need the entrance and exit to seal the doors, like in Escape Room, and two diagonally opposite corners to tell if the player is in the room. The center location will be where we spawn monsters.

The color-coded wool blocks help us see which location is which, but you can place down any block.

Temp1529264231 Webp.net gifmaker %281%29
Step 10
Store your location data in an object

Make an object called room1 and store all your locations as properties of that object.

You can remove the wool marker blocks once you have all your locations recorded

Step 11
Make export functions to seal and unseal the doors!

Make two export functions, sealDoors() and unsealDoors() that put iron bars over the room's entrance and exit using the Drone.


If you're stuck, check out how you did this in the Escape Room

Step 12
Make a sound when the bars close!

Use the sounds module to play the iron trap door closing and opening sounds when the bars close and open.

Here's what the sounds are called in the Sounds module:


Check out your Sounds in ScriptCraft lesson if you need a hint

Step 13
Checking if the player is in the room

Just like in Escape Room, we're going to make a function to test if the player is in the room.

You may recall that that function was very long. And repetitive.

Check out all the places where this function says the exact same thing:

Step 14
You can make code shorter using "||" ("or")

Whenever you see two "if" statements that do the same thing, you can usually combine them with ||

Check out this example:


In this program, if it's Monday, it tells you to go to school. If it's Tuesday, it tells you to go to school.

But really, you should go to school if it's Monday OR Tuesday, right?

In fact, you should go to school on any weekday . . .

Step 15
Make an export function to see if the player is in the room

Make a shorter version of the inEscapeRoom() function.

We need to:

  • make a variable for the entity's location
  • make 3 arrays, one for the X's of the corners, one for the Y's, and one for the Z's
  • make sure those are sorted so that the smaller one comes first
  • if the entity's X is smaller than the smaller one, or larger than the larger one, say that it is not in the room
Step 16
Add logic for Y and Z

Change your inRoom() function to return false if the entity is outside the Y or Z coordinates, too.

Step 17
Check the player's position

Make a function called checkPlayerPosition() that is called in the playerMove() event.

It should tell us true or false for whether the player is in room1, whenever they move.

Check Your Work
Step 18
Test your functions

Save, deploy, reload, and move around your temple. You should see false most of the time, and true only when you are inside the first room.

Step 19
Make the doors seal when a player enters the room

Delete the broadcast message from inside your checkPlayerPosition() function, and make it seal the doors if the player is in the room.

Check Your Work
Step 20
Test out your trap!

Save, deploy, reload, and enter the room.

The doors should close, but do you notice anything strange?

If you have a sound on, you should hear the iron bars clank over and over again, every time you move.

We need to find a way to make the doors seal only ONCE

Step 21
Add a "sealed" property to your room

Add a new Boolean property to room1 to tell us whether or not it is sealed. It should start out as false

Don't forget to add commas between all your properties!

Step 22
Make the doors seal only if they haven't already

To make the doors only seal once, we'll add an if statement around the code so that it only happens if the doors aren't already sealed.

Then, we'll mark the room as sealed by setting the room1.sealed property to true, so that we know not to do it again.

Remember that ! means "not"!

Step 23
Do the same for unsealDoors()

This time, you should check if a room is already sealed, and change sealed to false again once it has been unsealed.

Step 24
Make the doors open when the dungeon game is stopped

Call your unsealDoors() function in your stopDungeon() function, so that you can get out of the room.

Step 25
Make sure the doors only seal when the game has started

Add a new condition to your if statement in the checkPlayerPosition function that makes the doors only seal if the player is in the room AND we've said to start the dungeon game.


If we haven't started the game, or the player is not in the room, the doors will not close

Step 26
Add two more Boolean properties to your room

Add two more properties to your room, started (so we can mark when the player first enters the room) and finished (so we can mark when they have done everything they need to do in it).

They should both start out as false, because when the game begins, they haven't started the room yet, and they certainly haven't finished it.

Step 27
Make a monster appear!

Add this code to spawn a zombie when the player first enters the room.

If we skipped the if, the room would spawn zombies over, and over again, the same way it tried to seal the doors over and over again.

Check Your Work
Step 28
Test out your monster appearance

Save, deploy, reload, and call startDungeon(). Then, run into your room and see if a zombie spawns when you get there.

Step 29
Add properties to your room to count the monsters

We need to add two more properties to the room: monstersIn will count how many monsters there currently are in the room, and monstersTotal will count how many we want to spawn.

Step 30
Use a loop to spawn the correct number of monsters

Edit your spawning code to be inside a while loop that continues generating zombies until there are the correct number.

We also need to make monstersIn count up 1 for each monster spawned, to keep track of them.

Check Your Work
Step 32
Make the room spawn 3 zombies

Change your monstersTotal property to 3 and test it on your server. You can try higher numbers if you want!

Step 33
Count down the monsters killed

Next, we need to subtract 1 from monstersIn each time a monsters dies.

Make a function called onMonsterDeath that checks if the entity that died is in the room and is a zombie, then subtracts one from monstersIn.

We'll call this function during the entityDeath() event.

Step 34
Mark the room finished when there are no more monsters in it.

We're going to add a new section to our checkPlayerPosition() function, that checks if the room has already started but not finished yet. Then it checks if there are no more monsters, and broadcasts a message.

Step 35
Can you think of a problem with this?

There are actually two problems with the code so far:

  1. Mobs spawn randomly in Minecraft. If an extra zombie got in the room and we killed it, it would also subtract one from monstersIn even though it wasn't in our total.
  2. Our inRoom() function doesn't always count you as in the room if you are on the edges. And zombies love edges. You could kill a zombie but not subtract one from monstersIn
Step 36
Find the zombie's ID

Mobs have a unique ID number given to them when they spawn. Change your checkPlayerPosition() code to broadcast that ID when we spawn the zombies.

Check Your Work
Step 37
Check the ID's

Save, deploy, reload, and startDungeon(). When you enter the room and zombies spawn, you should see three sequential numbers.

Step 38
Add an array of ID's to the room

Make a new property called monsterIDs for room one. It starts out as an empty array ([]), that we can turn into a list of each zombie's ID as we spawn them.

We also want to set monstersTotal back to 1.

Step 39
Save the ID numbers

Change your bukkit.broadcastMessage to room1.monsterIDs.push to store each ID number in your array.

Remember that push() just adds items into arrays.

Step 40
Check if a dying zombie's ID matches

In the onMonsterDeath() function, add an if statement that checks if the monster that died has an ID that matches the one you saved. We'll add a broadcastMessage to confirm.

We don't need to check if the zombie is in the room, anymore, so take that part of the code out.

Check Your Work
Step 41
Go back and kill the monster

Save, deploy, reload, start the dungeon and go back into the room to kill the zombie. When it dies, you should see the message "room monster" confirming that its ID matched the saved one.

Step 42
Use a loop to check all the ID's

So far, when a creature dies, we check its ID against the first one in the room1.monsterIDs array.

Change the code in your onMonsterDeath() function to use a for loop to check each ID in the room1.monsterIDs array.

Step 43
Release the player!

Now that we can check when all the monsters are defeated, we can unseal the door! Edit your code to make a chime sound, broadcast a message, unseal the doors, and mark the room as finished.

Step 44
You've finished the first room!

You have now completed the first room of your dungeon game! In the next lesson we'll learn how to use a class to make four more rooms like this, as well as changing the way the game starts, and giving it a way to end.

Dungeon Game Info


MVCode Clubs

Created By

Mandala2 slinkous


Temple Dungeon

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach