PROJECT 3

MONSTER ARENA

image

MONSTER ARENA IS A MULTILEVEL MINECRAFT MINIGAME WHERE THE PLAYER IS IN A LARGE ARENA WITH MONSTERS THAT SPAWN AROUND A MELON CUBE BLOCK. The player’s goal is to reach the melon cube without being attacked by the monsters. Every time the player reaches the melon cube, he moves on to the next level, which has more monsters that are harder to beat.

In this project you make a Monster Arena mod. Then you can play Monster Arena with your friends and see who can defeat the most monsters!

DRAW THE GAMEPLAY LOOP

Draw the gameplay loop (see the next page) for the Monster Arena game. It will work just how it did in the project before this one.

image

This project helps you make each iteration of the gameplay loop harder to beat. To start, you have a simple arena with one melon block, one monster, and one level.

funwithcode Iterate means to do something over and over. Every time you do that something, you make progress. For example, if you iterate on the gameplay loop, you add a feature (like a new challenge or reward).

ITERATION 1: MAKE MONSTER ARENA

Here’s one way to tackle the first iteration of the gameplay loop:

  1. Start: Create a basic arena enclosed by a fence.

    In Monster Arena, the arena is round. A large fence surrounds the arena to keep the player and monsters inside it.

  2. Goal: Add a melon block to break.

    For this minigame, you also need to write code to reset the arena when the player breaks the melon block.

  3. Challenge: Add monsters to the arena.

    You add one monster to the arena, and the player has to avoid the monster while trying to break the melon block. If the player breaks the melon block, the game resets.

    tip Ask your friends to test your game after you add monsters. They’ll probably say that it isn’t hard to play, but they can tell you what kinds of challenges they like. Do they want more monsters or a bigger arena?

  4. Reward: Replay the first level.

    In the first iteration, the user who breaks the melon block is the one who gets to play Level 1 again. In Iteration 2, you add more levels.

    After you plan out the gameplay loop in Steps 1–4, it’s time to build Monster Arena.

    remember Whenever you build any game, the first thing you need is a basic scene.

image

START: CREATE A BASIC ARENA WITH A FENCE

You may already know that the radius is the line from the middle of a circle to any point on the circle’s edge. Everything in Minecraft is made with blocks.

tip Just count the blocks between the center and the edge of the circle — that is the arena’s radius.

Monster Arena needs this stuff:

  • A place for the player and monster to run around
  • A way to make sure that the monsters and player can’t leave the area

For this basic scene, you create

  • A platform
  • A fence

In the Spleef project you built an arena using the LearnToMod library named sarah-ArenaBuilder. That library makes square arenas. This time you need a round one. Use WorldEdit commands to make the arena in LearnToMod!

A WorldEdit command can run in Minecraft to edit the world.

tip You can explore lots of WorldEdit commands online. Search for the term minecraft worldedit commands using a search engine. Visit wiki.sk89q.com/wiki/WorldEdit (the official WorldEdit wiki) to see a list of all WorldEdit commands for Minecraft. Just click a link to a category (like Filling Pits, which is under Utilities).

warning WorldEdit commands work when you’re connected to the LearnToMod Minecraft Server. Follow the instructions on the Minecraft Connect badge if you don’t know how to connect to the LearnToMod Minecraft Server.

TEST WORLDEDIT COMMANDS IN MINECRAFT

Before you make the arena, test out the WorldEdit commands in Minecraft.

First you type the code to make

  • a platform
  • made of stone
  • with a radius of 20

Type this: //cyl stone 20. Then you see the platform.

image

remember When you’re trying to build a large structure, double-click the spacebar to hover, and then click your mouse and hold the spacebar to move up into the sky before running your mod. This action creates the large platform in the sky and makes it easier to see the arena.

Try making the fence with WorldEdit commands. If you don’t move, you can make a wooden fence inside the stone fence. Just enter the code that follows the kind of fence:

  • Stone fence: //hcyl stone 20 4
  • Wooden fence: //hcyl fence 19 4

warning If you move after you create the platform and before you create the fence, your fence won’t fit perfectly around the platform.

image

If you call //cyl stone 20 and //hcyl stone 20 4 and //hcyl fence 19 4 without moving, you wind up with an arena with a fence around it.

funwithcode Calling code means that Minecraft will handle that task. You don’t have to build the cylinder by hand, one block at a time.

image

MOD THE ARENA IN LEARNTOMOD

After you test the WorldEdit commands, call them from the LearnToMod mod. Crazy idea — I know.

remember To call WorldEdit commands from LearnToMod, you have to use a PerformCommand block. It’s under the Players category.

image

When you use a PerformCommand block, the first slash mark (/) is already included, so you need to have only one slash mark to make the platform and fences. You can see here what the main function looks like for the initial scene.

image

Test your mod to make sure that it’s the right kind of arena.

REFACTOR: MOVE THE ARENA CODE TO A NEW FUNCTION

Before you add a goal, refactor your code so that the main function stays simple. You can see here how to refactor your code to clean up the main function.

image

remember Refactoring code means to change the way it looks without changing what it does. Refactoring is useful for you as a coder because it can make it easier for you to understand the code later on.

GOAL: ADD A MELON BLOCK TO BREAK

The arena function stays the same, but now add a melon block on the opposite side of the arena from the player.

Here’s the new function, SetupGame, which places the melon block and puts the player in the starting position. The next steps help you make the SetupGame function.

image

To add a melon block, follow these steps:

  1. Create the SetupGame function and add a call to SetupGame to main.
  2. Add a new drone named d and move it to one side of the arena.

    The drone should move only 18 blocks because the stone and the fence take up 2 blocks, and you want the player inside the fence.

  3. Move the drone up by 2.

    This makes the player start above the platform and fall into the arena.

  4. Move the player to the location of the drone.

    Now the player’s at the starting spot.

  5. Move the drone 33 blocks to the opposite side of the arena.

    That leaves two blocks empty between the melon block and the fence.

  6. Place the melon block and move the drone backward by 1 block.

    For right now, nothing happens when the player breaks the melon block. You write that code in Iteration 2.

TEST: MAKE SURE YOUR GAME WORKS

Test your mod by dragging it from your mod chest into your HotBar and then clicking to run it. You should see this scene when you run the mod.

image

CHALLENGE: ADD MONSTERS TO THE ARENA

After your game is all set up, you can start making it playable. On the first iteration of the gameplay loop, add one monster for the player to beat (or to avoid, at least).

This time, write the code to spawn the monster in a separate function from the beginning. This code makes a monster spawn near the melon block. The drone is at the melon block, so it moves backward (toward the player) and then spawns the creeper.

image

tip Before you spawn the creeper, move the drone 1 block away from the melon block. That way the creeper doesn’t spawn inside the block and just die.

Now when you test your game, you see a screen like this. That’s one nasty monster!

image

REWARD: REPLAY THE FIRST LEVEL

In other iterations you reward players with harder levels. (Who’d think of a harder game as a reward?) For this first iteration, however, just let people replay the first level.

First you make a function that gets called when the break block event happens. The on_block_break function checks to see if the melon block was broken. If it was, you see the message “Yay! You broke the Melon Block!”

Follow the steps to make this function:

  1. Create a function and call it on_block_break (like you did to create the SetupGame function).
  2. Create a variable called event_block and put an info’s block inside the variable.

    The event_block is in the Variables category. The info’s block is in the Misc category.

  3. Make a variable called event_block_type and put an event_block’s type inside it.
  4. From the Logic category, drag an if block and an = block and compare the event_block_type variable to the MELON_BLOCK (in the Materials H-M category).
  5. Send this message: Yay! You broke the Melon Block!

    Next, you’ll actually call the on_block_break function when the player breaks a block.

  6. Click the Minecraft and then Events category and drag a do function when happens block and put it in your main function.
  7. Under the Misc category, grab the function block that has a drop-down menu and put it in the first empty spot in the do function when happens block.
  8. Click the down arrow to change the function to on_block_break.
  9. Grab an Event block from under the Minecraft and then Events category. Change the event to block_break by clicking the blue down arrow.

    You’ve made an event that calls the on_block_break function whenever the player breaks any block. This code shows how to add this event call into the main function.

    remember If you don’t remember how to do some (or any) of these parts, look at the pictures in this book and go through all the categories until you find the right blocks. You can also post on the forum at forum.learntomod.com and ask for help, or go back and earn badges under the Learn tab at mod.learntomod.com.

    Now you want that person to restart the level.

  10. Add two variables to the SetupGame function:

    • One keeps track of where the player should start out.
    • One puts down the melon block.

      This code shows how to add these two variables.

  11. Write a ResetGame function that does this stuff:

    • Destroys all monsters in the arena
    • Teleports the player back to the starting position
    • Moves drone d back to the melon block’s starting spot
    • Makes a new melon block
    • Spawns monsters again

    This code shows the new function.

    This code shows how to call it from the on_block_break function.

image
image
image
image
image

TEST: ITERATION 1 COMPLETED

After you finish the four parts of Iteration 1, you can test your game and make sure that everything works. Your code should look like this.

image
image

remember When you’re testing, try a few test cases. That makes sure that you’ve tested everything you’ve written. (Project 2 explains testing.)

ITERATION 2: ADD LEVELS

On Iteration 2 of the Monster Arena gameplay loop, you can

START: MAKE THE ARENA UNIQUE

Add some designs to the arena floor. Take your time. Be creative. Use the platform or the fences. You might even want to add a ceiling to the arena!

I used two //hcyl WorldEdit commands to make one hollow cylinder (tube) of glass and one of diamond.

image

You can see the code for changed arena function.

image

tip If you add a ceiling, start your player inside the arena. Otherwise, that person will be locked out.

GOAL: WAIT UNTIL A LATER ITERATION

Sometimes when you’re going through the gameplay loop, you don’t want to change one of the sections — like when your goal is still the same. No problem. You don’t have to make any changes to the goal this time around.

Kick back. Relax for a minute.

CHALLENGE: ADD MONSTERS

Moving around one creeper isn’t the hardest thing in the world. It’s time to add more monsters.

By making one small change to the spawn_monster function with the number 5, you can spawn five creepers instead of one. Now your player is outnumbered!

image

REWARD: ADD A SECOND LEVEL

In Monster Arena, levels are different based on what monsters there are. To add a second level, follow these steps:

  1. Create a variable named Level. Start the variable at 1.

    It keeps track of which level the player is on. It starts at 1 because the player starts on the first level.

  2. Make a list of monsters.
  3. Name the list Monsters.
  4. Add two types of monsters: creepers and zombies.

    This code shows how to create these two variables in the SetupGame function.

  5. Change the spawn_monster function to spawn a different monster depending on what level you’re on.

    If you change your code in your spawn_monster function to look just like the following code, the monsters change based on the level.

    The code you change chooses the correct item from the list.

  6. Increase the level variable every time the player breaks the melon block.
  7. If the player reaches the final level (right now that’s Level 2), tell her that the game is finished. If she hasn’t reached the last level yet, this game should reset with the new monsters.

    The code on the next page shows what changes you have to make in on_block_break that need to happen to start the new level.

image
image
image

TEST: MAKE SURE BOTH LEVELS WORK

After you add the changes to the spawn_monster function, test your mod. When you start the test, you should see five creepers.

If you break the melon block, the game resets and you see five zombies.

If you break the melon block again, you see the message “Yay! You beat all of the levels! Good job!

image

ITERATION 3: ADD LEVELS TO YOUR LEVELS

After you start getting the hang of making the Monster Arena minigame, you can feel good adding a few more levels and challenges.

tip You can skip the Start and Goal sections in Iteration 3. The game is becoming fun because you’re adding more challenges and rewards.

CHALLENGE: SWITCH TO SURVIVAL MODE

You may have noticed that it’s pretty easy for a player to get past all the creatures. That’s because the player is in Creative mode.

  1. Add a perform command block.

    The code shows the changes you make to the SetupGame function to do this. You only change the last block of the function — just add the perform command block.

    When the game starts, the player goes into Survival mode.

    Now that you’ve made it easier for the player to die, you should include an event that happens when the player respawns.

  2. Change the main function and create the new RespawnPlayer function.

    tip Look at code in the book and compare it to your code. You should be able to figure out what’s missing in your code and how to add it. But don’t worry! If you ever get stuck, just go to forum.learntomod.com and ask for help!

  3. In the main function, add a js block (found in the Misc category) that has this code in it: events.when(“player.PlayerRespawnEvent”,RespawnPlayer,me,false).
  4. Create a new function called RespawnPlayer. It should look exactly like this code.

    I’ll let you figure out how to make it by looking at the correct code here in the book. Create it from scratch. I know you can do it! After you have made the new RespawnPlayer function and made the changes to the main function, the player teleports back to the arena when she respawns.

image
image

REWARD: ADD FIVE MORE LEVELS

Adding levels is pretty easy because you already set up the levels to be based on a list. To make more levels, follow these steps:

  1. Change the conditional statement so it’s based on the list length.

    remember To refactor means to change the way code looks without changing what it does.

    You’re making sure that you iterated through all the monsters in the Monster List (instead of a specific number, like 2).

    The code shows how to change the conditional statement to have it based on the list length. Figure out the one place you should change your code. (Hint: You aren’t checking if Level is less than 2. You’re checking if Level is less than the length of a list.)

    remember A conditional statement is one that compares two things. You can read it like a sentence. For example, if level is greater than the length of monsters, then send a message; otherwise, reset the game.

  2. Change the SetupGame function to add monsters to your Monster List.

    The code shows the updated SetupGame function. Add entities to the Monster List. You can find them under Minecraft and then Entities. Change which one you’re adding by clicking the yellow arrow and choosing from the list.

    remember To add more positions into a list, click the star on next to the words create list with and drag item blocks into the list block.

  3. Change the spawn_monster function.

    This code shows how to spawn more monsters as the levels get harder.

image
image
image

MAKE MORE ITERATIONS: BE CREATIVE AND UNIQUE

If you followed the steps in this project, you created an entire multilevel Monster Arena minigame! (Yay! You made so many levels! Good job!)

Keep iterating on the gameplay loop to make more challenges, new rewards, and different goals. You can change the arena by adding patterns to the platform.

tip A more challenging addition is to add random monsters at each level.

For example, at Level 1 you add five creepers, and at Level 2 you add ten monsters that are (randomly) creepers or zombies. At Level 3, you add a total of 15 monsters that are (randomly) creepers, zombies, or spiders.

This code shows how to add randomness to the spawn_monster function.

image