Project 6
Making Minecraft minigames can be a lot of fun because after you make it you can play games inside of Minecraft for hours, but what is even more fun is making minigames that you can play with your friends. In this project, I show you how to convert single-player games (games that only have player) into multiplayer games (games that have more than one player). I also explain how to change the gameplay loop (for the single-player Spleef game from Project 3) so that you can design a multiplayer game from the ground up.
Rather than remake the entire Spleef game, you can start from the single-player version and turn it into a multiplayer version.
Note: Before you begin this section, you should have already built Spleef. If you haven’t built it yet, go back to Project 4 and build a single-player Spleef game.
To start from single-player, follow these steps:
Now you’re ready to start modifying your single-player version of Spleef to make it multiplayer.
Even though you’re making a multiplayer game, you can still use the gameplay loop to guide you in making small changes. At each iteration, you can test to ensure that you haven’t made any mistakes. This makes reverting to earlier versions easier because you’re making only small, incremental changes, and because they’re small, they’re easier to undo.
Before you start iterating on the gameplay loop, summarize on paper the changes you plan to make, like this:
The new gameplay loop should be simple and not have a lot. After you successfully make the multiplayer game, you can always revisit the gameplay loop and make it more interesting, such as making the arena bigger.
Before you add a second player to the game, you need to do a bit of refactoring. Instead of referring to me
, you should refer to a specific player who is defined in your mod. In this section, I walk you through the steps to do it — while setting up for the multiplayer version.
First you need to determine the Minecraft username of Player1 and put that info into your mod. Follow these steps:
Make a new function named SetupMultiplayer, and call it from main
, as shown in Figure 6-7 (and described in Project 2).
The main
function already has other blocks in it because you’re starting from the Spleef game I show you how to build in Project 4.
thoughtstem
as Player1.Review your mod and replace any references to me
with references to the player with the name of the Player1
variable.
For example, if your Minecraft name is thoughtstem
, Figure 6-9 shows three ways that you can reference your player in Minecraft.
Start with main
(as shown in Figure 6-10) and step through it line by line. This means you will look at the functions when they are called from main
.
Since the first function call in main
is SetupMultiplayer
, look at that function.
SetupMultiplayer
, shown in Figure 6-8, is the one I made for my multiplayer version, so nothing needs to change.
main
is the call to the arena
function. Figure 6-11 shows the arena
function.arena
function only builds the arena. It has no indication of players in the game, so nothing needs to change here, either.main
is the event that is triggered when a block is broken. When any player breaks a block, the StartGame
function is called.StartGame
function, change the me
block to a Player named Player1
block, as shown in the yellow box in Figure 6-12.main
is the event that is triggered when a player respawns. When any player respawns, the SetupPlayer
function is called.SetupPlayer
function, replace the two me
blocks with the Player named Player1
block, as shown in Figure 6-13.if
statement to ensure that the player who respawned was Player1 and not any other player in the Minecraft world (see Figure 6-14). This is in preparation for later in this project, when I show you how to add other players to your Minecraft world.In the respawn event line in main
(see Figure 6-15) is a reference to me
. It doesn’t have to be changed, because me
is the player who launched the mod. Regardless of who is playing the multiplayer Spleef game, the game-launcher (me
) is the only one who finds out whenever anyone respawns (this is also described as me
receiving the event).
The final line in main
is the event that is triggered when a player moves.
Change the final line so that Player1 (instead of me
) triggers this event.
Figure 6-16 shows the change you make to main
.
When any player moves, the function removeBlockAfterStep
is called.
Change the removeBlockAfterStep
function to ensure that it was Player1 who moved, as shown in Figure 6-17.
First get the name of the player who moved, and then use an if
statement to ensure that it was Player1.
removeBlockAfterStep
function calls the removeBlock
function. In this function, you’re sending a message to me
, but you want to send it to the player who just made a block disappear.Add a parameter named p_name
, and send the function the name of the player who just moved.
Figure 6-18 shows how to change removeBlock
and removeBlockAfterStep
.
Figure 6-19 shows the entire code block you now have for your Spleef multiplayer mod.
Test your mod to make sure that it behaves exactly as the original Spleef game (described in Project 4) behaved. There should be no changes to how you play and what happens during play; it’s still the single-player version of Spleef.
After Iteration 1 (making your Spleef game independent of the me
block) you can move on to Iteration 2 and add in another player so that your game becomes multiplayer. In this section, I walk you through the exact same process as the section “Iteration 1: Refactor the Single-Player Version.”
To add Player2, follow these steps:
Start with main
and look at the first function, SetupMultiplayer
. Add another variable named Player2
, and put the Minecraft username of the second player there.
Figure 6-20 shows an example where the two players are thoughtstem
and thoughtstem2
.
It’s difficult to test the multiplayer version of Spleef without having a real-life second player to join you, so (in this section) work with a friend who also has Minecraft and who can join your server when you’re ready to test.
Back in main
, the next function is arena
. The previous section explains that the arena
function doesn’t need to know what players are playing, so you can skip to the next line.
The third line in main
is the event that calls the StartGame
function when a block is broken.
In the StartGame
function, make sure both players are set to Survival mode. Also, because you don’t know which player broke the melon block, make sure both players are teleported to the center of the arena.
Figure 6-21 shows how to make those changes.
StartGame
function, rename the blocksDestroyed
variable to become more specific to Player1, and then add another variable for blocks destroyed for Player2, as shown in Figure 6-22.Back in main
, the next line is the event that is called whenever a player respawns.
SetupPlayer
is called. To make SetupPlayer
multiplayer-safe, add an else-if
statement to the if
statement that essentially does the same as Player1, but for Player2.Figure 6-23 shows all these changes to the SetupPlayer
function.
Be sure to use all the right variables for Player1 and Player2 or else when Player1 respawns, it will teleport Player2 back to the arena.
main
, the next line is the event that is called when a player moves; the removeBlockAfterStep
function is called. Figure 6-24 shows the change to the if
statement that takes place. If either Player1 or Player2 was the one who moved, call the removeBlock
function.Change the removeBlock
function also, to increment the correct blocksDestroyed
counter and to announce the name of the 200-block destroyer.
Figure 6-25 shows how to change this if
statement. (
Note:
The fireworks display has been removed.)
You’re done! Figures 6-26 and 6-27 show the final batches of code, which you can also see at
mod.learntomod.com/programs/sarah-Spleef_Multiplayer
To test this multiplayer version of Spleef, launch Minecraft and go to the LearnToMod server as I showed you in Project 1. Then type the command /open
, as shown in Figure 6-28. This opens your Minecraft world to anyone else who has access to the LearnToMod server, but the only way they can get in is if they know the number that you see in Figure 6-29.
You see the message shown in Figure 6-29, but your number will probably be different.
Have a friend first join the LearnToMod server and then type the command /join ##, where ## represents the number displayed in your server when you type the open
command. For example, Figure 6-30 shows what the other person would type if the message is the one shown in Figure 6-29.
Click Mod in your LearnToMod account, and then run the Spleef_Multiplayer mod in Minecraft. You and your friend are teleported to the center of the arena, as shown in Figure 6-32.
When one of you breaks the melon block, you both should start making blocks below you disappear, as shown in Figure 6-33.
Play the multiplayer version to make sure you have written all of the code correctly as you think it should be.
You should have the multiplayer version of Spleef end whenever one player reaches 200 blocks. Before doing this, however, revisit your scene. Only 400 blocks are on the platform, so it might be difficult to achieve the goal of destroying 200 blocks because it would mean that a player would have to destroy exactly half of the blocks to win, minimizing the number of strategies (like destroying blocks on opposite sides of the arena) that the players can use. An easy fix is to double the size of the arena.
Here’s a quick fix: In the Arena
function, change PlatformSize
from 20 to 40 for both the lava platform and the diamond arena, as shown in Figure 6-34.
Now, in the removeBlock
function, an easy way to end the game is to set both players to Creative mode, and move them to the arena’s center (which is directly above the arena). Figure 6-35 shows the three changes you need to make to end the game when one player destroys 200 blocks: in the removeBlock
, StartGame
, and endGame
functions.
Congratulations! You have successfully made a multiplayer version of Spleef! Be sure to continue building onto this game (maybe try adding a third player), test out all of the changes you make in Iteration 3, and remember: Have fun!
18.116.90.141