Tuesday, 25 April 2017
GPIO zero Minecraft "Whack on board Led's"
This code uses Minecraft to turn on the on board leds by whacking either a TNT block or Glass block. It uses GPIO zero.
To find out about Gpio zero go here:
https://gpiozero.readthedocs.io/en/stable/notes.html#how-can-i-tell-what-version-of-gpiozero-i-have-installed
1. In Minecraft lay a TNT block, glass block and grass block.
2. Open the below code in Python 3.
3. Run the code below
4. Whack the one of the blocks and you should see some action with one of the on board LED's.
TNT turns on the red light
Grass turns on the green light
Glass is random
Code
#import minecraft libraries
import mcpi.minecraft as minecraft
import mcpi.block as block
#import gpio and time
from gpiozero import LED
from time import sleep
import random
mc = minecraft.Minecraft.create()
mc.postToChat("Turn on your Pi lights with MCPI and GPIO zero")
LightList= ["R","L"]
red = LED(35)
green = LED(47)
def RedOnMCPI(msg,time):
mc.postToChat(msg)
red.on()
sleep(time)
red.off()
def GreenOnMCPI(msg,time):
mc.postToChat(msg)
green.on()
sleep(time)
green.off()
def RandomLight():
for i in range(0,6):
light = random.choice(LightList)
if light == "R":
RedOnMCPI("Red",1)
if light == "L":
GreenOnMCPI("Green",1)
while True:
evs = mc.events.pollBlockHits()
for e in evs:
pos = e.pos
b = mc.getBlock(pos.x,pos.y,pos.z)
if b == 46:#tnt
RedOnMCPI("Red",1)
elif b == 2:#Grass
GreenOnMCPI("Green",1)
elif b == 20:#Glass
RandomLight()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment