Tactical Computer Action (for beginners and their support team)

Month: August, 2013

Minecraft Pi API: Getting Blocks

2013-08-18 12.13.30
Finding the type of block at a specific location is very useful. With this information you can do a variety of things such as checking to see if the player is flying, finding out whether a door is open or seeing if the player has placed a melon on an alter as an offering to the melon god.

Like the methods that set blocks, the methods that find out what type a block is use co-ordinates to determine which block you are interested in. Each method returns a value or an object for that block. There are three methods that we will look at: getBlock(), getBlockWithData() and getBlocks().


When you want to find the type of any block in the game, the getBlock() method is the one you need to use. It is pretty simple, you provide co-ordinates as arguments and it returns the block type as an integer value.

getBlock(x, y, z)

The following example gets the block type at co-ordinates (12, 0, 16):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 12
y = 0
z = 16
blockType = getBlock(x, y, z)


Each block in the game has a number of different states, 16 in total. This allows a single block ID to have a number of different variations. For example the wool block has 16 different states, each one representing a different colour. The TNT block can be smashed like a regular block in state 0 and is explosive in state 1. To see how to set the states check out the section on optional arguments for setBlock() and setBlocks().

Finding out the state of a block with the getBlockWithData() method is relatively simple, yet a tiny bit more complex than the getBlock() method. The getBlockWithData() method takes co-ordinates as arguments in order to determine which block you are interested in. The method returns an object, which contains two attributes, id and data. The id and data attributes store the block type and its state respectively.

getBlockWithData(x, y, z)

The following example finds out the block type and state of the block at co-ordinates (12, 0 ,16):

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()

x = 12
y = 0
z = 16
block = getBlockWithData(x, y, z)

blockType =
blockState =


The getBlocks() method is supposed to return the values of all the blocks within a cuboid that is defined between two co-ordinates. At the moment the getBlocks() method does not work. I have written a function that achieves the same thing, however it is quite slow and may not be interchangeable with the getBlocks() method if it is ever corrected.

Below is my alternative function. It takes two sets of co-ordinates and returns a 3-dimensional list where x is the first list, y the first nested list and z the second nested list:

def getBlocks(x1, y1, z1, x2, y2, z2):
    xhigh = max(x1, x2)
    xlow = min(x1, x2)
    yhigh = max(y1, y2)
    ylow = min(y1, y2)
    zhigh = max(z1, z2)
    zlow = min(z1, z2)

    blocks = []
    for x in range(xhigh - xlow + 1):
        for y in range(yhigh - ylow + 1):
            for z in range(zhigh - zlow + 1):
                block = mc.getBlock(xlow + x, ylow + y, zlow + z)
                blocks[x][y][z] = block
    return blocks

Capacitive Touch Potatoes on the Raspberry Pi

2013-08-11 17.43.50

With a capacitive touch sensor you can use everyday objects as switch inputs with the Raspberry Pi. You can use a variety of things as inputs including potatoes, pencil drawings, toys and anything else you can think of. In this post I’ll show you how to connect your Raspberry Pi to an Adafruit capacitive touch sensor to use potatoes as inputs for your Python programs.

You will need:

  1. A Raspberry Pi (Model A or Model B)
  2. A GPIO breakout with a ribbon cable (or jumper wires), like the Adafruit Pi Cobbler
  3. An Adafruit 5-point capacitive touch sensor
  4. 5 x 10k resistors
  5. A selection of breadboard jumper wires
  6. A breadboard
  7. Something fun to use as input (I’m using potatoes)

Step 1: Wiring

The first thing you need to connect the components on the breadboard. Use the following diagram:


Note: the capacitive touch sensor is slightly wider in real life so there will be fewer breadboard rows to work with. Other than that this will not affect the layout.

Step 2: Code

Before you start you need to have the GPIO Python module installed. To install it connect to the internet and run the following in a terminal:

$sudo apt-get update
$sudo apt-get install python-dev
$sudo apt-get install python-rpi.gpio

Now that you have the Python GPIO module installed you can run a Python program. Copy the following code into a Python file and save it as

import time
import RPi.GPIO as GPIO


# Set the GPIO pins
input1 = 4
input2 = 17
input3 = 18
input4 = 22
input5 = 23
GPIO.setup(input1, GPIO.IN)
GPIO.setup(input2, GPIO.IN)
GPIO.setup(input3, GPIO.IN)
GPIO.setup(input4, GPIO.IN)
GPIO.setup(input5, GPIO.IN)

# Used to make the switch only repeat once
prevInput1 = True
prevInput2 = True
prevInput3 = True
prevInput4 = True
prevInput5 = True

#repeats infinitely to check the GPIO input
while True:
    # Get the state of the inputs.
    # True = not pressed
    # False = pressed
    buttonInput1 = GPIO.input(input1)
    buttonInput2 = GPIO.input(input2)
    buttonInput3 = GPIO.input(input3)
    buttonInput4 = GPIO.input(input4)
    buttonInput5 = GPIO.input(input5)

    # If input 1 has been pressed then print "Yellow"
    if not buttonInput1 and prevInput1:
        print "Yellow"
    prevInput1 = buttonInput1

    # If input 2 has been pressed then print "Green"
    if not buttonInput2 and prevInput2:
        print "Green"
    prevInput2 = buttonInput2

    # If input 3 has been pressed then print "White"
    if not buttonInput3 and prevInput3:
        print "White"
    prevInput3 = buttonInput3

    # If input 4 has been pressed then print "Orange"
    if not buttonInput4 and prevInput4:
        print "Orange"
    prevInput4 = buttonInput4

    # If input 5 has been pressed then print "Purple"
    if not buttonInput5 and prevInput5:
        print "Purple"
    prevInput5 = buttonInput5


Step 3: Run it

Make sure everything is wired correctly and that the Raspberry Pi is connected to the breadboard with a ribbon cable. To run the code open the terminal and navigate to the directory that you saved Run the code with sudo:

sudo python

Press a potato your program should print a colour to the terminal.

How it Works

When you press the potato connected to the the capacitive touch sensor the capacitance of the potato changes. Capacitance is the electronic charge stored by an object. The touch sensor detects this change and connects the corresponding input to ground changing the signal that goes to the inputs on the GPIO. Normally the GPIO receives 3.3v, but when the sensor connects it to ground the signal changes to 0v.

Our Python program checks the input from GPIO pins. Normally the GPIO receives 3.3v when the potato is not pressed. The Python program stores this state in the buttonInput variables as True. When the potato is pressed the input changes to 0v and the Python program stores this state as False in the buttonInput variables. When the buttonInput variable is False, code is run to print a colour string to the terminal. There are five buttonInput variables, one for each of the touch inputs.