This post will show you how we built a Minecraft map of the world and then used some of our previous python code to track the International Space Station on that map. It was inspired by the fact that there are now two ‘Astro Pi‘ computers in space on the ISS.
Step one is finding a suitable map and converting it into something we can use. I used the ascii art generator at picascii.com to make a text file map of the world from an image of a cylindrical projection. You can get the text file here. Download the file and save it to a folder where you will have your python code.
If you don’t have Minecraft Pi edition already then you should install by running apt-get install minecraft-pi or grab it from the website. Also have a look at this page from the Raspberry Pi website which will give you an idea of what we are doing.
Start Minecraft on your Pi and select a new world [otherwise you will lose everything!] then change desktops and open a new file in IDLE which is the python development environment and run the code below. Note: the code is for python 2.
""" Make a Map in Minecraft""" __author__ = '@damianmooney' from mcpi import minecraft as minecraft from mcpi import block as block import random def clearZone( alocx, alocz, blocx, blocz ): mc.setBlocks( alocx, 1, alocz, blocx, 128, blocz, block.AIR ) mc.setBlocks( alocx, -5, alocz, blocx, 0, blocz, block.WATER ) if __name__ == "__main__": mc = minecraft.Minecraft.create() clearZone( -128, -128, 128, 128 ) print('Cleared') f = open('world3.txt', 'r') # open your ascii art text file mymap = f.read() f.close() myrows = mymap.split('\n') print mymap # --start our top corner of world adjust to get 0,0 ok on map x = 100 z = 85 for i in myrows: #for each line in our map #print len(i) print i z -=1 x = 100 y = 0 for j in i: # go through each position on the current line x -=1 if j != " ": # if the map is not empty blank minecraft then place a grass block position = (x, y, z) mc.setBlock(position, block.AIR) mc.setBlock(position, block.GRASS) elif j != " ": # place water position = (x, y, z) mc.setBlock(position, block.AIR ) mc.setBlock(position, block.WATER )
This code blanks the whole map, reads in our ascii art text file and then goes through each line and puts either a grass block or a water block depending on the value in that position in the text file. You can use this technique to build all sorts of things.
Hopefully you should end up with something like this.
Now we will grab the position of the ISS and convert it to match our map dimensions. We can then update once a minute to see how things work. As we are using web based data your Pi will need an internet connection.
""" Get current ISS position from http://wheretheiss.at/ and map it on a raspberry pi with minecraft """ __author__ = '@damianmooney' from mcpi import minecraft as minecraft from mcpi import block as block from datetime import datetime import time import urllib2 import json def getiss(): """ call where the iss at api thanks to Bill Shupp""" response = urllib2.urlopen('https://api.wheretheiss.at/v1/satellites/25544') mydata = response.read() return mydata def do_coord(longitude): """ longitude: convert our longitude to a minecraft co-ordinate""" mine_long = longitude * -.55 return mine_long if __name__ == "__main__": mc = minecraft.Minecraft.create() mc.postToChat(" Minecraft ISS Tracker for @naascoderdojo") mc.camera.setFollow() mc.player.setting("autojump", False) mc.player.setPos(6, 20, 50) while True: iss = getiss() pos = json.loads(iss) lat = pos['latitude'] lon = pos['longitude'] mc.postToChat(' ISS Location Lat: %.2f Long: %.2f' % (lat,lon)) new_long = do_coord(lon) mc.player.setPos(int(new_long), 20, int(lat)) print('lon %d lat %d' % (new_long, lat)) time.sleep(60) # --only update once per minute - don't be greedy with api
You need to have your minecraft map running first, then switch to a second desktop on your pi and open a command window. Run the python code above and switch back to your first desktop.
Hopefully you should no see something like the image below. Double tap the space bar so your avatar hovers.
The code itself is pretty simple, we read in the json from the api, extract the latitude and longitude and then convert that to x, y, and z values in our minecraft world. The map itself is rough and ready but it is made out of blocks.