Monday, 19 February 2018

Raspberry Pi Minecraft Python Programming

GEEK is a festival of Play & Games and they gave me the opportunity to create an attraction that aimed to introduce people to programming.

I decided to run a Minecraft Python Challenge using Raspberry Pi computers that come with a very special version of Minecraft that can be controlled using the Python programming language. Kids in particular love Minecraft and love building with code even more! Write programmes in Python and see them create Minecraft Coolness in an instant instead of clicking literally thousands of times.
This blog should give you enough information for you to recreate this attraction in your own home, a local school, or some other event.

What is Raspberry Pi?

The Raspberry Pi is a very small, very cheap computer with very large usefulness. You can use it as a standalone computer to surf the internet, check your email and watch videos. It's not as powerful as a laptop, but it is good enough for general use and for just over £30 that's incredible. If that's not enough it is also designed for learning computer science skills and is great for some super cool projects (just google Raspberry Pi projects).

What is Minecraft Pi Version?

Minecraft created a special version of their game just for the Raspberry Pi to help everyone learn those computer science skills. In some ways it is a cut-down version - there are no monsters or animals, you can't craft items and most disappointing, TNT does not explode. In other ways it is the best version of Minecraft because you can write a small computer programme to build a house then loop that to build a city of houses in less than a second. There are many cool people out there writing programmes to build other cool stuff too including turning any block into an exploding TNT-style block - cooler than TNT itself!

What is Python?

These programmes are all written in a programming language called Python. It has nothing to do with snakes, but it has a lot to do with some serious code used in business and holding the internet together. It is also a really easy language to learn and teaches all the important computer science concepts a future programmer needs to know.

What do you need to run this workshop?

  • Raspberry Pi Computer (affiliate link to Pi Hut)
  • Raspbian Operating System
  • Keyboard, Mouse, Monitor/TV
  • Example code
  • Sense of adventure and persistence

The Raspberry Pi computer uses an operating system called Raspbian. For the technically minded it is a special version of Linux for the Raspberry Pi which is free and has hundreds of powerful applications that can be downloaded. It's special for many reasons especially that it comes with the Minecraft Pi Version and Python programming system. Raspberry Pi computers are often sold with Raspbian (sometimes as part of Noobs - google it!) and is what we used to run our attraction. Nothing special or unusual at all.
Plug these into the Raspberry Pi - the keyboard, mouse and monitor or TV - any display with an HDMI connection.
You may need to connect to the internet on the first use of the Pi to fully install Raspbian but after that you can run it off line. The Pi can do wifi or an ethernet connection to the internet.
Once you've got the Raspberry Pi up and running you need to start two programmes from the 'Raspberry Menu in the top left corner: Under the games menu choose minecraft; under the programming menu choose Python 2 (Idle). Play in Minecraft and code in Python.
The following examples were printed out and laminated for each visitor to use. Each teaches important concepts and together they are enough to build some awesome programmes if you have the imagination.

Hello Minecraft!

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

msg = “Hello World”

Where Are You?

X is forward/backwards, Y is up/down, Z is left, right (sort of depends where you’re looking!)

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

while True:
pos = mc.player.getPos()
print pos.x, pos.y, pos.z #see next example for pro tip

Creating A Block

import mcpi.minecraft as minecraft
from mcpi.block import *
mc = minecraft.Minecraft.create()

x, y, z = mc.player.getPos() #pro tip - can you explain it?
x = x+3
mc.setBlock(x, y, z, GLOWING_OBSIDIAN)
mc.setBlock(x, y, z+2, 246)
mc.setBlock(x, y, z+4, WOOL, 16)

Stacking Blocks

import mcpi.minecraft as minecraft
from mcpi.block import *
mc = minecraft.Minecraft.create()

x, y, z = mc.player.getPos()
blockType1 = MUSHROOM_RED
blockType2 = 103

x = x+2
mc.setBlock(x, y, z, blockType1)

y = y+1
mc.setBlock(x, y, z, blockType2)

Making a big block of blocks

import mcpi.minecraft as minecraft
from mcpi.block import *
mc = minecraft.Minecraft.create()

blockType = ICE
x1, y1, z1 = mc.player.getPos()
x1 = x1+10

x2 = x1 + 20
y2 = y1 + 20
z2 = z1 + 60
mc.setBlocks(x1, y1, z1, x2, y2, z2, blockType)

Challenge: Now can you hollow out the block to make a warehouse? Tip: fill the first block of blocks with a smaller block of blocks of AIR. Does that even make sense!?

Repeating your build

import mcpi.minecraft as minecraft
from mcpi.block import *
mc = minecraft.Minecraft.create()

blockType = BEDROCK
numberOfRepeats = 12
x, y, z = mc.player.getPos()
x = x+5

numberOfBlocks = 1
while numberOfBlocks <= numberOfRepeats:
mc.setBlock(x, y, z, blockType)
y = y+1
numberOfBlocks = numberOfBlocks + 1 #Pro tip: you could use numberOfBlocks += 1

Challenge: can you make a square of blocks like a fence around a field?

Block Names and Numbers

AIR = Block(0) STONE_SLAB = Block(44)
STONE = Block(1) BRICK_BLOCK = Block(45)
GRASS = Block(2) TNT = Block(46)
DIRT = Block(3) BOOKSHELF = Block(47)
COBBLESTONE = Block(4) MOSS_STONE = Block(48)
WOOD_PLANKS = Block(5) OBSIDIAN = Block(49)
SAPLING = Block(6) TORCH = Block(50)
BEDROCK = Block(7) FIRE = Block(51)
WATER_FLOWING = Block(8) STAIRS_WOOD = Block(53)
LAVA_FLOWING = Block(10) DIAMOND_BLOCK = Block(57)
LAVA_STATIONARY = Block(11) FARMLAND = Block(60)
SAND = Block(12) FURNACE_INACTIVE = Block(61)
GRAVEL = Block(13) FURNACE_ACTIVE = Block(62)
GOLD_ORE = Block(14) DOOR_WOOD = Block(64)
IRON_ORE = Block(15) LADDER = Block(65)
WOOD = Block(17) DOOR_IRON = Block(71)
LEAVES = Block(18) REDSTONE_ORE = Block(73)
GLASS = Block(20) SNOW = Block(78)
LAPIS_LAZULI_ORE = Block(21) ICE = Block(79)
SANDSTONE = Block(24) CACTUS = Block(81)
BED = Block(26) CLAY = Block(82)
COBWEB = Block(30) SUGAR_CANE = Block(83)
GRASS_TALL = Block(31) FENCE = Block(85)
WOOL = Block(35) GLOWSTONE_BLOCK = Block(89)
FLOWER_CYAN = Block(38) STONE_BRICK = Block(98)
MUSHROOM_BROWN = Block(39) GLASS_PANE = Block(102)
MUSHROOM_RED = Block(40) MELON = Block(103)
GOLD_BLOCK = Block(41) FENCE_GATE = Block(107)
IRON_BLOCK = Block(42) GLOWING_OBSIDIAN = Block(246)

First launched in February 2012 GEEK is held annually in Margate, Kent. For more information please see

No comments:

Post a comment