Controlling Minecraft: Pi Edition

The GPIO pins on the Raspberry Pi are one of the reasons for its popularity. In this guide we've used the CircuitPython libraries to connect to capacitive touch sensors. This has a range of applications and allows you to create some really creative projects.

The Raspberry Pi has a special version of Minecraft that can be programmed with Python. Combining Minecraft on the Raspberry Pi with our capacitive touch sensors allows us to create programs with limitless potential for creativity and fun.

Install Minecraft: Pi Edition

Before we begin you'll need to have Minecraft: Pi Edition installed on your Raspberry Pi. To install Minecraft Pi you can use this guide on the Adafruit Learning System.

Copy the API

In order to connect to a Minecraft game you will need to copy the API.

Open a terminal and create a new directory named Minecraft within the Documents directory with the following command:
mkdir ~/Documents/Minecraft
Next, we will copy the API into this folder with the following command:
cp -r ~/mcpi/api/python/ ~/Documents/Minecraft

Super Jump

This code uses the capacitive touch pad with Minecraft: Pi Edition to make the player jump 10 blocks into the air when the pad is pressed.

This code works best with the momentary board, though can be used with the toggle board and can be adapted for the 5-pad board.
import time
import board
import mcpi.minecraft as minecraft
from digitalio import DigitalInOut, Direction

mc = minecraft.Minecraft.create()

pad_pin = board.D23
pad = DigitalInOut(pad_pin)
pad.direction = Direction.INPUT

already_pressed = False

while True:

    if pad.value and not already_pressed:
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y + 10
        z = pos.z
        mc.player.setPos(x, y, z)
    alreadyPressed = pad.value
    time.sleep(0.1)

Melons

This code will place a melon block at the current position of the player. If you hold down the touch pad of the capacitive touch sensor or use toggle sensor, a trail of melons will be created wherever the player walks. This example works particularly well with the toggle sensor board.
import time
import board
import mcpi.minecraft as minecraft
from digitalio import DigitalInOut, Direction

mc = minecraft.Minecraft.create()

pad_pin = board.D23

pad = DigitalInOut(pad_pin)
pad.direction = Direction.INPUT

# melon block
block_type = 103

while True:

    if pad.value:
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, block_type)

    time.sleep(0.1)

Control Panel (5-Pad Board)

In the last example we use the 5-pad capacitive touch sensor board to do 5 different things in Minecraft Pi.

This is what each pad does:
0 - Teleports the player to co-ordinates (0, 0, 0)
1 - Places a flower at the player's position
2 - Places explosive TNT at the player's position
3 - Checks if the player is in water
4 - Toggles whether or not blocks can be smashed
import time
import board
import mcpi.minecraft as minecraft
from digitalio import DigitalInOut, Direction
mc = minecraft.Minecraft.create()

# set the GPIO input pins
pad0_pin = board.D22
pad1_pin = board.D21
pad2_pin = board.D17
pad3_pin = board.D24
pad4_pin = board.D23

pad0 = DigitalInOut(pad0_pin)
pad1 = DigitalInOut(pad1_pin)
pad2 = DigitalInOut(pad2_pin)
pad3 = DigitalInOut(pad3_pin)
pad4 = DigitalInOut(pad4_pin)

pad0.direction = Direction.INPUT
pad1.direction = Direction.INPUT
pad2.direction = Direction.INPUT
pad3.direction = Direction.INPUT
pad4.direction = Direction.INPUT

pad0_already_pressed = False
pad3_already_pressed = False
pad4_already_pressed = False

immutable = False

tnt = 46
water = 9
flowers = 38

while True:
    pad0_pressed = not pad0.value
    pad1_pressed = not pad1.value
    pad2_pressed = not pad2.value
    pad3_pressed = not pad3.value
    pad4_pressed = not pad4.value

    if pad0_pressed and not pad0_already_pressed:
        #teleport
        x = 0
        y = 0
        z = 0
        mc.player.setPos(x, y, z)
    pad0_already_pressed = pad0_pressed

    if pad1_pressed:
        #Flowers
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, flowers)

    if pad2_pressed:
        #TNT
        pos = mc.player.getPos()
        x = pos.x
        y = pos.y
        z = pos.z
        mc.setBlock(x, y, z, tnt, 1)

    if pad3_pressed and not pad3_already_pressed:
        #Chat message: Are in water?
        pos = mc.player.getPos()
        block = mc.getBlock(pos.x, pos.y, pos.z)
        inWater = block == water
        mc.postToChat("In water: " + str(inWater))
    pad3_already_pressed = pad3_pressed

    if pad4_pressed and not pad4_already_pressed:
        #Immutable
        immutable = not immutable
        mc.setting("world_immutable", immutable)
        mc.postToChat("Immutable: " + str(immutable))
    pad4_already_pressed = pad4_pressed

    time.sleep(0.1)

Download the Code

Open a terminal on your Pi.

Pull Down the above three scripts directly onto your Raspberry Pi using wget.

Download: file
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Super_Jump.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-Melons.py
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Capacitive_Touch_Sensors_on_the_Raspberry_Pi/MC-5pad.py

Running the Code

To run any of the examples on this page you will need to be in Minecraft game world on your Raspberry Pi. You will get an error if you don't have Minecraft open or if you're on the title screen.

All of these examples use the same wiring as the rest of this guide.

The code runs like any other Python program that uses the GPIO. You can run it from the terminal on your Raspberry Pi with the following commands:

Download: file
$ sudo python3 ./MC-Super_Jump.py
Download: file
$ sudo python3 ./MC-Melons.py
Download: file
$ sudo python3 ./MC-5pad.py
This guide was first published on Apr 07, 2014. It was last updated on Apr 07, 2014. This page (Controlling Minecraft: Pi Edition) was last updated on Sep 17, 2019.