All the block coding you have tried thus far is also viewable in this raw form. Don’t get intimidated - just take a look, and try to read it as you would read sentences. By looking back and forth between Blocks and JavaScript, you can probably see what it is doing pretty well!
Activity: Fall Is in the Air
Minecraft Survival worlds are full of high places. Let’s use an event handler and a counter to come up with a useful postmortem report after you have fallen from a high place. This death report will show how many blocks you fell before respawning.
For this project, you will want to be in a Survival world next to a large cliff. Alternatively, you can toggle between Creative mode and Survival mode and fly up to a high point for testing.
You will create a variable that keeps track of the distance you have fallen, and you will report that distance in blocks after you respawn.
Do the activity
Pull out chat commands and player events
Create a new MakeCode project and name it “Falling.”
From
||player:PLAYER||
, drag a||player:on chat command||
to the Workspace.Right-click the
||player:on chat command||
and selectduplicate
. Do this twice so that you end up with three||player:on chat command||
blocks.Name these commands
"cr"
(creative),"su"
(survival), and"pm"
(postmortem).From
||player:PLAYER||
, drag out two event blocks:|player:on player walk||
, and|player:on player died||
.Change
||player:on player walk||
to||player:on player fall||
.
player.onChat("cr", function () {
})
player.onChat("su", function () {
})
player.onTravelled(TravelMethod.Walk, function () {
})
player.onChat("pm", function () {
})
player.onDied(function () {
})
Be “Creative”
The first thing you’ll do is construct the ||player:on chat command||
for "cr"
. This command will change the game mode to Creative. You can fly in Creative mode, so this is very helpful to get to a high place.
From
||gameplay:GAMEPLAY||
, drag out|gameplay:change game mode||
and drop it inside||player:on chat command "cr"||
.In
||gameplay:change game mode||
, selectcreative
mode, and change||mobs:target||
toyourself @s
.
player.onChat("cr", function () {
gameplay.setGameMode(
CREATIVE,
mobs.target(LOCAL_PLAYER)
)
})
Now, to switch to Creative mode, all you need to do is issue the chat command “cr” in the chat window and you will be able to double-tap the spacebar to fly straight up as high as you want.
Just survive
You’ll want to use the same strategy to switch into Survival mode.
From
||gameplay:GAMEPLAY||
, drag out|gameplay:change game mode||
and drop it inside||player:on chat command "su"||
.In
||gameplay:change game mode||
, change||mobs:target||
toyourself @s
.
player.onChat("su", function () {
gameplay.setGameMode(
SURVIVAL,
mobs.target(LOCAL_PLAYER)
)
})
Make the fall
variable
- In
||variables:VARIABLES||
, click Make a Variable, and name the new variable||variables:fall||
.
This variable will start incrementing (adding up) each block the player falls. You will also use another variable to store the final number of blocks the player fell.
Remember the fall
In
||variables:VARIABLES||
, click Make a Variable, and name the new variable||variables:report||
.From
||variables:VARIABLES||
, drag||variables:change item||
into the||player:on player fall||
event.In
||variables:change item||
, select your variablefall
instead of the default,item
.From
||variables:VARIABLES||
, drag out||variables:set item||
. Right-click and duplicate this once. Now you should have two.Put one
||variables:set item||
into||player:on player died||
.In this
||variables:set item||
block, selectreport
instead ofitem
.From
||variables:VARIABLES||
, dragfall
into the second slot and replace the0
so the block readsset report to fall
.
This will save the value of the latest fall in the report
variable. Then you can reset fall to 0 and try another jump.
Basically, something like this is happening after you fall 42 blocks:
- You fall 42 blocks.
- The variable
fall
equals 42. - You copy 42 to
report
. - You change
fall
back to 0. - You print out the report because you are curious.
- You go find something else to jump off of.
Reset the fall count
- Move the other
||variables:set item||
you duplicated in step 15. Put this inside||player:on player died||
at the very bottom. Change the variable tofall
.
let fall = 0
let report = 0
player.onDied(function () {
report = fall
fall = 0
})
player.onTravelled(TravelMethod.Fall, function () {
fall += 1
})
Report your fall count
Finally, you need to report the height of your fall.
From
||player:PLAYER||
, drag||player:say||
into||player:on chat command "pm"||
.Click Advanced to see the
||text:TEXT||
Toolbox category.From
||text:TEXT||
, drag||text:join||
into||player:say||
and replace"Hi!"
.
let report = 0
player.onChat("pm", function () {
player.say("Hello" + "World")
})
Join the variable to the message
In
||text:join||
, enter “You fell “ in the first slot, replacing"Hello"
.From
||variables:VARIABLES||
, drag||variables:report||
into the second||text:join||
slot, replacing"World"
.
let report = 0
player.onChat("pm", function () {
player.say("You fell " + report)
})
More fall details
This will work, but it will look something like “You fell 43”. Well, 43 what? You should tell the player how many units they fell. In this case, you want to say “You fell 43 blocks”. In order to do this, you will need to go into JavaScript.
- Click the JavaScript toggle button at the top of the screen to switch into the MakeCode JavaScript editor.
Find this line of code:
player.say("You fell " + report)
.Add the string
" blocks"
to the end of the line so it looks like this:
player.say("You fell " + report + " blocks")
Go back to Blocks
- Click the Blocks toggle button at the top of the screen to switch back into the MakeCode Blocks editor.
Do you see how the block changed? Are you daring enough to try and change more things on the JavaScript side? See what happens!
Try out the commands
That’s it! Now enter “cr” in the chat window to enter Creative mode, double-tap the spacebar, hold it down until you are high in the sky, and then enter “su” in the chat window to fall back to earth. After you die, click Respawn. In the chat window, enter “pm”. You can see a report of how many blocks you fell!
Complete program
let fall = 0
let report = 0
player.onChat("cr", function () {
gameplay.setGameMode(
CREATIVE,
mobs.target(LOCAL_PLAYER)
)
})
player.onChat("su", function () {
gameplay.setGameMode(
SURVIVAL,
mobs.target(LOCAL_PLAYER)
)
})
player.onDied(function () {
report = fall
fall = 0
})
player.onTravelled(TravelMethod.Fall, function () {
fall += 1
})
player.onChat("pm", function () {
player.say("You fell " + report + " blocks")
})
Shared Program: https://makecode.com/_MoyCau9H9VvP
Challenges
Now you can change some things to make your own different and unique situations!
Challenge 1 - Let’s Report in Blocks
Can you figure out a way to print your fall report in blocks in the Minecraft world? Do you think this might be easier to read than the small words in the chat window?
This is what you will need to do:
- Change the
say
block to something else. Can you figure out what block will allow you to print this information in blocks?
Challenge 2 - Set Up a Variable to Track Total Fall Distance over Multiple Falls
So far, you can report your last fall, but what about reporting the total number of blocks you fall over several jumps? If you fall 40 blocks and then 50 blocks, the total is 90 blocks. Entering “pm” would return, “You fell 50 blocks”. Your new command, “tot” (total) will actually say, “You have fallen 90 blocks total so far! Keep Jumping!”.
This is what you will need to do:
- You need a new
||variables:VARIABLE||
in which to store the total fall distance. - You need to use
||variables:set||
to addfall
to this variable every time you die. - You need a new
||player:on chat command||
. You can use “tot” or something else - it doesn’t really matter what you call it. - You need to use
||text:join||
to put the enture message together like you did before.
Experiments
Here there are no rules… Copy the code for the experiments and change things around to see what kind of results you can create. Suggestions are given, but do as you like!
Experiment 1 - Track Swim Distance
What if you report how far you swim? You could report other actions you might do as well. You could keep track of several actions and then total them all together, or provide a massive report showing all the things you did. Play around with the code and see what kinds of things you might report and store in variables.