/ Capacitive_Touch_Sensors_on_the_Raspberry_Pi / MC-5pad.py
MC-5pad.py
1 # SPDX-FileCopyrightText: 2019 Mikey Sklar for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import mcpi.minecraft as minecraft 8 from digitalio import DigitalInOut, Direction 9 mc = minecraft.Minecraft.create() 10 11 # set the GPIO input pins 12 pad0_pin = board.D22 13 pad1_pin = board.D21 14 pad2_pin = board.D17 15 pad3_pin = board.D24 16 pad4_pin = board.D23 17 18 pad0 = DigitalInOut(pad0_pin) 19 pad1 = DigitalInOut(pad1_pin) 20 pad2 = DigitalInOut(pad2_pin) 21 pad3 = DigitalInOut(pad3_pin) 22 pad4 = DigitalInOut(pad4_pin) 23 24 pad0.direction = Direction.INPUT 25 pad1.direction = Direction.INPUT 26 pad2.direction = Direction.INPUT 27 pad3.direction = Direction.INPUT 28 pad4.direction = Direction.INPUT 29 30 pad0_already_pressed = False 31 pad3_already_pressed = False 32 pad4_already_pressed = False 33 34 immutable = False 35 36 tnt = 46 37 water = 9 38 flowers = 38 39 40 while True: 41 pad0_pressed = not pad0.value 42 pad1_pressed = not pad1.value 43 pad2_pressed = not pad2.value 44 pad3_pressed = not pad3.value 45 pad4_pressed = not pad4.value 46 47 if pad0_pressed and not pad0_already_pressed: 48 #teleport 49 x = 0 50 y = 0 51 z = 0 52 mc.player.setPos(x, y, z) 53 pad0_already_pressed = pad0_pressed 54 55 if pad1_pressed: 56 #Flowers 57 pos = mc.player.getPos() 58 x = pos.x 59 y = pos.y 60 z = pos.z 61 mc.setBlock(x, y, z, flowers) 62 63 if pad2_pressed: 64 #TNT 65 pos = mc.player.getPos() 66 x = pos.x 67 y = pos.y 68 z = pos.z 69 mc.setBlock(x, y, z, tnt, 1) 70 71 if pad3_pressed and not pad3_already_pressed: 72 #Chat message: Are in water? 73 pos = mc.player.getPos() 74 block = mc.getBlock(pos.x, pos.y, pos.z) 75 inWater = block == water 76 mc.postToChat("In water: " + str(inWater)) 77 pad3_already_pressed = pad3_pressed 78 79 if pad4_pressed and not pad4_already_pressed: 80 #Immutable 81 immutable = not immutable 82 mc.setting("world_immutable", immutable) 83 mc.postToChat("Immutable: " + str(immutable)) 84 pad4_already_pressed = pad4_pressed 85 86 time.sleep(0.1)