Superpowers for Steve

With our newly learned programming skills let's give Steve some superpowers! Usually when Steve steps off a ledge he falls down and gets hurt and when he jumps into water, he sinks unless he starts swimming. Lets write a program that lets Steve automatically build a glass bridge whenever he steps off the ground into water or air. With this new program Steve will never fall or sink. An awesome superpower!

Along the way, we will learn about functions and arguments and use conditions and loops from the previous adventures.

Lets get started ...


In [1]:
import sys
# sys.path.append('/Users/esumitra/workspaces/mc/mcpipy')
# Run this once before starting your tasks
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
mc = minecraft.Minecraft.create()

Task 1: What are you standing on Steve?

We first need to detect if Steve is falling or sinking. How do we do that? As you know, we use blocks for building things in Minecraft. Most of you have built houses and crafted weapons. The secret is that in Minecraft every square that is visible is a block. The ground is built from ground blocks and water is built from water blocks. Even air is built from air blocks! That means Steve is going to fall if he is standing on an air block and he will sink if he is on a water block.

The function getBlock(x,y,z) gives us the block at the Minecraft coordinates (x,y,z). Each kind of block has a unique identifier. An air block has an identifier block.AIR.id and a water block has an identifier block.WATER_STATIONARY.id and block.WATER_FLOWING.id. To use these block ids we need to import the block library which has the ids of all the blocks used in Minecraft.

Your first task is to write a program that prints the block Steve is standing on when run. Complete the program below and verify that the program prints the correct block


In [3]:
# Task 1
pos = mc.player.getTilePos() # Steve's current position
# b = mc.getBlock(?,?,?)

Hmm ... The program is printing numbers. It turns out that the block identifier is a number. Often numbers are used as identifiers. In order to print a useful message try the following

if b == block.AIR.id:
    print "I am on air"
if b == block.WATER_FLOWING.id:
    print "I am on flowing water"
if b == block.WATER:
    print "I am on water"
if b == block.WATER_STATIONARY:
    print "I am on water"

Update your program for task 1 above and verify that you are able to correctly detect air and water.

Nice job

Task 2: Some fun

The previous task had a lot of statements to figure out if Steve was standing on air or water. We need to use these statements everytime Steve moves to a new position. Wouldn't it be nice if Minecraft had a function just like postToChat that would tell us if Steve was on a air or water block? Unfortunately, Minecraft does not have such a function but you have some superpowers - you can define your own functions! And when you define your own functions, you can use these functions in your programs as you need.

First, lets take a look at the function postToChat like the example below

mc.postToChat("Minecraft rocks!")

The name of the function is postToChat. The message you give the function to post is called the function's parameter. A function may have no parameters like in minecraft.Minecraft.create() or it may have one parameter like mc.postToChat("something useful") or it could have many parameters like max(1,5,2). Some functions return values when you call the function. For example, the function max(1,5,2) returns the maximum value of the numbers 1,5 and 2. Try it out.

Now lets write our own fun function :) Remember every function has a name, can have zero or more parameters and can have a return value. Lets define a function named myCoolFunction with one parameter named n that will post the message "Minecraft rocks times ..." when you call the function. In Pytho, functiosn are defined using def as shown below.

def myCoolFunction(n):
    mc.postToChat("Minecraft rocks times " + str(n))

Go ahead and type the code below to define your function


In [ ]:
# Task 2 code:

Now that you have defined your own cool function lets call the function with different arguments like

myCoolFunction(1)

myCoolFunction(3)

myCoolFunction(5)

Try calling your function below. That was something fun with functions!


In [ ]:
# call your cool function

Task 3: Is Steve Safe?

For this task we will write a function named isSafe that will take a parameter position and return a value False if the input parameter position is above air or water and will return the value True otherwise. We will use the statements from Task 1 to write this function. Use return to return a value from this function. The outline of the function you need to write is below. Try to complete the function in the code block below.

def isSafe(position):
    b = mc.getBlock(position.x,position.y-1,position.z)
    if b == ?:
        return False
    if b == ?:
        return False
    return True

In [ ]:
# Task 3

Task 4: Steve's Safety Status

Lets test the function isSafe that you wrote in Task 3 by posting a message evertime Steve is not safe i.e., lets post a message "You are not safe" evertime Steve is over air or water. Type and modify the code below to call your function to show the message.

while True:
    pos = mc.player.getTilePos() # Steve's current position
    time.sleep(0.1)
    # add your code here

Hint: You can call your function in an if statment like

if not isSafe(pos):
   doSomething

Nice job!


In [ ]:
# Task 4

Task 5: Set a Block

Now for the fun part of giving Steve superpowers. All we need to do to make Steve build a bridge everytime he is not on a safe block is to set the block under him to a GLASS block or any other block we want to use for the bridge. To set a block at a position in Minecraft use the setBlock function. E.g., if Steve is at coordinates pos, to set a glass block under him use

mc.setBlock(pos.x, pos.y-1, pos.z, block.GLASS.id)

Lets use the safe function you wrote earlier and modify the program in Task 4 to set a glass block evertime Steve is not on a safe block.

Hint: Change the mc.postToChat function in the Task 4 program to mc.setBlock


In [ ]:
# Task 5

Other blocks that you can try for your bridge are STONE, GRASS and OBSIDIAN. Try building the bridge with different blocks and have fun exploring the world!