PROJECT 2

SPLEEF

image

ONE OF THE HARDEST (BUT ALSO FUN!) MINECRAFT MODS THAT YOU CAN MAKE IS A MINIGAME. A minigame is a game inside Minecraft.

Spleef is where a player walks around a platform and makes a block disappear by stepping on it. Using just this idea, you can make a lot of different versions of Spleef, like these:

This project shows you how to make a simple Spleef game, and helps you change Spleef to make it your own.

INTRODUCE THE GAMEPLAY LOOP

The gameplay loop is a way to help you figure out your design.

remember You should always design your code before writing it. It will keep you from getting lots of errors when you code it.

The gameplay loop has four parts:

image

START: CREATE A BASIC SCENE

Start by drawing an idea on paper or building it in Minecraft without mods. Start thinking about how the scene will affect your players. Later, when you build, you can make the arena bigger or add details like colors.

For example, a game where you use the normal Minecraft world is different from a game where you’re in a 20 × 20 arena with walls. In the Minecraft world you have never-ending space, but in the arena you only have a 20 × 20 area. You play differently depending on what kind of space you’re in.

remember The design process is iterative, which means that you repeat each step multiple times. That’s what the gameplay loop is for. Even if you make a decision early on, you can make a change later.

As you build Spleef, you might start by creating a small world. As you iterate through the gameplay loop (challenges, goals, rewards), you make the world larger — eventually making it infinitely large, as it is today.

GOAL: ADD A WAY TO WIN AND LOSE

Some games may be difficult to make at first.

tip Just break up the parts of the game according to the gameplay loop pieces. Then you can see a way to make a simple version of the game by creating a basic scene and a way to win (or lose).

For example, if you were playing a super-simple version of Minecraft in Survival mode, the goal would be to not die at night. It wouldn’t be hard to survive, because there would be no enemies. That’s when you add the simplest feature: the hearts. And you add logic to the game to ask players whether they want to respawn whenever they run out of hearts. Again, at this point the game might not be much fun, but when you reach the next steps (the challenge, followed by the reward), you start to add enemies and ways that players can earn back hearts.

CHALLENGE: MAKE IT HARDER TO WIN

Your game gets interesting when you start making it hard to beat. At first, you might want to offer a small challenge. Don’t worry: You can iterate up to the more difficult challenges as you go through the gameplay loop.

For example, in a simple game, the first challenge could be one creeper that tries to attack. This challenge makes the game more difficult. As you iterate through the gameplay loop, you can start adding more creepers or even creatures, and then add items for the player to use as defense.

REWARD: MAKE PLAYERS WANT TO WIN

Who doesn’t like to get rewards? A sticker, some candy, a No Homework pass?

Rewarding your players makes your game fun for them. If they’re not going to get rewards, what’s the point of playing?

Lots of things can be rewards:

  • Moving to the next level.
  • Items like stars and coins.
  • Coins for buying items that help them beat other levels.

In a simple game, you can give a player full health after finding an Instant Health potion. Along the same lines, if the player is playing at the Peaceful difficulty level, just staying alive and not getting hurt is rewarded with health.

PLAN AND TRY TEST CASES

Before playing your mod, figure out the different test cases to make sure that your game is working properly.

funwithcode Test cases are different ways to check your mod to make sure it’s working.

Before you write a test case, make two lists that say

If your test case passes, you know that the mod is correct. If something doesn’t do what you thought it should, debug your code.

In Minecraft, you can’t tell the game that you no longer want to trigger events.

remember To trigger an event means that Minecraft recognizes that the event has happened and then calls the function that was set up in the event call.

For example, the SetupPlayer function has an event setup block in it. When the player respawns, this event is triggered after two seconds, and then it sends the player to the middle of the arena.

But you don’t always want events to be triggered when you’re testing. For example, in Spleef you replace only the block below you with air if you’re walking on the arena’s diamond blocks. If you’re playing the Spleef mod and then decide to explore a cave, you start to fall through the ground.

You can get an event to stop triggering. You have two ways to do that:

DEBUG YOUR CODE

Debugging is another step in coding. Not even the best coders write something perfectly the first time. And when a coder — even an expert — makes a mistake, she debugs to find the mistake and fix it.

To debug means to go through code, block by block, and make sure it’s working. You might have to disable some blocks to run the mod. (Do that by clicking the block and then clicking Disable Block.)

image

tip One of the best ways to debug is to disable all the code expect for a small part, then run the mod to see if it does what it should. (Test it!) If it does work, enable one more line of code and test it again. Keep doing this until you find the one line (or block) of code that gives you the error.

You’re going to run into different errors, so I can’t fix the problems for you. I can tell you that when you find the block, you’ll probably be able to figure out what’s wrong by reading your code and comparing what’s actually happening to what you want to have happen.

MAKE SPLEEF: ITERATION 1

After you know how to use the gameplay loop, you can design and build a single-player minigame.

First, make a new mod by following these steps:

  1. Go to your home page and click Mod at the top of the page.
  2. Name the mod Spleef.
  3. Click the Blockly (Multiplayer) button.
  4. Click the mod tile that gets created.

    My mod tile says See Inside: Spleef by sarah. When you click the tile, the mod page opens.

  5. image Click the Code button.
image

Now you’re ready to make the Spleef game. The rest of the sections help you follow the gameplay loop.

EXPLORE THE ARENABUILDER LIBRARY

The scene on the next page is an arena with a fence around it. To make this arena, you’ll use the ArenaBuilder library on LearnToMod.

remember A library is a mod that’s written for you. You can use it without knowing know how it’s written.

You should tiptoe through the library to better understand how to build an arena, in case you ever wanted to build your own arena someday. If you want, you can skip to the next section.

image

To explore the library, follow these steps:

  1. Go to mod.learntomod.com/programs/sarah-ArenaBuilder.

    You see these five functions:

    • init
    • move_drone
    • ArenaWithFence
    • Platform
    • Fence
  2. Click the question mark (?) on a function.

    A comment pops up to describe the function.

  3. Look through the code and see what it’s doing.

    Notice a couple things:

    • There’s no main function, because this mod can’t run on its own in Minecraft. Mods that don’t have a main function have to be called from other mods. I explain in the next section.

      remember For a mod to run on its own, a main function has to tell Minecraft where to start. If you don’t have a main function, then Minecraft won’t know where to start and won’t work.

    • The export block lets other mods use the function. Other mods can call the functions even though they’re in a different mod. Three functions are being exported: ArenaWithFence, init, and move_drone.
  4. Now you can use the ArenaWithFence, init, and move_drone functions in your Spleef game mod.
image

START: IMPORT THE ARENABUILDER LIBRARY

After you look through the ArenaBuilder library, go back to your Spleef mod and import that library by following these steps:

  1. Under the Misc category, find the import block and drag it into the mod.
  2. Type sarah-ArenaBuilder to replace the text lib_name.

    If you enter the name correctly, the block stays green and new functions show up under the Functions category.

    The three functions you have access to are the three that were exported from the ArenaBuilder library: ArenaWithFence, init, and move_drone.

  3. Create a main function.

    To do this, click the Functions category and drag a function block from the sidebar into the programming area. Then click where it says do something, delete that, and type in main.

  4. Add a call to the init function from the ArenaBuilder library.

    To do this, click the Function category and drag the sarah-ArenaBuilder.init function into the main function that you created in Step 3.

    Now Minecraft knows where to start reading the code.

    This code shows you how to set up your ArenaBuilder.

  5. Add a call to the ArenaWithFence function from the ArenaBuilder library to the main function.

    To do this, click the Functions category and drag the sarah-ArenaBuilder.ArenaWithFence function into your main function.

  6. Fill in the five spaces where you can connect other blocks to this function.

    They’re called parameters.

    • Set FenceHeight to 5. Numbers are under the Math category.
    • Set PlatformSize to 20.
    • Set PlatformWidth to 1.
    • Set ArenaMaterial to type DIAMOND_BLOCK. To get to it, click the Minecraft category and then click Materials [D-G].
    • Set FillMaterial to AIR. To get to it, click the Minecraft category and then click Materials [A-F] category.

    This code makes a 20 × 20 arena made of diamond with a fence that’s five blocks high and a platform width of one block. The arena will be filled with air. This makes the arena where you’ll play Spleef.

  7. Make sure that the mod is saved, click the Mod button, and test the code in Minecraft.

    You see your scene.

    remember Because you’re an ace at following instructions, you already finished the LearnToMod Run Your Mod badge to learn how to test your code inside of Minecraft.

  8. Refactor the code:

    • Create a new function (the same way you created your main function) and call it Arena.
    • Move the call to sarah-ArenaBuilder.init and sarah-ArenaBulding.ArenaWithFence into the Arena function.
    • Go to the Functions category and drag the call to the Arena function into your main function.

    In this case, you’re refactoring the code so that the arena is built in a function named Arena and so that the arena function is called from main.

    Congratulations: You created the basic Spleef scene! That’s a nice reward, huh?

remember Refactor means to change it without changing what it does.

image
image
image
image
image
image
image

GOAL: MAKE A WAY TO WIN AND LOSE

When you have an arena, it’s time to make a way for the player to win and lose. The easiest versions of the goal for Spleef are these:

  • Win: You win if you stay on the diamond platform.
  • Lose: You lose if you fall through the platform.

It’s impossible to lose right now because players don’t make the blocks below them disappear. However, you can still make the players fall through the platform.

First, make sure that players start in the right place and know what to do when they respawn. The steps in this section help you do this. (The next section adds a challenge: Blocks disappear one second after being touched.)

To set up the winning and losing conditions for Iteration 1 of Spleef, follow these steps:

  1. Add a call to the ArenaBuilder’s SetArenaCenter function at the end of the Arena function.

    remember The SetArenaCenter function is in the Functions category because you imported my ArenaBuilder mod, and I exported the SetArenaCenter function.

    This step finds the middle of the arena so that the player can be moved there for the start. You can see the added call to SetArenaCenter in the Arena function.

    SetArenaCenter also creates a melon block, which can start the game. The melon block is on the platform. Make sure you see it before moving on.

  2. Make a new function named StartGame that takes info as a parameter.

    The game starts when the player breaks the melon block.

  3. Call StartGame.

    You can see that the StartGame function is called only when a player breaks a block. This function runs every single time a player breaks a block. Now the player has to step on the blocks.

  4. Run the mod in Minecraft.

    Make sure that when you break the melon block, you’re put into Survival mode.

  5. Set up the respawn function.

    A player who dies respawns (comes back to life) to the center of the arena.

    • Create a new function called SetupPlayer (just like you create your main function).
    • Click the star next to your function and drag an input name block into the function.
    • Change x to info and click the star again to close the parameter box.
  6. Figure out the name of the player that just respawned.

    • Make a new variable called respawn_player.
    • Click the Variables category and drag a Set item to block into the SetupPlayer function.
    • Click the drop-down menu next to the word item and click New Variable. Name it respawn_player and click OK.

    Your work should now look like what you see in this code.

  7. Under the Misc category, drag an item’s default block and place it inside the respawn_player variable that you just created.
  8. Choose info from the item drop-down menu and replace default with Player.

    To get the player’s name, do the same thing you did to get the player, but name the variable player_name and get respawn_player’s DisplayName.

  9. Drag the do function after block from the Minecraft/Events category, a function block from the Misc category, and a number block from the Math category.

    Change the number to 2000. Arrange them like you see in this code.

  10. From the Entities category, drag a Teleport block into the function block and teleport me (in the Player category) to sarah-ArenaBuilder.GetArenaCenter.

    You’ll find it in the Functions category.

  11. Grab a js block from the Misc category and type in on_respawn(player_name), like you see on the next page.
  12. Set up the respawn event.
  13. Add a js block (from the Misc category) to your main function and type it just the way it is here: events.when(“player. PlayerRespawnEvent”,SetupPlayer,me,false)
  14. The JavaScript block calls the event PlayerRespawnEvent, which requires these parameters:

    • The function to call when the player respawns.
    • The player who is respawning.
    • A true or false value that tells the mod whether this is a BedSpawn. (Use the false value because you don’t want to teleport back to your bed.)

    The SetupPlayer function gets data from the info parameter, but this time the info parameter is a player, not a block.

    remember Events can be tricky, but if you need help you can always review badges that you’ve already earned or ask questions on the LearnToMod forums.

  15. Run the mod and break the melon block.

    When you do that, the game mode should switch to Survival.

  16. Break a diamond block and fall.

    Two seconds after you respawn, you’re in the arena again.

image
image
image
image
image
image
image
image
image

You have a game! But unless you break a diamond block, you never lose. In the next section, I help you find a way to make the game more challenging.

CHALLENGE: MAKE BLOCKS DISAPPEAR ONE SECOND AFTER TOUCHING THEM

To add the first challenge to the game, you need to create another event. This time, when the player moves, a function is called that finds the location of the player. It replaces the block below the player with an air block.

  1. Add the removeBlockAfterStep function.
  2. Add the removeBlock function.
  3. Make a call to them from the main function.

    Now the entire Spleef mod should look like what you see on the next page.

  4. Test out the game.

    Try these sample test cases for the Spleef mod:

    • The scene sets up correctly: When you run the mod, two arenas appear: a brick arena with lava and a diamond arena with a tall fence.
    • The player is put into a different mode when the melon block breaks: When you break the melon block, the player is set to Survival mode.
    • Blocks disappear: One second after you touch a block, it disappears.
    • Lava changes player health: When the player falls into the lava, he loses all his health. (This should be true because the player is in Survival mode, but you should still check that it happens.)
    • Respawn back to arena: Two seconds after the player respawns, she automatically returns to the arena.

    Remember earlier in this project, when you used a conditional statement to check if the player broke the melon block: If one thing is true, do Step A, and if it isn’t true, do Step B? You can add a conditional statement that makes sure you’re walking on a diamond block before it changes to an air block.

    You can see the blocks you need to add to the removeBlock function to check the type of block you’re walking on.

image
image
image
image
image
image

REWARD: GIVE THE PLAYER POINTS

Points can be the number of blocks that the player destroys before falling through the platform. You just have to count the number of blocks that get converted to air.

Follow these steps:

  1. Add to the StartGame function a new variable named blocksDestroyed and set it to 0.
  2. Add 1 to the blocksDestroyed variable.

    You should add 1 to the blocks_destroyed variable every time you place an air block in the removeBlock function. That’s how you’re destroying blocks — you’re replacing them with air.

  3. Add a Send message block to the SetupPlayer function.

    That lets a player know how many blocks she destroyed before falling through the platform.

    Now when you play the Spleef game, a message appears after you respawn.

    You finished an entire iteration of the gameplay loop for your Spleef game. Flex — you showed some real Minecraft muscle.

image
image
image
image

MAKE SPLEEF: ITERATION 2

After you do one iteration of the Spleef game and play the heck out of it, you might be ready to repeat the gameplay loop and do something more challenging.

In this section, I walk you through four examples in each part of the gameplay loop so that you can

START: ADD A LAVA PLATFORM

After you have a working game, you can add lava to the scene. Everyone knows what happens when you fall into lava.

Because you’re using the ArenaBuilder library, you don’t have to make many changes to the Spleef code. You can see here what call to add to the arena function. When you add it, you get a different scene.

image

GOAL: DESTROY AT LEAST 200 BLOCKS

Your mod already counts the number of blocks that the player destroys. You can reward your players even more by adding to the removeBlock function. Congratulate the player on becoming a Master Spleefer.

image

CHALLENGE: ADD AN ENEMY

Nobody needs enemies, but they sure do make games more lively. You can add an enemy to the arena by using the melon block as the spawning point.

Spawn a creeper in a random spot in the arena. Then the player has to avoid the creeper and try not to fall before destroying 200 blocks.

image

REWARD: ADD FIREWORKS

You’ve challenged your players plenty, so make sure they get plenty of rewards. Besides congratulating players, you can give them a fireworks show!

Add the Fireworks block to the removeBlock function.

image

The Fireworks block is under the World category.

image