Minecraft Pi is a stripped down version of the popular game, similar to the pocket-editions that run on various tablets. It comes installed on the popular Raspbian operating system and can be connected to Python 2 allowing children to write code that produces actions in the game. This allows children, after writing a small amount of code, to produce something with striking visual impact (certainly more than Hello World).
Before children start hacking, they must ensure they have done the following: opened a Minecraft world and started playing the game (tab key will release the cursor from Minecraft Pi); opened Python 2; opened a new file from the Python shell. Once they have written their program, children need to save it (ALT+S) before running (F5).
The first activity addresses the need for programs to be sequenced correctly in order for them to achieve specific goals and involves children writing knock-knock jokes with appropriate delays between the lines of text. The code displayed, which is case sensitive, connects Python to Minecraft, imports the required modules for the program into Python and then posts each string (line of printable text) to the message chat area. By using the time.sleep() command children can create a delay between each message to allow sufficient time to read it.
Moving on, children gain an appreciation of the requirement for precise and unambiguous instructions and the use of predetermined vocabulary and syntax. Children learn how to place a block of red wool below the character’s feet by writing the following code.
First children can modify the colour of the wool by changing the number after .id (1-15). Then by using a list of block ids, they can experiment with changing the block. Once they have mastered this, they attempt to create a large cuboid of blocks by modifying the code further. As well as working out the parameters of the shape, they have to pay attention to the language as they are now instructing to setBlocks and not setBlock. I find it very useful to get children to check each other’s code and debug errors together. My class are used to my response to their statement of “it doesn't work” (regardless of what type of programming they are doing), of “it is doing exactly what you have told it to do.”
Introducing repeats can be a big jump, but by using a forever loop, something most primary-aged children will be comfortable with from Scratch, we can then discuss with them the need for programs to keep running if the instructions are to be applied constantly during the game. The example below returns the player's current position (using x, y, z, coordinates) and then uses that position to change the block below the player's feet. By placing this code within a forever loop, this will happen continuously allowing children to create trails behind their character. Children can tinker with the code to change the position of the block relative to the character, use the setBlocks command to create columns behind them, change the delay before the loop repeats or change the block being placed (TNT.id, 1 - a version of TNT that can be activated by a sword - is a favourite of my class).
The concept of selection can be introduced by creating an event that will happen if the player stands on a certain block. In the example below, the program checks which block is beneath Steve's feet and, if that block is sand, then turn it into a diamond block. Again the children can tinker with the program to change the conditional statement and the subsequent event.
Programming with Minecraft Pi is not about learning how to code with Python, it is an engaging way of starting the transition from graphical programming to text-based programming. It requires children to use pre-determined vocabulary and syntax and to identify and correct errors in their code. However the use of the minecraft is a huge motivating factor and one that makes them want to check the accuracy of their work, test and explore their ideas and collaborate with others, skills that can be utilised in all areas for the curriculum.
Have you used such tactics to teach programming? Share your experiences below!