Activity: The Linked Wall
In this activity, you will link events. You will build a wall of glass. When you break a glass block, a new diamond block will randomly appear in the wall. Then, if you break the new diamond block, orange wool will randomly appear.
Do the activity
Get a jump start
The next lesson, Lesson 3, is about coordinates. Variables are taught in Lesson 4. This activity uses both, but don’t be scared. This activity gives you everything you need to know in a very simple way, so that at the end you can examine these new concepts in the code but do not need any upfront knowledge of them.
The focus remains on events, but in order to make the activity a bit more interesting, these instructions need to include these other concepts.
- Select and Copy the following code (use
Control
+C
).
let to_position: Position = null
let from_position: Position = null
let PlayerPosition: Position = null
player.onChat("position", function () {
PlayerPosition = player.position()
from_position = positions.add(
PlayerPosition,
pos(6, 0, 0)
)
to_position = positions.add(
PlayerPosition,
pos(-6, 13, 0)
)
})
- In the block editor, change to the JavaScript view.
- Paste the code into this window (use
Control
+V
).
- Switch back to the blocks view.
You should now have all the coordinates, and variables to store those coordinates in. Enter position in the chat window to store the coordinates you will need. With that done, let’s get started.
From
||player:Player||
, drag out a||player:on chat||
event handler.Change
"jump"
to"wall"
.
Here you will build your wall using the positions or coordinates you gather.
- From
||blocks:BLOCKS||
, place a||blocks:fill with||
block inside||player:on chat command "wall"||
.
Let’s adjust some things.
Fill with glass instead of grass.
From
||variables:VARIABLES||
, put||variables:from_position||
intofrom
in the||blocks:fill with||
block.Next, from
||variables:VARIABLES||
, put||variables:to_position||
intoto
in the||blocks:fill with||
block.
This should be enough to build your wall. You can test it out if you wish. You will need to first type position in the chat window and press Enter
. Next, type wall in the chat window and press Enter
.
let to_position: Position = null
let from_position: Position = null
let PlayerPosition: Position = null
player.onChat("position", function () {
PlayerPosition = player.position()
from_position = positions.add(
PlayerPosition,
pos(6, 0, 0)
)
to_position = positions.add(
PlayerPosition,
pos(-6, 13, 0)
)
})
player.onChat("wall", function () {
blocks.fill(
GLASS,
from_position,
to_position,
FillOperation.Replace
)
})
Next, you need to react when blocks are broken.
From
||blocks:BLOCKS||
, drag a||blocks:on broken||
block onto the Workspace.Change
grass
toglass
.From
||blocks:BLOCKS||
, put a||blocks:place||
block into||blocks:on broken||
.Change
grass
todiamond
.
let to_position: Position = null
let from_position: Position = null
let PlayerPosition: Position = null
player.onChat("position", function () {
PlayerPosition = player.position()
from_position = positions.add(
PlayerPosition,
pos(6, 0, 0)
)
to_position = positions.add(
PlayerPosition,
pos(-6, 13, 0)
)
})
player.onChat("wall", function () {
blocks.fill(
GLASS,
from_position,
to_position,
FillOperation.Replace
)
})
blocks.onBlockBroken(GLASS, function () {
blocks.place(DIAMOND_BLOCK, pos(0, 0, 0))
})
Now all you need to do is fix the position code.
From
||position:POSITION||
, place||position:pick random position||
into the||blocks:diamond||
block.From
||variables:VARIABLES||
, put||variables:from_position||
intofrom
in this block.From
||variables:VARIABLES||
, put||variables:to_position||
intoto
in this block.
It would look like this…
let to_position: Position = null
let from_position: Position = null
blocks.onBlockBroken(GLASS, function () {
blocks.place(DIAMOND_BLOCK, randpos(
from_position,
to_position
))
})
Now you can just duplicate this block and change a few things to finish.
Duplicate
||blocks:on glass broken||
.Change
glass
todiamond
.Then change
||blocks:place diamond||
to||blocks:place orange wool||
.
Again, to use this code, enter a world. First, type position into the chat window. Next, type wall into the chat window. Finally, start breaking blocks and see what happens.
let to_position: Position = null
let from_position: Position = null
let PlayerPosition: Position = null
blocks.onBlockBroken(GLASS, function () {
blocks.place(DIAMOND_BLOCK, randpos(
from_position,
to_position
))
})
blocks.onBlockBroken(DIAMOND_BLOCK, function () {
blocks.place(ORANGE_WOOL, randpos(
from_position,
to_position
))
})
player.onChat("wall", function () {
blocks.fill(
GLASS,
from_position,
to_position,
FillOperation.Replace
)
})
player.onChat("position", function () {
PlayerPosition = player.position()
from_position = positions.add(
PlayerPosition,
pos(6, 0, 0)
)
to_position = positions.add(
PlayerPosition,
pos(-6, 13, 0)
)
})
Challenges
Now you can change some things to make your own different and unique situations!
Challenge 1 - Add More Blocks When Glass Is Broken
The original code is good, but let’s add even more blocks if glass is broken. How could you add three diamond blocks when glass is broken instead of just one diamond block?
There a few ways to do this.
You will need to:
- Look at the original code to see how diamond blocks are created.
- Find a way to do that two more times.
Challenge 2 - Instead of Orange Wool, Create Chickens!
The original code creates orange wool blocks when diamond blocks are broken. Can you change this so 10 chickens are spawned when diamond blocks are broken?
You will need to:
- Change the code that is triggered when a diamond block is broken.
- Spawn 10 chickens (you could use a repeat loop or 10 of the same blocks).
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 - The Colosseum
This code is huge! Have some fun!
Can you figure out how to start it?
Here’s how this [code]https://makecode.com/_AT7c7CET990X) works: When you kill one of the animals or monsters, they are respawned into the score box above their name. This works with an ||mobs:on animal killed||
event. That is your scoreboard! If you kill all the monsters and animals, all of them will be up above their names in their cages at the end of the fight.
There are a lot of functions in this code. Functions are good way to make bigger code more readable and organized.
What changes could you make to this? Could you change the animals? Could you change the words on the walls? You might try to add in some villagers on top of the colosseum to give it a crowd.