Welcome Home

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.

Coordinate Systems

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.

Task 1: Moving in Minecraft coordinate systems

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.

Task 2: Write a program to show Steve's position on the screen

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

Task 3: Prettying up messages

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.

  • [ ] Modify your program to print a pretty message shown below to correctly print Steve's position
    Steve's position is: x = 10,y = 20,z = 30
  • [ ] Modify your program to use a variable names message to store the pretty message and then print the message
    Hint:
message = ...
print(message)

In [ ]:
## Type Task 3 program here

Task 4: Display Steve's coordinates in Minecraft

For this task instead of printing Steve's coordinates, lets display them in Minecraft using the postToChat() function from Adventure1

You should see a message like the one below once you run your program.


In [ ]:
while True:
    time.sleep(1)
    ## Type Task 4 program here

Home

In Minecraft move to a location that you want to call home and place a Gold block there. Move Steve on top of the Gold block and write down his coordinates. Lets save these coordinates in the variables home_x and home_z. We will use these variables to detect when Steve returns home.


In [ ]:
## Change these values for your home
home_x = 0
home_z = 0

Is Steve home?

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

Recap

In this adventure you learned about coordinates, variables and if conditions. You used your knowledge to greet Steve with a warm message when he returns home.

Great Job!