Sunday, 6 August 2017

BitIO tutorial 4:Tilt me ‘Around the World’

This takes the basic '' demo from David Whales BitIO master available here:

Credit to Damien Mooney for the world rendering script:

In the second blog and third blog I extended the basic demo to drop random wool blocks where ever you tilted the Micro:bit and in tutorial three I extended this to lay a road of sorts. 

Blog 2   

Blog 3

In this blog post we will hike up the challenge to combine both push buttons in addition to the accelerometer.

Section 1 of the code:

If you press the a button on the Micro:bit then a data file is read and world map is then rendered into the Minecraft world. You can then use the accelerometer to navigate the character around the map by tilting the Micro:bit.

Here is a video to show this in action:

Section 2 of the code:
If you press the B button then the character teleport's to predetermined locations on the map. Here is a video of this in action:

Here are some static images:

The files you will need are here should you not want to code the BitIO python file:
World map data file:

Build world library:

BitIO file:

If you do want to have a go at making the BitIO file then here are your instructions:

This is what the code will do:

1. Ensure that you have read blog 1:
and downloaded the BitIO master files from David Whales github

2. Go into the 'Bitio master' folder, find the 'src' folder and save 'World map data file' and  'Build world library' from the links above into that folder.

3. Create a new Python 3 script and type out the following:


#“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

Written by @ncscomputing on top of the bitio produced by David Whale

world building code imported library from Damien Mooney's blog:

import serial
from mcpi.minecraft import Minecraft
import time
from mcpi import block as block
import random

mc = Minecraft.create()

# - demonstrates using a button

import microbit

print("micro:bit connected - press button A to test")
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

def Teleport(x,z,Country):
    mc.setBlock(x,1, z,35,random.choice(WoolList))

while True:
    #=======buildworld call when a button pressed
    if microbit.button_a.was_pressed():
        mc.postToChat("Button A pressed: build world")    
        print("Button A pressed: build world")

"build world")

    # manual teleport using acceleromter readings
    pos = mc.player.getTilePos()
    x = microbit.accelerometer.get_x()/300 # -ve=left/+ve=right
    y = microbit.accelerometer.get_y()/300 # -ve=forward/+ve=backward

    pos.x += x # east/west
    pos.z += y # north/south

    mc.player.setTilePos(pos.x, pos.y, pos.z) # set player position

    if microbit.button_b.was_pressed(): 
        print ("Button B pressed: Manual teleport")
        mc.postToChat("Button B pressed: Teleport")  

4. Save it as in the same 'src' folder as the previous files.
5. Open Minecraft, create a new world.
6. Make sure your Micro:bit is plugged in to your PC/Pi.
7. Run the python file that you created above.
8. Try pressing the 'a' button let it build the world map.
9. Try using tilting the Micro:bit to see if you can navigate around the world map.
10. Now try and press the 'b' button and see if the teleportation works.


Try extending the teleportation code to go to extra countries on the word tour.

Enjoy :)

You can now download a pdf version of these tutorials as part of the updated #hackpack resource booklet here:

No comments:

Post a Comment