PROJECT 2
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.
The gameplay loop is a way to help you figure out your design.
The gameplay loop has four parts:
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.
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.
Some games may be difficult to make at first.
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.
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.
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:
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.
Before playing your mod, figure out the different test cases to make sure that your game is working properly.
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.
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:
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.)
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.
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:
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.
Now you’re ready to make the Spleef game. The rest of the sections help you follow the gameplay loop.
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.
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.
To explore the library, follow these steps:
Go to mod.learntomod.com/programs/sarah-ArenaBuilder
.
You see these five functions:
Click the question mark (?) on a function.
A comment pops up to describe the function.
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.
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.
After you look through the ArenaBuilder library, go back to your Spleef mod and import that library by following these steps:
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.
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.
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.
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.
Fill in the five spaces where you can connect other blocks to this function.
They’re called parameters.
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.
Make sure that the mod is saved, click the Mod button, and test the code in Minecraft.
You see your scene.
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.
Refactor the code:
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?
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:
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:
Add a call to the ArenaBuilder’s SetArenaCenter function at the end of the Arena function.
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.
Make a new function named StartGame that takes info as a parameter.
The game starts when the player breaks the melon block.
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.
Run the mod in Minecraft.
Make sure that when you break the melon block, you’re put into Survival mode.
Set up the respawn function.
A player who dies respawns (comes back to life) to the center of the arena.
Figure out the name of the player that just respawned.
Your work should now look like what you see in this code.
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.
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.
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.
The JavaScript block calls the event PlayerRespawnEvent, which requires these parameters:
The SetupPlayer function gets data from the info parameter, but this time the info parameter is a player, not a block.
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.
Run the mod and break the melon block.
When you do that, the game mode should switch to Survival.
Break a diamond block and fall.
Two seconds after you respawn, you’re in the arena again.
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.
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.
Make a call to them from the main function.
Now the entire Spleef mod should look like what you see on the next page.
Test out the game.
Try these sample test cases for the Spleef mod:
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.
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:
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.
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.
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
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.
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.
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.
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.
The Fireworks block is under the World category.