Tour the world with Minecraft and Python

A project to teach geography in a fun way

Project difficulty level: Advanced

Prerequisites: You need to have the Python shell installed on your Pi, and Python updated to the 3.4 version at least.

My name is Chris Penn I am a secondary CS/IT teacher from the West Midlands of the UK. I’m also an organiser of local Raspberry Jam events in Coventry, Warwickshire and Eagle Labs in Birmingham.

As a teacher I try to use Raspberry Pi’s as often as I can to teach programming in KS3 and KS4 (ages 11-14). I use coding with Minecraft to try and engage students and get them interested and also run a lunch time club using Pi’s to try and give students chances to play the game and learn how the fundamentals of programming work.

For other fellow educators out there, along the way over the last few years I have created quite a stack of Minecraft-based education resources which you can find on Github. You can also find me on Twitter (@warksraspijam, @ncscomputing) where I get inspiration for new projects and share my own.

If you live in the Midlands or beyond and would like to come to a Jam then have a look at the latest events.

I like to mess around with Pi’s generally to learn new skills and I try my best to feed that back into my lessons as often as I can. I hope you enjoy the hack.

The project

With introductions out of the way, let’s get cracking. Once finalised this project will:

  • Create a world map in Minecraft, reading the data from a file.
  • Plot the coordinates of some countries, defined by you.
  • Teleport the player around to these countries based on the coordinates.
  • Use a loop to do this infinitely.

This could be a great project to teach students Geography based in Minecraft!

For the project we use a couple of tools that we want to reference and whose authors we want to give credit to.

Damian Mooney created the script which we will use to draw and create the world map. This is used in his ISS Minecraft real time tracker project which you can see here.

Also want to thank the author of the raspi2png software I used to capture the screenshots. I followed the tutorial created by Martin O’Hanlon, is available here you can also use this guide from Les Pounder.

Building the project

Here's the list of steps you need to follow to make it happen:

  • Create a folder on your desktop to store the files. We will call it “Around the world in Minecraft” for example.

  • Download the world map data file from Damien’s website here. This file contains the blueprint for the Minecraft map. This will automagically create a world map in Minecraft so we can focus on the fun bit of travelling around it.

  • Save it to the folder you created above. Now you need to download Damien’s python script which draws the world based on the blueprint downloaded in the previous step. For this project I have very slightly tweaked it so the link is from my Github repository here.

  • Now we have the map blueprint and a script that actually paints it on Minecraft. Looking good so far. Next step is to create a script to teleport Steve and control his position.

  • Open up the Python shell.

Create a new document and type in the following code:
Complete Code:

“Written by : @ncscomputing /@warksraspijam”

from mcpi import minecraft as minecraft
from mcpi import block as block
from datetime import datetime
import time
import random
import BuildWorldDM as bw 

WoolList  [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 

EngX = 1.0
EngZ = 50.0

UsaX =  53.8
UsaZ = 39.7

CanadaX = 59.0
CanadaZ = 61.3

IceLandX = 10.0
IceLandZ = 61.1 

mc = minecraft.Minecraft.create()

bw.Build()

time.sleep(9)

def Teleport(x,z,Country):
    mc.player.setPos(x,20,z)
    mc.camera.setFollow()
    mc.setBlock(x,1, z,35,random.choice(WoolList))
    mc.postToChat(Country)

while True
    Teleport(EngX,EngZ,"England")
    time.sleep(8)
    Teleport(UsaX,UsaZ,"USA")
    time.sleep(8)
    Teleport(CanadaX,CanadaZ,"Canada")
    time.sleep(8)
    Teleport(IceLandX,IceLandZ,"Iceland")
    time.sleep(8)
  • Now you have created your code you will need to save it.

  • Open up Minecraft and create a new world and press f5 to work through any errors.

  • Your program should now be teleporting around the 4 countries ☺

Explanation of the code

The code first imports the required libraries into the system. Is essential that you import Damien's python script with the correct name of the file. E.g: If the file name is BuildWorldDM.py you would import BuildWorldDM

    from mcpi import minecraft as minecraft
    from mcpi import block as block
    from datetime import datetime
    import time
    import random
    import BuildWorldDM as bw

After that variables are created. They are the latitude and longitude coordinates of the following countries USA, Eng land, Iceland and Canada, that we use as Minecraft X and Z axis positions.

The list is used to store the different colours of wool. You could use random.randint() function but that is just personal preference.

WoolList  [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 

EngX = 1.0  
EngZ = 50.0

UsaX =  53.8  
UsaZ = 39.7

CanadaX = 59.0  
CanadaZ = 61.3

IceLandX = 10.0  
IceLandZ = 61.1  

After that, we connect to the game so that we can manipulate it. The build world library function ‘build’ is called which builds the map of the world. A delay of 9 seconds is added to give the Pi enough time to build the world

A function called ‘Teleport’ is created that allows us to efficiently move the player and drop a coloured block on that country and then send the new country name as a message to the chat.

mc = minecraft.Minecraft.create()

bw.Build()

time.sleep(9)

def Teleport(x,z,Country):  
   mc.player.setPos(x,20,z)
   mc.camera.setFollow()
   mc.setBlock(x,1, z,35,random.choice(WoolList))
   mc.postToChat(Country)

And to finalize, we start a permanent loop. This allows the program to run until you kill the python script.

The function Teleport is reused for each country name and X and X coordinates passed through as parameters.

while True  
   Teleport(EngX,EngZ,"England")
   time.sleep(8)
   Teleport(UsaX,UsaZ,"USA")
   time.sleep(8)
   Teleport(CanadaX,CanadaZ,"Canada")
   time.sleep(8)
   Teleport(IceLandX,IceLandZ,"Iceland")
   time.sleep(8)

Bonus!

Here's a short screencast of how to use raspi2png to take screenshots of your world!