8325548264145603-Coding With Minecraft - Unit 6 - Functions
8325548264145603-Coding With Minecraft - Unit 6 - Functions
Unit 6 – 4 Lessons
60 minutes per lesson
Single Student
EDUCATION.MINECRAFT.NET
1
THEME OVERVIEW
In this unit, we’ll explore the coding concept of functions. Often in programming,
there are tasks or procedures that are used frequently within the same program.
Rather than rewrite the lines of code that perform a particular task each time you
need it, you can group that set of instructions together as a function. Grouping
frequently used instructions as a function makes your code more efficient. You can
write the set of instructions once as a function and from then on simply ‘call’ the
function from inside your program whenever you need that task done. A function
is usually given a name that describes the task it will perform when called, making
your code easier to read too!
LESSON OVERVIEW
All of the following lessons are approximately 45-60 minutes, depending on how
much time students are given to explore the coding activities:
• Lesson A: Introduction to Functions
• Lesson B: Coding with Functions
• Lesson C: Building on Functions
• Lesson D: Get Creative with Functions
LESSON OBJECTIVES
• Learn and apply the coding concept of functions
• Describe the different kinds of coding functions
• Explain the importance of functions in coding and while playing Minecraft
• Design an original project to apply the use of functions to create an
automated solution
2
• Remind students that there may be more than one solution for each of the
activities.
• Students should keep a coding mindset. As with any other sport or activity,
you must practice in order to get better. Coding is no different.
MINECRAFT MECHANICS
C C
Summons the Agent and opens the MakeCode
interface
T T
Opens chat panel in Minecraft for commands to
be typed
ESC ESC
When a student wants to leave the game, leave
chat, or pause the game
CODING BLOCKS
Function
Lets you create a portion of code that you
can reuse in your program
KEY VOCABULARY
Function – block of organized, reusable code that is used to perform a single,
related action
LESSON ACTIVITIES
Lesson A: Introduction to Functions (Slides 1-25)
Direct Instruction (Teacher-Led; “I Do”)
Welcome to back to Coding with Minecraft, a place to learn all about the basic
concepts of computer science. Over the next couple of sessions, we are going to
learn about functions. (slide 1)
Today, we are going to learn about what are functions and how we use them not
only in real-life, but how they are used in Minecraft. (slide 2)
3
Review the lesson objectives (slide 3)
Loops will repeat the code inside them for the number of times you ask. Functions
run the code inside them only once, but their power is that they can allow you to
reuse code anywhere you want. You can write the set of instructions once as a
function and from then on simply “call” the function from inside your program
whenever you need that task done. This is great because you need to edit in only
one place if you need changes in the future. Also, your code will be easier to read.
Consider the following two examples. Which one would you rather edit? (slide 5)
4
Both of the preceding examples do the same thing. The difference is when you use
functions, you only need to edit things once. If you want to change coordinates, for
example, or an animal you are spawning, you just make one change! Without
functions, you would need to make five changes. This makes testing and
debugging a lot easier.
Grouping frequently used instructions into a function makes your code more
efficient but also makes it easier to understand. Functions help you organize your
code so that it fits the problem you are coding for. A function is usually given a
name that describes the task it will perform when called, making your code easier
to read. You are able to divide up problems into smaller, more manageable pieces.
Let’s suppose you want to make code that drives a car. Using functions, you would
make one function for each part of driving. You can think about your overall goal
and use functions to help you plan. To drive, you need to turn on the car, sit, stop,
open the door, and do various other things like this. Each of these would be a
function.
In order to consider and think through the steps needed for code, individuals often
use pseudocode to help them think through the specific steps needed.
Pseudocode is a detailed, readable description of what a computer needs to do.
Instead of trying to write this out in code, we will write this in plain English. (slide
6)
5
So now that we have figured out the steps, let’s consider how this pseudocode
would then translate into a function. (slide 7-8)
Imagine that a parent makes you lunch every day – wouldn’t it be nice to delegate
this task to a computer? Ask students to imagine a robot that would make them a
peanut butter and jelly sandwich (PB & J) every day. What are the steps involved in
making a peanut butter and jelly sandwich? Can they write a function that includes
all the steps necessary to make a yummy sandwich? (slide 10)
AN EXAMPLE RUN:
• The first step is often “Open the bread.” Feel free to just rip open the bag of
bread, because their instructions did not say anything about untying the
knot or unclipping the bag opening.
• If the instruction is “Put two slices of bread on the plate,” you can put one
slice on top of the other, because that instruction did not specify how to
place the slices.
6
• If their next instruction is “Put peanut butter on one slice of bread,” you can
put the whole peanut butter jay on the bread slice, because they gave no
instructions about opening the jar first.
• If an instruction tells you to do something you simply cannot like “Use knife
to scoop out jelly” yet the jelly jar isn’t even open, you can just report a
“runtime error” and stop the program.
By the time you have gone through a few steps, or run a couple of programs to the
point where they produce an error, the students have gotten the idea that they
have left out important steps and also made assumptions about what functions
you already know how to perform, and they are asking for their papers back so
they can rewrite their pseudocode.
EXAMPLE FUNCTIONS
Main Program
MakePB&J function
• Open bread bag
• Remove two slices of bread
• Place each slice face down, side by side on the plate
• OpenJar Peanut butter
• OpenJar Jelly
• Pick up the knife
• Spread Peanut butter on one slice of bread
• Spread Jelly on the other slice of bread
• Put the knife down
• Pick up one slice of bread and lay it face down on the other slice of bread
• Wrap the bread in a paper towel
7
Helper functions
Open function
• Grasp the end of the bag with the opening
• Unclip the plastic holder
• Untwist the wrapping
• Reach in
OpenJar function
• Put one hand on the top lid of the jar and grasp tightly
• Put the other hand around the base of the jar
• Repeat until the lid is loose: twist your top hand counter-clockwise
• Remove the lid of the jar
Spread function
• With a knife, reach into the jar
• Scoop out contents
• Move the knife backward and forward over the bread until the knife is clean
• Repeat the previous three steps until the bread is completely covered
Challenges
Challenge 1 – Write a function for a task
Have each student choose a “simple” task, like tying a shoe or brushing their
teeth, and ask them to write in pseudocode a function to perform that task.
• Along with their pseudocode, each student should bring in whatever props
are necessary to perform their function
• Select a student’s pseudocode and give that function and the props to
another student to perform.
• After watching you the day before, the students are primed to follow the
instructions as written!
These exercises help students realize the value of functions to organize their
programs, and also how each function can itself include “calls” to smaller
functions.
Challenge 2 – A Cleaning Robot
• How might you program a robot to clean the house?
• Are there tasks that are common for all rooms of the house? (Vacuuming,
dusting)
• Are there tasks that are specific to certain rooms in the house? (Clean the
toilet, make the bed)
• Which tasks can you assign to functions? (pickUpStuff, dust, sweep, vacuum)
• How might you break up the overall task of cleaning a house into specific
functions? (cleanTheKitchen, cleanTheLivingRoom, cleanBathrooms, etc.)
8
Have students write two different examples for cleaning a room in the house by
using pseudocode.
Have all students log into Minecraft: Education Edition. After students have logged
into the platform, navigate to the Coding with Minecraft subject kit. (slide 13) Once
in the Coding with Minecraft subject kit, select Unit 6: Functions to begin the
lesson. (slide 14)
In this activity, you will create a simple mini-game that creates a tiny pool of water,
then transports the player to a little platform 64 blocks high where the only way
down is to jump and land in the pool of water. If you jump and miss the pool of
water, you lose. Luckily, you’ll build this mini-game in MakeCode by using
functions, so you can try again and again. (slide 15)
Steps for the Coding Activity: A Leap of Faith Mini Game (slides 17-19)
9
• Step 1: Click the Advanced tab on the Toolbox to display more Toolbox
categories.
• Step 2: In the FUNCTIONS toolbox drawer, click Make a Function button.
• Step 3: Name this function pool and click Ok.
• Step 4: Repeat the previous step to create two more functions named:
platform and teleport.
• Step 5: From PLAYER, drag on chat command onto the workspace. Rename
this on chat command to "play".
• Step 6: From FUNCTIONS, drag the three blocks call function pool, call
function platform, call function teleport into on start.
This will be our main program to start our mini-game. Hopefully, you can see how
functions help to organize our planning when coding. Now, let’s build out these
three functions.
• From BLOCKS, drag fill with into pool. fill with will fill a three-dimensional
box from the first set of coordinates to the second set of coordinates.
• Using the drop-down menu in fill with, select a block of water.
• In fill with, enter the following values for the first set of from coordinates: (0,
-1, 0).
• In fill with, enter (2, -3, 2) as the second set of coordinates.
10
This will make a 3 x 3 x 3 pool of water that is located below our player’s feet.
After the pool is built, a platform will need to be built high in the sky. There’s no
need for a tower or a ladder - there’s only one way down! You want to create a
wooden platform that is slightly offset from the pool below so that you have at
least a chance of jumping off the right side of the platform and landing in the pool.
Coding Solution:
Then, you will need to publish and import the program into Code Builder.
11
Step 3: Select the green “Copy link” Step 4: Inside Code Builder, select the
button. “Import” button.
Now, we are going to return to our Minecraft world to try out our code. (slide 22)
12
Let’s make the jump a little more interesting. You can make it more dangerous by
changing the water to lava and spawning some monster around the pool. You
should equip your character with a weapon too.
See if you can:
1. Change the water to lava.
2. Spawn some monsters next to the pool.
3. Give your character a sword of some kind.
Make new functions that handle the preceding steps 2 and 3. You can call these
new functions Monsters and Sword. You will then need to call these new functions
from on start.
It seems like you would die every time. Is there a way to fix this? Maybe save your
character when you fall? You might add water or change the gamemode to make
the challenge better.
Challenge 2: On Bounce
Did you know you can bounce on slime?
Change water in the original activity to slime! Add a call to a function in a new
event, on player bounce. Your function can do whatever you want. The new code
would look like this with your idea added to the function:
Extensions
There are no rules for these extensions. See what you can come up with!
13
Extension 2 - Bouncing, Lightning, and Zombie Pigmen
This code just takes things up another notch from the last experiment!
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 23)
1. How does making something a function save you time when editing?
Answer: You only need to edit changes in the future and the function will
change anywhere it appears in the program.
2. How is a loop different than a function?
Answer: A loop is a block of code to be executed a specified number of times. A
function is a grouped block of code to complete an action.
Make an ordered list of the functions you need to perform to exit the room once
class is finished.
Answer: Answers will vary; listen for the teacher to dismiss us, put my things away,
clean my desk, stand up, grab my backpack, walk to the door, etc.
Conclude the lesson by reviewing the concepts covered in Lesson A. (slide 24)
14
Import project from here:
https://minecraft.makecode.com/?ipc=1#tutorial:github:mojang/educationconten
t/CodingWithMinecraft/unit_6/lesson_B
Make sure you are set-up to demonstrate and lead the students through this
coding experience. It is best if you complete this activity on a device connected to a
projector or presentation screen. Split the screen and display the classroom
presentation alongside of the Minecraft world.
Have all students log into Minecraft: Education Edition. Students should continue
working in the same world from Lesson A. Directions for navigating to the previous
world can be found on slide 30.
Create another mini-game that instantly transforms pigs into an army of Zombie
Pigmen to combat! If you spawn zombie pigs and cannot defeat them, you lose.
Luckily you’ll build out this mini-game in MakeCode by using functions so you can
try again and again. (slide 31)
15
on chat command "play" will be the main code that starts your mini-game.
Now, let’s build out the three functions that will accomplish your goals.
The trick here is that if a pig is hit by lightning, it transforms the pig into a Zombie
Pigman! This is why you want to spawn both at the same coordinates (~0, ~0, ~-5).
The first issue is the difficulty level of the game. The pig will not change into a
Zombie Pig if the game is set to Peaceful.
16
Next, you want to change the game mode for your player. Setting the gamemode
to Survival mode ensures that the Pigman will come after you! As soon as you hit
the Pigman, it will start to attack you back!
Finally, fighting will be very difficult without a weapon. You can give yourself a
weapon to make the game more reasonable for your player. (slide 38)
• From MOBS, grab a give block and place it as the last block in
the setup function.
• You will need to target yourself and give yourself a sword or other weapon.
The example gives one diamond sword.
17
Adjust Time (slide 39)
The last step for this mini-game is to add visual effects by adjusting the time of
day. This will give the appropriate atmosphere that all zombies require. You will
add a time set block. You can find this by using the search bar or looking in
GAMEPLAY.
By setting the game to midnight, you set the current time to the start of midnight.
Time set blocks are an easy way to make time move to a common part of the day.
Midnight is equal to 18,000 ticks (12:00 AM).
• From GAMEPLAY, drag a time set block into the atmosphere function.
• Adjust this to read time set ‘midnight’.
Go to Minecraft, enter t to open the chat, enter play, and then press Enter.
NOTE: If the time is not changing, make sure the always day option is not turned
on in the Settings.
After defeating a pigman, you want another pigman to spawn. You can create a
new pigman with the event on Mob killed. You will use the function call Pigman.
• From MOBS, grab a on animal killed event and place it on the Workspace.
• Adjust this block so that it is on monster zombie pigman killed.
• Add a call function zombiepig to this new event.
Coding Solution:
18
Then, you will need to publish and import the program into Code Builder.
Step 3: Select the green “Copy link” Step 4: Inside Code Builder, select the
button. “Import” button.
19
NOTE: If you are having difficulty with the copy link,
try using the short cut: press Ctrl + C (or Command +
C) on your device.
Step 5: Select the “Import URL” button. Step 6: Paste the Project URL. Then,
select the “Go ahead!” button.
Now, we are going to return to our Minecraft world to try out our code. (slide 40)
Coding Solution:
20
Independent Work (Teacher Support; “You Do”)
Challenges
Challenge 1 – Function to Keep Score
Put the following blocks in the code from the activity. If you put these blocks in the
correct places, your scoreboard should work!
Extensions
Try out this code and just have fun! If you can or just take note of what you see.
You might remember something later and you can use some of this code for a
future project.
Extension 1 – Lots of Pigmen and Lots of Lightning
The on chat command is actually a function. It is the most powerful function
because you can pass in variables. Passing in information to a function is super
powerful. Here you are able to do a lot with just this one on chat command
because it calls itself.
Use this code to start with. Can you figure out what it is doing?
Extension 2 – Flashlight
21
Combining events and functions can get some cool results. It is difficult to see at
night and you may want to know what you are standing on. This code calls a
function to test what you are standing on as you walk and returns a message if
you are standing on grass.
This is similar to the work you will do for the artificial intelligence lesson in Unit 9.
LESSON CONCLUSION
Upon completion of this lesson, students should be able to answer the following
questions: (slide 41)
The following questions can also be used as an exit ticket (formative assessment).
A printable version of these questions can be found in the Assessment Guide.
1. What would have happened if you set your pigs and lightning to different
coordinates in your zombiepig function?
Answer: The lightning would not strike the pig and transform it into a
zombie.
2. Name a real-life function where a small error would mean something
doesn’t happen in the correct way.
Answer: Answers will vary; e.g., baking a cake, building a model airplane,
reading directions to a test, etc.
Conclude the lesson by reviewing the concepts covered in Lesson B. (slide 42)
22
Review the lesson objectives. (slide 45)
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. (slide 46)
Have all students log into Minecraft: Education Edition. Students should continue
working in the same world from Lesson B. Directions for navigating to the previous
world can be found on slide 47.
We will create the first part of the burger together. (slides 49-52)
• Step 1: Click the Advanced tab on the Toolbox to display more Toolbox
categories. In FUNCTIONS, click the Make a Function button.
• Step 2: Name this function bottomBun() and click “ok”.
• Step 3: Repeat the previous step to create four more functions names meat,
lettuce, tomato, and Top Bun.
• Step 4: From PLAYER, drag a on chat command block onto the Workspace.
Rename this on chat command to "burger".
23
• Step 5: From FUNCTIONS, drag the five blocks call function bottomBun, call
function meat, call function lettuce , call function tomato, and call function
topBun into on chat command "burger".
on chat command "burger" will be your main program to start your burger build.
Now, you just need to fill in the individual functions.
24
Independent Work (Teacher Support; “You Do”)
Students should finish the rest of the code on their own. They need to complete
the remaining 4 parts to complete the burger. Then, they should import the
program into Code Builder and test the code. (slide 54)
Coding Solution:
25
Challenges
Let’s add more to the burger!
Challenge 1- Burger Plate
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 on chat command
“burger”.
Challenge 2 – Circular Lettuce
Can you use the circle block to create more realistic lettuce?
Now you can change some things to make your own different and unique
situations!
Extension – Bat Cave
This code uses three functions. One function digs a cave. Another delays the code
to give you a chance to fly away because the last function creates a whole lot of
bats.
NOTE: In order for the cave function to work properly, you need to be standing on
the ground when you start the code.
LESSON CONCLUSION
Upon completion of this lesson, review the concepts covered in this lesson. (slide
55)
Then, students should take the quiz (found in the assessment guide). This is
intended as a low-stakes formative assessment tool to establish whether students
are prepared to take on the unit independent project.
26
Questions Answers
1. How does making something You only need to edit in one place if you
a function save you time when need changes in the future and the function
editing? will change anywhere in it appears in the
program.
2. How is a loop different than a A loop is a block of code to be executed a
function? specified number of times. A function is a
grouped block of code needed to complete
an action.
3. What is the difference A function is a self-contained set of
between a function and a instructions for performing a specific task
custom function? within a computer program. Most objects
have multiple functions associated with
them. A custom function is a function
created by the programmer (as opposed to
functions associated with objects). A custom
function usually has a specific purpose and
can be/is used in multiple places within a
program.
4. What is the difference A Helper function usually has a specific
between Main Code functions purpose and can be/is used in multiple
and Helper functions? places within a program. A Main Code
function is a self-contained set of
instructions for performing a specific task
that often includes several Helper functions.
5. Why is it good coding practice To make it easily recognizable—In a large
to give functions meaningful coding program, naming functions with
names? names you can easily identify makes finding
problems much easier.
Then provide students with a preview of the next lesson. (slide 56)
27
Review the lesson objectives. (slide 58)
In this unit, you learned how organizing blocks of code into separate functions
makes your code more readable and saves space. Functions generally contain
sections of code that go together logically and accomplish one thing. The
function’s name usually tells you what it does.
Functions can call other functions. Although functions in MakeCode do not accept
parameters, you can use an on chat command block to pass in a parameter. You
might pass in a number to affect what the function does. Also, you could update
the value of an existing variable and then any function can access that. (slide 59)
For this independent project, build your dream house or build a replica of an actual
building in Minecraft. Either way, use functions to automate creating as many
different sections of the house as possible. You might start with creating a floor
command that creates a floor by filling blocks in a square area at your feet. Or, you
might have a carpeting agent that creates long carpet runners that run the length
of your house. How about a builder that can create a farm for you automatically?
(slide 60)
28
• Build a doorway.
• Dig a basement.
Review the success criteria for the independent project. (slide 62)
Come up with something original that meets the following criteria:
• Creates, or helps to create a building, temple, monument, or other piece of
architecture in Minecraft
• Provides at least three separate and distinct functions
• Includes descriptive names that represent what each function does
Grading Criteria
FUNCTIONS
4 = At least three separate functions, logically separate and appropriately named.
3 = Two separate functions, logically separate and appropriately named.
2 = One function logically separate and appropriately named.
1 = No functions logically separate nor appropriately named.
CHAT/PARAMETER
4 = Project addresses all required elements effectively and efficiently.
3 = Project is missing 1 of the required elements or is mostly effective and efficient.
2 = Project is missing 2 of the required elements or is somewhat ineffective and/or
inefficient.
1 = Project is largely ineffective and/or inefficient.
29
LESSON CONCLUSION
Upon completion of this lesson, review the concepts covered in this lesson. (slide
64.
30
EDUCATIONAL STANDARDS
CSTA Standards
• 2-AP-14 Create procedures with parameters to organize code and make it easier to
reuse.
• 2-AP-11 Create clearly named variables that represent different data types and perform
operations on their values.
• 2-AP-10 Use flowcharts and/or pseudocode to address complex problems as
algorithms.
• 2-AP-12 Design and iteratively develop programs that combine control structures,
including nested loops and compound conditionals.
• 2-AP-13 Decompose problems and subproblems into parts to facilitate the design,
implementation, and review of programs.
• 2-AP-19 Document programs in order to make them easier to follow, test, and debug.
ISTE Standards
• 1.4.c Students develop, test and refine prototypes as part of a cyclical design process.
• 1.5.a Students formulate problem definitions suited for technology-assisted methods
such as data analysis, abstract models and algorithmic thinking in exploring and
finding solutions.
• 1.5.c Students break problems into component parts, extract key information, and
develop descriptive models to understand complex systems or facilitate problem-
solving.
• 1.6.b Students create original works or responsibly repurpose or remix digital
resources into new creations.
31