Monday 24 July 2017

BitIO Blog 3: Extending the accelerometer 'Tilting Rainbow Road'



This takes the basic 'tilt_mc.py' demo from David Whales BitIO master available here:
https://github.com/whaleygeek/bitio

In the second blog the 'tilt_mc.py' demo was extended to:

1. Use a list to randomly select a wool block.
2. Drop this block underneath the player the end result is a colourful mess. :) (See below)

The code was saved in the 'src' folder to work and did this >>>


This third blog will extend this further to drop a roads worth of blocks each time the character is moved from a tilt of the Micro:bit. The end result will look like this:



Again you will use the accelerometer to move player and it will drop the road to the left of the character. The code will need to be saved in the src folder. (This is all explained in blog 1)  Here is the code (a marginally more complex version of the previous blog):

Code:
#Original written by David Whale located here as part of his BITIO library
#https://github.com/whaleygeek/bitio/blob/master/src/tilt_mc.py
#adapted by @ncscomputing 20/07/17


import mcpi.minecraft as minecraft
import mcpi.block as block
import microbit
import time
import random




mc = minecraft.Minecraft.create()

blocksList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
time.sleep(7)
mc.postToChat("Rainbow Road 3.0 Micro:bit controlled")
while True:
    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

    Count = 1
    while Count <=16 :
        mc.player.setTilePos(pos.x, pos.y, pos.z)
        mc.setBlock(pos.x-Count, pos.y-1, pos.z,35,Count)
        Count = Count+1
    time.sleep(0.25)

Download the code from here:
https://raw.githubusercontent.com/ncscomputing/HpAnthologyV2/master/Rainbow%20road%20tilt.py



You can now download a pdf version of these tutorials as part of the updated #hackpack resource booklet here:
https://github.com/ncscomputing/HpAnthologyV2/raw/master/Hackpack%20Anthology%20V2%200.3.pdf

Enjoy :)



1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete