Duplicate
Remember that you can also duplicate blocks by right-clicking and selecting Duplicate. Then you just make adjustments.
Create a tribute to a very popular food, the burger. This program uses functions to create a mouth-watering creation with a few ingredients.
This is the perfect example for functions. Functions allow you to divide your big projects of code into smaller more manageable pieces. The construction of a burger can also be divided in such a way.
Your burger code will have five parts:
Create a new MakeCode project called burger.
Click the Advanced tab on the Toolbox to display more Toolbox categories.
In ||functions:FUNCTIONS||
, click the Make a Function button.
Repeat the previous step to create four more functions named meat
, lettuce
, tomato
, and Top Bun
.
From ||player:PLAYER||
, drag a ||player:on chat command||
block onto the Workspace.
Rename this ||player:on chat command||
to "burger"
.
From ||functions:FUNCTIONS||
, drag the five blocks ||functions:call function bottomBun||
, ||functions:call function meat||
, ||functions:call function lettuce||
, ||functions:call function tomato||
, and ||functions:call function topBun||
into ||player:on chat command "burger"||
.
NOTE: The order of these function calls is important.
function lettuce() {
}
function bottomBun() {
}
function topBun() {
}
player.onChat("burger", function () {
bottomBun()
meat()
lettuce()
tomato()
topBun()
})
function meat() {
}
function tomato() {
}
||player:on chat command "burger"||
will be your main program to start your burger build. Now, you just need to fill in the individual functions.
The first thing you’ll do is create the bottom bun.
From ||blocks:BLOCKS||
, drag a ||blocks:fill||
into ||functions:bottomBun||
. Change the block by clicking Oak Wood Planks from the drop-down menu.
Enter the coordinates for the top bun with a starting position of (~3, ~0, ~3)
and ending position of (~8, ~0, ~8)
.
function bottomBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 0, 3),
pos(8, 0, 8),
FillOperation.Replace
)
}
function lettuce() {
}
player.onChat("burger", function () {
bottomBun()
meat()
lettuce()
tomato()
topBun()
})
function topBun() {
}
function meat() {
}
function tomato() {
}
Doesn’t look like much yet!
Let’s create a layer of meat.
From ||blocks:BLOCKS||
, drag a ||blocks:fill||
into ||functions:meat||
.
Adjust the material by selecting Brown Terracotta from the drop-down menu. Then change the position in the coordinates section to a starting position of (~4, ~1, ~4)
and a finishing position of (~7, ~1, ~7)
.
function bottomBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 0, 3),
pos(8, 0, 8),
FillOperation.Replace
)
}
function lettuce() {
}
player.onChat("burger", function () {
bottomBun()
meat()
lettuce()
tomato()
topBun()
})
function topBun() {
}
function meat() {
blocks.fill(
BROWN_TERRACOTTA,
pos(4, 1, 4),
pos(7, 1, 7),
FillOperation.Replace
)
}
function tomato() {
}
Let’s create a layer of lettuce.
From ||blocks:BLOCKS||
, drag a ||blocks:fill||
into the ||functions:lettuce||
function.
Change the material selecting Lime Concrete from the drop-down menu. Then change the position in the coordinates to a starting position of (~4, ~2, ~4)
and a finishing position of (~7, ~2, ~7)
.
function bottomBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 0, 3),
pos(8, 0, 8),
FillOperation.Replace
)
}
function lettuce() {
blocks.fill(
LIME_CONCRETE,
pos(4, 2, 4),
pos(7, 2, 7),
FillOperation.Replace
)
}
player.onChat("burger", function () {
bottomBun()
meat()
lettuce()
tomato()
topBun()
})
function topBun() {
}
function meat() {
blocks.fill(
BROWN_TERRACOTTA,
pos(4, 1, 4),
pos(7, 1, 7),
FillOperation.Replace
)
}
function tomato() {
}
Let’s create a layer of tomato.
From ||blocks:BLOCKS||
, drag a ||blocks:fill||
into the ||functions:tomato||
function.
Change the block by selecting Red Concrete from the drop-down menu. Then change the position in the coordinates to a starting position of (~4, ~3, ~4)
and a finishing position of (~7, ~3, ~7)
.
function tomato() {
blocks.fill(
RED_CONCRETE,
pos(4, 3, 4),
pos(7, 3, 7),
FillOperation.Replace
)
}
tomato()
The finally function is the top bun. To give the burger a more realistic look, this function will fill in two layers instead of just one.
From ||blocks:BLOCKS||
, drag two ||blocks:fill||
blocks into the ||functions:topBun||
function.
Change the material for each fill to Oak Wood Planks.
Change the top fill coordinates first. The position for the coordinates should have a starting position of (~3, ~4, ~3)
and a finishing position of (~8, ~4, ~8)
.
The bottom fill should have a starting position of (~4, ~5, ~4)
and a finishing position of (~7, ~5, ~7)
.
function topBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 4, 3),
pos(8, 4, 8),
FillOperation.Replace
)
blocks.fill(
PLANKS_OAK,
pos(4, 5, 4),
pos(7, 5, 7),
FillOperation.Replace
)
}
topBun()
function meat() {
blocks.fill(
BROWN_TERRACOTTA,
pos(4, 1, 4),
pos(7, 1, 7),
FillOperation.Replace
)
}
player.onChat("burger", function () {
bottomBun()
meat()
lettuce()
tomato()
topBun()
})
function topBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 4, 3),
pos(8, 4, 8),
FillOperation.Replace
)
blocks.fill(
PLANKS_OAK,
pos(4, 5, 4),
pos(7, 5, 7),
FillOperation.Replace
)
}
function tomato() {
blocks.fill(
RED_CONCRETE,
pos(4, 3, 4),
pos(7, 3, 7),
FillOperation.Replace
)
}
function bottomBun() {
blocks.fill(
PLANKS_OAK,
pos(3, 0, 3),
pos(8, 0, 8),
FillOperation.Replace
)
}
function lettuce() {
blocks.fill(
LIME_CONCRETE,
pos(4, 2, 4),
pos(7, 2, 7),
FillOperation.Replace
)
}
Let’s add more to the burger!
You should create a plate for the burger. To do this, include another function and name it plate.
Don’t forget to add a call to the plate function in ||player:on chat command "burger"||
.
Can you use the circle block to create more realistic lettuce?
Let’s change some things to make our own different and unique situations!
This code uses three functions. One function digs a cave. Another delays the code to give you a change to fly away because the last function creates a whole lot of bats!
Add something fun to this. Can you think of an enhancement?