Code Club

 Putting some Fun into Programming

Our Code Club Worksheets

Prepare your Raspberry Pi Starting with Minecraft on the Pi Build a Minecraft House
Scratch and an LED Minecraft Turtle

The Code Club meets every Wednesday lunchtime in Boscawen classroom. Children can either take part in coding on laptops or the £25 Raspberry Pi computers. The school as part of the curriculum already has the children learning to code using Scratch, from MIT, and Kodu, from Microsoft, in the classes. The Code Club takes it a little further.



On the laptops the children are leaning how to create a website of their own. On the Raspberry Pi computers the children use a slightly different version of Scratch called ScratchGPIO. This version allows programming into and out of the Pi using LEDs and switches and a whole array of equipment should you have access to it.

An initial session has the children coding in ScratchGPIO to switch an LED off and on. Then trying to make it flash and flash in a pattern. This introduces them to looping commands instead of repeating them.

The Raspberry Pi can also run a special edition of Minecraft Pocket Edition that allows programming into it. This is especially popular with the children as any parent may be aware. Running this on the Pi also allows inputting into and outputting from the game as with ScratchGPIO.

Below are some links that might be useful. The Raspberry Pi site is particularly good with a very nice video for anyone new to the credit card sized computer.

Code Club

Scratch and an LED

This exercise has three parts with each more complicated than the first. If you are working as a team of three decide who is going to go first. Each can help the others but this way everyone gets a go with typing in the commands. Swap places after each exercise.

Before switching on your Raspberry Pi connect your LED up as in the picture below. The lead with the resistor goes into Pin 25 and the other into Pin 7.

Prepared LED
 This LED has been prepared in advance. A 220 ohm resistor should be soldered to the short lead, cathode – next to flat edge – from the LED. Then half a female connector lead is soldered to both leads enabling quick fitting to the Raspberry Pi.GPIO pin numbers start from the left as in the picture, closest pin towards you. As pins 7 and 25 are odd numbered ones they are therefore in the closest row to the camera.

When you are ready and you have checked that the pins are correct turn on the Pi and then get ScratchGPIO started. The solutions to the exercises are at the bottom of the page, but try to do them first before looking.

Exercise One

Open a new Scratch file.

File > New

You now need to be able to switch the LED on and off on command. The two commands that do this are these:

Broadcast commands for Pin 7
Broadcast commands for Pin 7

These can be made from the Command part of Scratch by choosing Broadcast and clicking on the small black arrow and selecting New. Type in the command as in the picture.

Now can you program Scratch to switch on the LED for 0.5 seconds when the Space Bar is pressed?

Save your work. Call it something like YourNameLED1.

Exercise Two

Now switch users and try this.

Start with a new file and recreate the same exercise as before. You should have been watching so this should only take you a minute or two.

Now can you extend the instructions so that when the Space Bar is pressed the LED flashes instead of just switching once? Try a pulse of 3 flashes.

Save your work. Call it something like YourName2.

Exercise Three

Switch users again and try this.

Start a New File again and recreate the same as the previous exercise. Have you been watching closely?

Now can you make the LED flash three times, pause, and then repeat three times over? Rather like this:

Flash, flash, flash ,pause, flash, flash, flash ,pause, flash, flash, flash , stop.

Save your work. Call it something like YourName2.

Exercise Four

Now that you have all got the hang of this here is something for all of you to do.


Draw a rectangle with a nice thick border on the Background with enough room for the Scratch Cat to move. Can you program the Cat to move using the arrow keys and whenever he touches the border the LED lights but when he isn’t the LED is off.

The Solutions

Here are the solutions to help if you got stuck.

Exercise One Solution
Exercise One Solution
Exercise Two Solution
Exercise Two Solution
Exercise Three Solution
Exercise Three Solution
Exercise Four Solution
Exercise Four Solution

Starting with Minecraft on the Pi

In these instructions any coding or typing into the computer is written

like this #messages about the command will be like this

on a new line.

Spaces in between characters are important so


is not the same as

python build My

Capital letters are also important and should be used when appropriate. Keep an eye out for changes in Capital letters.

After typing a line of instruction hit Enter key and the instruction will run.

Run Minecraft

From December 2014 Minecraft was installed on the Raspbian operating system as standard. It can be started by using the Menu drop down list from the top left of the screen. These instructions reflect this change as the school Raspberry Pis have all been updated.

Now Minecraft should be running and can be played with as normal. Use the mouse to create a world (or move right by England football shirts pressing D to open an old one). It is in creative mode only and has a limited size like the original pocket edition. The Minecraft window can be moved around the screen by grabbing with the mouse the window just underneath the top of the visible window. Try and place the Minecraft window to the upper right leaving plenty of screen space available to use.

Press Tab to release the mouse so that it can be used on the Pi. Just click inside the game to go back into it.

Run an LXTerminal

Open an LXTerminal and organise the screen so that the game and this terminal can be seen. You will  be using this terminal to talk to Minecraft. In this exercise the commands are not recorded so once the Pi is switched off all is forgotten.

We need to run Python so that commands will be understood. So in the LXTerminal type:


Now we can start to program into the game. The Python interpreter should be running now and the start of a new line will be like this


The LXTerminal is waiting for you to give an instruction using Python language.

Try something simple like:


Or how about:

print("Hello, world!")

For Minecraft we start by importing some Minecraft modules that contain some instructions for Python about the game. Type these in with Enter after each line. Watch out for the Capital letter.

from mcpi import minecraft

from mcpi import block


The last line opens a means of communicating with the game calling it mc. The mc is a variable and you have called it mc but it could be called anything you like in practice. Steve would work just as well as mc, just as long as all the commands needing mc are replaced with Steve.

Test it!

Now things will start to happen. Keep an eye on the game as you press enter after each line.

mc.postToChat(“Hello, world!“)

You should now see a line of text appear on the Minecraft game. Try typing the same again but with your own message. A tip for you though. Instead of retyping every line again in the LXTerminal try using the up arrow on the keyboard. It will cycle through the lines you have previously written. These can be easily edited to make a new command.

To communicate with Minecraft the commands begin with mc. So to find the position of the block the player is standing on would need:


which will give something like this


Finding the player position within the game is very useful when building something. By knowing where Steve is standing it is quite easy to start the build a short distance away from where ever he is. If you used any random coordinates to place some blocks down you may have to walk a way through the world to find them.

We find the getTilePos command more useful than getting the players position:


The coordinates are retrieved as a series of three numbers x, y and z. Where x and z are the block distance on the ground from AC Milan football shirts the centre of the game. The y coordinate is the height above, or below, the centre of the game. These all refer to a three dimensional graph in maths so it is good practice for you!

The spaces within the brackets here are not important but good Python practice. It makes it easier for you to read but note there are no spaces for the code outside the brackets.

To place a block of TNT (no. 46) at a position where x is 5, y is 12 and z is 6 would need:

mc.setBlock(5, 12, 6, 46)

or use by using the block name such as this in capitals:

mc.setBlock(5, 12, 6,

To place a whole block of blocks at a position x,y,z would need:

mc.setBlocks(x, y, z, x2, y2, z2, blockID)

Here the program will place the first block and fill the space up to the second block. Notice the slightly different name setBlocks with an s. If that is only two blocks away then that will be just three blocks needed. Like the picture below. But if the second block was a hundred blocks away it will place those just as fast!


Minecraft Steve has placed three blocks x+5 blocks away Barcelona football shirts from him. If his coordinates are 0,0,0 then the coordinates of the yellow blocks(the z, y, z) could be 5,0,0 and the x2, y2, z2 are 5,0,2. The use of Lego as a way of understanding the positions of blocks is a useful aid.

Try to place a single block a few x blocks away from your Steve player. Get your Steve tile position using the command above. Add a say three to the x number and use the setBlock command to place a block of your choice there. Now try changing the z and then the y coordinates too. Change the block Manchester United football shirts ID as well for different blocks. And if you are feeling really clever build a block of blocks with stone and fill it inside with ones or air or maybe water. What happens if you go over and mine those blocks? And for those with a destructive way use the TNT with a one after it, like this:

mc.setblock(x, y, z, 46, 1)

The TNT can then be made to explode by hitting it. If you place a large block of blocks down you’ll probably really Real Madrid football shirts slow your game down. But of course you’d never do that, would you?!

blockandwater  Here is a block of wood planks 10 x 10 x 10 with a block of water placed inside 9 x 9 x 9.

The block ID numbers are:

        0 => “Air”         1 => “Stone”         2 => “Grass”
        3 => “Dirt”         4 => “Cobblestone”         5 => “WoodenPlank”
        6 => “Sapling”         7 => “Bedrock”         8 => “Water”
        9 => “StationaryWater”         10 => “Lava”         11 => “StationaryLava”
        12 => “Sand”         13 => “Gravel”         14 => “GoldOre”
        15 => “IronOre”         16 => “CoalOre”         17 => “Wood”
        18 => “Leaves”         19 => “Sponge”         20 => “Glass”
        21 => “LapisLazuliOre”         22 => “LapisLazuliBlock”         23 => “Dispenser”
        24 => “Sandstone”         25 => “NoteBlock”         26 => “Bed”
        27 => “PoweredRail”         28 => “DetectorRail”         29 => “StickyPiston”
        30 => “Cobweb”         31 => “TallGrass”         32 => “DeadShrub”
        35 => “Wool”         37 => “YellowFlower”         38 => “CyanFlower”
        39 => “BrownMushroom”         40 => “RedMushroom”         41 => “BlockofGold”
        42 => “BlockofIron”         44 => “StoneSlab”         45 => “Brick”
        46 => “TNT”         47 => “Bookcase”         48 => “MossStone”
        49 => “Obsidian”         50 => “Torch”         51 => “Fire”
        52 => “MobSpawner”         53 => “WoodenStairs”         56 => “DiamondOre”
        57 => “BlockofDiamond”         58 => “Workbench”         59 => “Wheat”
        60 => “Farmland”         61 => “Furnace”         62 => “Furnace”
        63 => “Sign”         64 => “WoodDoor”         65 => “Ladder”
        66 => “Rails”         67 => “CobblestoneStairs”         68 => “Sign”
        71 => “IronDoor”         73 => “RedstoneOre”         78 => “Snow”
        79 => “Ice”         80 => “SnowBlock”         81 => “Cactus”
        82 => “ClayBlock”         83 => “SugarCane”         85 => “Fence”
        87 => “Netherrack”         88 => “SoulSand”         89 => “Glowstone”
        96 => “Trapdoor”         98 => “StoneBricks”         99 => “BrownMushroom”
        100 => “RedMushroom”         102 => “GlassPane”         103 => “Melon”
        105 => “MelonVine”         107 => “FenceGate”         108 => “BrickStairs”
        109 => “StoneBrickStairs”         112 => “NetherBrick”         114 => “NetherBrickStairs”
        128 => “SandstoneStairs”         155 => “BlockofQuartz”         156 => “QuartzStairs”
        245 => “StoneCutter”         246 => “GlowingObsidian”         247 => “NetherReactorCore”


Build a Minecraft House

In this exercise you will learn to build a simple house. This is achieved by placing a large set of blocks and then placing a block of air inside it effectively mining out the interior. The same technique is used to make a doorway and windows.

As before any code to type is written like this

this is code #with a comment after, this is ignored by the program

Your house can be made from any material you like, just consult the Minecraft block library below.

Minecraft Coordinates

Minecraft uses coordinates just like the maths in class. Sorry about that but if you listened in class you’ll find this easier or when it comes up in class you’ll understand it better because of Minecraft!

minecraft axis xyz coordinates How Minecraft coordinates work. Here the blue strip equates to the x axis (east and west). The red is the y axis (up and down) like a graph. Can you see that? Minecraft is in 3D so we need a depth axis into and out of the view and that is the yellow direction or z. The coordinates are written in this order (x, y, z).

The Code

Open a new file in Geany, or if you’re clever open an old one and use the first few lines of that one. Save As in the Code_Club folder as before. Start coding with:

from mcpi import minecraft #importing the Minecraft module
from mcpi import block #importing the blocks information module
import time as time #importing the time module

Now we need to communicate with the game. Rather like a two way radio we can give it instructions and it can give us information.

#making a link into the Minecraft game called mc

The house can be built anywhere. The problem with that is you will have to find it! It would be easier if it was built somewhere close to the player. So the first command is to get the position of the player and call it the variable me.

# Find player position (me) by finding position of block under their feet
me = mc.player.getTilePos()

This will return to the program the coordinate of the block under the player’s feet. (The command getPlayerPos gets the players position on the block under their feet). This is in the form of x, y, and z. The x coordinates can be thought of being the east and west direction, z the north south and y the up down.

Lego_Minecraft_House The house that you built. Or it will be soon. Use this to guide you in understanding the coordinates. The setBlocks command uses the two red Lego brick positions to build the whole block of the yellow bricks. A similar command though slightly smaller will place a block of air inside hollowing out the block. The door and windows are done a similar way.

The code places the bottom corner of the block three x blocks away (east). The far top corner is seven x blocks away and four z blocks away and three y blocks up. Count them on the picture to see.

# Build a block of blocks to make the building here wood is no. 5
mc.setBlocks(me.x+3, me.y, me.z, me.x+7, me.y+2, me.z+4, 5)

# Remove interior by adding a block of air 0 slightly smaller than previous block
mc.setBlocks(me.x+4, me.y, me.z+1, me.x+6, me.y+1, me.z+3, 0)

# Make a door by adding a two blocks of air
mc.setBlocks(me.x+3, me.y, me.z+2, me.x+3, me.y+1, me.z+2, 0)

# Add first window of air, notice the setBlock not setBlocks command.
mc.setBlock(me.x+5, me.y+1, me.z, 0)

# Add second window of air on opposite wall
mc.setBlock(me.x+5, me.y+1, me.z+4, 0)

If you want the windows can be of glass. Can you do that? And the building does not have to be of wood either.

Save the program. So let’s run it and see what happens. Hopefully you will end up with the house in the picture, if you chose to use the gold blocks.

Run Minecraft and Run Your Program

From the Menu in the top left hand corner open Minecraft from the Games menu.

Place the Minecraft window into a corner to give you some room. Move the character to somewhere in an open space. Press Tab to release the mouse from the game. Now open an LXTerminal and type:

cd Code_Club  


If all is well the program will run and place a house not very far away from Steve. If the house is not quite right check your code and debug it. If it was right, try changing the building material? Place Steve somewhere else and build another and another. You could make a whole street of small house really quickly. Try it.

Make Another Street of Houses

Moving the Steve character and rerunning the program to build another house is all very well. But Python can do it a lot quicker for you.

All that is needed is for the program to run the sequence for placing the blocks again but in a different position. For a street of houses it is just a matter of adding a bit more to the y coordinates so that the houses are all facing out. If you change the x position all the doors would face the back of the previous house. Of course, if you change the y coordinate all the houses would be built on top of one another. And you wouldn’t want that would you? Would you??

So let’s go back to the program in Geany. Do a Save As with the program to save a new copy calling it

We need to make a loop that rebuilds a house and also adds a figure to the original coordinates again and again. Place the following code before the first setBlocks command. Set two variables with names such as these:

house_number = 1 #build the first house as number one in our street
move_along = 7 #build the corner of the next house 7 blocks further along

Now the command for the loop. We need to check that the street is not too long so maybe 5 houses is a start.

while house_number < 6:

The setBlock lines already in the program now need to be placed inside the while command. This means that you need to indent them. Place the curser at the front of a line and hit tab once for each line. It should look like this. I’ve left the comments out to make it easier to read but leave yours in:

while house_number < 6:

____mc.setBlock(me.x+5, me.y+1, me.z, 0)
____mc.setBlock(me.x+5, me.y+1, me.z+4, 0)

Now if this was run it would run indefinitely. The house_number would never reach 6 and all the houses would also be built on the same spot. So we need to add a one to the house_number and also move the builders along the street. If we also put a small delay in we may see the houses appearing one after the other in the game. So now add after the previous code and still indented:

____time.sleep(2) #delay of 2 seconds
____house_number += 1 #add 1 to house number
____move_along += 7 #add 7 to the distance from the first corner

Finally we need to add the move_along distance to the z coordinate as mentioned before. The first house will be built where z is the player’s coordinate value (that’s the first z mentioned in the first setBlocks above). The second house has 7 added to z, the third will have another 7 added again so it will be 14 away from the first. And so on.

So add to all the z coordinates in all the setBlocks and setBlock commands :

+ move_along

So for example the first line would now read (remember the indent):

____mc.setBlocks(me.x+3, me.y, me.z+move_along, me.x+7, me.y+2, me.z+4+move_along, 5)

Save the file and run in Minecraft. Any problems? Then it is time to debug again.

Something a bit different

How about making a house with a roof of a different material? Can you do it? And how about a path to each door of gravel and a road going down the street? Or maybe a tower block of houses going up instead of along?

Minecraft Block Names and ID

These are the list of available blocks to build with in the program. Use the number as outlined above like this:

mc.setBlock(x, y, z, 1)  #which will place a stone block number 1


mc.setBlock(x, y, z,  #same thing but note the use of CAPITAL NAME


Prepare your Raspberry Pi

Here we will explain how we set up the Raspberry Pi computers that we use in the Perranporth School Code Club. It is not the only way it can be done but it has all the basics there together with a few extras that will be needed in the worksheets to follow.

What you should have.

We’re going to presume that you have a Raspberry Pi credit card sized computer in your possession. You will also need a screen of some sort as a monitor. The Pi will simply connect to a modern TV with an HDMI lead. Like other computers it will need a USB keyboard and a USB mouse. And power is supplied by a micro USB 5v power supply identical to a lot of mobile phone chargers. The power supply should provide 2 amp of power, not all mobile phone ones do. The official Pi power lead provides 2.2 amp and is only £5 so if you want to be really sure it will work get one of those. The last lead that is not essential but very useful on the initial set up is an ethernet connection to your router for the internet. Just for this start up it means that some really useful software can be installed. But it does not have to be done now and the instructions will still work after the install so don’t worry too much about it.

So the shopping list so far is:

  • Raspberry Pi
  • HDMI Lead
  • USB Keyboard
  • USB Mouse
  • Micro USB Power Supply
  • and …
  • NOOBS Micro SD Card Operating System

The NOOBS –New Out Of the Box Software- SD card is the memory of the Raspberry Pi. NOOBS contains various version of operating systems that can be installed onto the card. The two most popular ones are the RaspBMC that turns a Pi into a media player centre and the one we will use here, the Raspian.

Put It All Together

Lets plug it all in. All the connections will only fit in their appropriate place so it’s not too difficult to work out. The micro SD card slots in underneath and is possibly the only one you will need to really look for. Before switching on switch your TV on first so that the Pi can find it on power up. So lets do that that. With everything connected turn the power on and have your finger ready to press a Shift key.

Some LEDs will light on the Pi and on the screen a multi-coloured square splash screen will appear followed by the Recovery screen. Press Shift on the keyboard.

The install screen will now show the various options to you. The first one, Raspbian, is the one we want. Use the arrow keys to get Raspbian highlighted in blue and a small cross in the box by pressing Enter. Now press i and Enter again. Raspbian will now install which will take around 20 minutes.

At the end a note will come up saying Os’s installed successfully. Press enter and away you go . The Pi will start to boot and the screen will fill with scrolling text. A set up screen will appear to finalise the procedure. There is nothing really here to do for now so select finish with the right arrow key then enter.

The Recovery Screen

While you’re waiting, a word about the Recovery Screen. This appears every time you boot the Pi with the card. Should you ever make a really big mess of your Piand nothing works then you can clear the card and reinstall the operating system again. Or in fact one of the others to try them out. Just press the shift key when the screen appears.

Lets Start Using the Raspberry Pi

Unlike using a Windows PC or an Apple Mac the Pi does not boot to a graphical user interface, GUI. That’s one with little icons you can click on. Instead you have a screen filled with loads of text. When the text stops moving and the Pi gets to the part of where it is asking for a user name you are ready to roll. This first time though this is not needed but from now on you will be asked for it. The user name is pi and the password is raspberry.

If you did not have an ethernet cable then now type startx and enter and the Pi will load the Graphical User Interface and you can now start using the Raspberry Pi.

If you do have an ethernet cable then you are ready to install some extra useful software straight away or you can do this later. Doesn’t really matter when but here is how you do it.

Install Geany, ScratchGPIO and Minecraft!

Adding new software is quite easy on a Pi. You just need to tell it do it it in a certain way. Sometimes the instructions seen really strange but just type it exactly as it is here and it should work. All the instructions are on a new line below and type them exactly word for word space for space as they are. First lets update the system so that everything is up to date with all the latest information. So at the pi@raspberrypi prompt type the following then enter:

sudo apt-get update

Loads of text will appear as the Pi downloads loads of information. When the pi@raspberrypi line reappears you can continue.

You upgrade the Pi to the latest version you can now type

sudo apt-get upgrade

But you do not need to do it. The idea is though that since the SD card was produced Raspbian may have gone through some changes to improve it. Upgrading matches your edition to the latest so it is good practice to do this occasionally. It does take a while to do though so if you want to start playing with the Pi maybe save it for later.

Update from December 2014

The Raspbian operating system has gone through a nice update that gives a more intuiative experience. After doing the update and upgrade commands above run the following too. As with the upgrade command it does take a while to run, be patient.

sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install raspberrypi-ui-mods


This is a useful editor that we use in the Perranporth Code Club to write the Python code to use inside Minecraft.

sudo apt-get install geany

That’s all it takes to install it. Most software packages can be installed this way. When asked “Do you  want to continue?” click Y and enter.

Yet more scrolling text, a little wait and the pi@raspberrypi will appear again.

Install ScratchGPIO

Scratch is already installed. But this version is different. It allows LEDs, switches, motors and other things to be controlled with by the Pi inside Scratch. The package is not installed as above as the software is downloaded from a website then installed. At the time of writing a new version 6 has just been released that supports the new A+ and B+ boards. It still has a few problems but they will be sorted quickly I’m sure. So lets install it.

wget -O

This gets the file to be installed. We now need to unpack it and install.

sudo bash

And that’s it. When we start the Graphical User Interface there will be an extra Scratch icon on the screen.

Install Minecraft

Minecraft Edition for Raspberry Pi

Now to the one that is the most popular item with the Perranporth coders. A special edition of Minecraft Pocket Edition was launched for the Raspberry Pi. It enables simple game play together with the ability to program right into the game and out of it too. What Minecraft gives is an immediate feedback and reward to the coders in an environment they understand and want to control. They can teleport the character Steve, build houses or blocks of flats or create an ore detector. So use:


(If the above line appears as two write it down as one without a gap except after wget). Installation needs the following:

tar -zxvf minecraft-pi-0.1.1.tar.gz

Minecraft has been installed but running it is a little different. For that you’ll need our worksheet or the link to the Minecraft page above.

So that’s it. You are ready to go. At the prompt type startx and enter.

We hope that this helps you get started and enjoy using your Raspberry Pi. Check out the daily blogs on the Raspberry Pi Foundation page. There are loads of ideas to whet the appetite. Good luck and have fun.