Usually when Steve comes home, there is no one at home. Steve can get lonely at times especially after long hard battle with creepers and zombies.
In this programming adventure we'll make Minecraft display a warm and friendly welcome message when Steve comes home. We'll test your program by exploring the world and then come back home to a friendly welcome. Along the way we will learn about coordinate systems which help us locate objects in a game. We will also learn about variables and conditions.
From your math classes, you will remember coordinate systems to locate points in a plane. The points (2,3), (-3,1) and (-1,-2.5) are shown in the grid below.
The Minecraft coordinate grid is shown below:
In Minecraft, when you move East, your X-coordinate increases and when you move South, your Z-coordinate increases. Let's confirm this through a few Minecraft exercises.
In Minecraft look at Steve's coordinates. Now move Steve to any other position. See how his coordinates change as you move?
[ ] Change your direction so that only the Xcoordinate moves when you move forward or back.
[ ] Change your direction so only the Z-coordinate moves when you move forward or back.
Remember functions from the first Adventure? A function lets us do things in a computer program or in the minecraft game. The function getTilePos() get the players position as (x,y,z) coordinates in Minecraft. Let's using this function to print Steve's position as he moves around. We need to store Steve's position when we call the function getTilePos() so that we can print the position later. We can use a program variable to store the position. A variable has a name and can be used to store values. We'll call our variable pos for position and it will contain the Steve's position. When we want to print the position, we print the values of the position x,y and z coordinates using another function print() which prints any strings you give it.
Start up minecraft and type the following in a new cell.
from mcpi.minecraft import *
mc = Minecraft.create()
pos = mc.player.getTilePos()
print(pos.x)
print(pos.y)
print(pos.z)
When you run your program by pressing Ctrl+Enter in the program cell, you should now see Steve's position printed.
Great Job!
In [ ]:
from mcpi.minecraft import *
import time
mc = Minecraft.create()
In [ ]:
# Type Task 2 program here
The messages we printed are somewhat basic and can be confusing since we don't know which number is x,y or z. Why not print a message that is more useful. Often messages are built by attaching strings and data. Try typing
"my name is " + "Steve"
in a code cell. What message gets printed? Now try
"my age is " + 10
Hmmm... That did not work :( Strings can only be attached or concatenated with other strings. In order to attach a number to string, we need to convert the number into a printable string. We will use another function str() which returns a printable string of its arguments. Since x,y,z coordinates are numbers, we need to convert them to strings in order to print them with other strings. Too how the str() function works type the following in a code cell and run.
"my age is " + str(10)
What gets printed by the line below?
"x = " + str(10) + ",y = " + str(20) + ",z = " + str(30)
You now have all the information you need to print a pretty message.
Steve's position is: x = 10,y = 20,z = 30
Hint:
message = ...
print(message)
In [ ]:
## Type Task 3 program here
In [ ]:
while True:
time.sleep(1)
## Type Task 4 program here
In [ ]:
## Change these values for your home
home_x = 0
home_z = 0
Now the magic of figuring out if Steve is home. As Steve moves in Minecraft, his x and z coordinates change. We can detect that Steve is home when his coordinates are equal to the cordinates of his home! To put it in math terms, Steve is home when
$$ (pos_{x},pos_{z}) = (home_{x},home_{z}) $$In the program we can write the math expression as
pos.x == home_x and pos.z == home_z
We can an if program block to check if Steve's coordinates equal his home coordinates. An if block is written as shown below
if (condition):
do something 1
do something 2
Lets put this all together in the program below
while True:
time.sleep(1)
pos = mc.player.getTilePos()
if (pos.x == home_x and pos.z == home_z):
mc.postToChat("Welcome home Steve.")
# the rest of your program from task 4
What happens when you run around in Minecraft and return to the gold block that is your home? That warm message makes Steve happy. He can now be well rested for battling creepers the next day.
In [ ]:
## Type Task 5 program here