Minecraft + Python Challenges

Learning Objectives


Another approach to boost your pogramming skills is to learn how to code for Minecraft. Using Python code you can interact with the Minecraft world to for instance move the player or add blocks to build structures.

For this challenge we are using a Raspberry Pi with Minecraft and Python 2 pre-installed.

Let’s see how this works…

Hello Minecraft World


Let’s start by writting a “Hello World” program that will write a welcome message on the Minecraft chat. This is the simplest program and will be useful to make sure we have the required library and can connect to Minecraft.

Here is the Python code:

from mcpi import minecraft, block

mc = minecraft.Minecraft.create()
msg = "Hello Minecraft World from 101 Computing"
mc.postToChat(msg)

So to test this program on your Raspberry Pi you will need to:

  • Type this program in your Python IDE (e.g. IDLE) for Python 2,
  • Launch Minecraft
  • In your IDE run the program
  • Check the Minecraft screen. If everything goes right, a message will appear on screen.

Teleporting your player


minecraft-xyz-coordinatesFor this Python script we are going to write a function to teleport the player up in the sky. We will call this function “jump”. It will take one parameter: the distance (number of blocks) we want the player to jump (up in the sky).

To understand the code below we need to understand how (X,Y,Z) are used in minecraft to retrieve or change the position of a player (and later on the position of blocks).

from mcpi import minecraft, block
import time

def jump(distance):
    #Let's wait 1 second
    time.sleep(1)
    
    #Retrieve the X,Y,Z coordinates of the player
    pos=mc.player.getPos()
    #Change the Y coordinate of the player to position it up in the sky    
    mc.player.setPos(pos.x, pos.y + distance, pos.z)

#Main Program Starts Here:
jump(100)

Building a Tower


minecraft-postThe purpose of this script is to build a ten-block high tower in front of the player. We will be using 3 different methods for you to compare:
  • Method 1: Building the tower one block at a time using sequencing,

  • Method 2: Building the tower one block at a time using iteration (for loop),

  • Method 3: Building the tower in one go using the setBlocks(), method.

Method 1Method 2Method 3
from mcpi import minecraft, block
import time

def createTower():
    #Let's wait 1 second
    time.sleep(1)
    
    #Retrieve the X,Y,Z coordinates of the player
    pos=mc.player.getPos()

    #Create a 10-block high tower, 5 blocks away from the player
    mc.setBlock(pos.x + 5, pos.y, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+1, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+2, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+3, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+4, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+5, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+6, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+7, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+8, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+9, pos.z, block.STONE)
    mc.setBlock(pos.x + 5, pos.y+10, pos.z, block.STONE)

#Main Program Starts Here:
createTower()
from mcpi import minecraft, block
import time

def createTower(numberOfBlocks):
    #Let's wait 1 second
    time.sleep(1)
    
    #Retrieve the X,Y,Z coordinates of the player
    pos=mc.player.getPos()

    #Create a tower 5 blocks away from the player
    for i in range (0, numberOfBlocks):
        mc.setBlock(pos.x + 5, pos.y + i, pos.z, block.STONE)

#Main Program Starts Here:
createTower(10)
from mcpi import minecraft, block
import time

def createTower(numberOfBlocks):
    #Let's wait 1 second
    time.sleep(1)
    
    #Retrieve the X,Y,Z coordinates of the player
    pos=mc.player.getPos()

    #Create a tower 5 blocks away from the player
    mc.setBlocks(pos.x + 5, pos.y, pos.z, pos.x + 5, pos.y + numberOfBlocks, pos.z, block.STONE)

#Main Program Starts Here:
createTower(10)

Challenge #1: Rugby Post


minecraft-rugby-postUse any of the three methods mentioned above and complete the code to create a full rugby post using Python.

Challenge #2: A complex Structure


Build a complex structure using a Python script. Try to identify patterns in your structure to use for loops and hence reduce the number of instructions in your program.

Try to build a Pyramid first starting from a 10 by 10 square base.

Or why not challenge yourself to build a castle like this one:
minecraft-castle

Did you like this challenge?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 7

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , ,