Setting Blocks
Python Minecraft - Lesson 4
Objectives
- To create programs using a text based programming language
- To understand and use 3D coordinates to position a player and blocks
- To understand and use variables in a program
- Use logical reasoning to debug errors in a program
Lesson Resources
Lesson 1 - Hack the chat
Lesson 2 - Debugging challenges
Lesson 3 - Using 3D coordinates
Lesson 4 - Setting Blocks
Lesson 5 - Setting Block Challenges
Lesson 6 - Going loopy
Introduction
Ask the students to set up their Raspberry Pi as we did in the first lesson. Once set up, they should open and arrange the three windows as we did in the previous lessons.
Recapping last time
What is a coordinate? They are a set of values that can be used to specify an exact location. How did we use them in our last lesson? To place our character and blocks in exact locations in the Minecraft world. What is different about the coordinates we’ve used for Minecraft to those you might use in Scratch or on a map? These coordinates are for a 3D environment, which uses three axes (X, Y and Z) and requires three numbers.
Challenge 7: Hide & Seek!
Take a look at the code below. We are going to use it to add a single block to our Minecraft world. We did that in our last lesson as well, but the difference here is that it will be added to a random location within a specified area, otherwise you’d have no chance of finding it!.
Before you run the code though, the environment and camera view we use for this task is quite important. Double tap the spacebar so your character takes off and then fly until you find a large flat area of land or water (it’s more likely you’ll find an open space of water). Make a note of your current coordinates and then turn the viewpoint to look downwards at the floor, a bit like this image:
Here’s the code we’re going to try, take note of the colour code for the highlighted parts below the table, these refer to your coordinates.
Back in challenge 2, we used the ‘random’ library to choose a random number between 0 and 2, and this number was used to select an item from a list that appeared in the chat pane. In this challenge we are going to use the same library (imported on line 2), but for a different purpose. We are creating three variables again (xPos, yPos and zPos). The value of yPos is set to 2. We need this to be fixed so the block we’re going to add appears at a height where we can see it. 2 on the Y axis will always be just above the floor level, which is why this needs to be done somewhere flat, otherwise it might appear inside a hill.
The values for xPos and zPos are a little different though. The code on lines 5 and 7 sets their values to a random number within a range that we specify (the numbers in the brackets). Everyone’s numbers will be different here, depending on where they are in their Minecraft world.
As the colour key above says, to work out the correct range of numbers to use, ask the students to take their current X and Z coordinates and add 20 to each number. These will be the beginning and end of the range.
In the example above the character’s X coordinate was 35 so we added 20 to it to get a range from 35 to 55. Do the same for the Z coordinate. This will ensure that the block is dropped in a relatively small area, and the area we are looking at!
The final piece of code to describe is the following:
mc.setBlock(xPos, yPos,zPos,103)
This code will set a block in our flat area. The number 103 is a ‘block reference’. It tells the program which block type to use. 103 is a melon block. So, when we run our code we will need to look around our area for a melon block.
Take a look at our before and after shots… Can you spot the difference?
Before:
After:
You can probably tell that we ran the code three times as there are three melon blocks in the ‘after’ shot. Every time you run the code it should pick a different random location in the area based on the random X and Z coordinates.
So now we know how to set a block in the Minecraft world we can use that mc.setBlock command to do some really interesting things:
Challenge 8: The Glitch
Imagine if there was a block in the minecraft world that was constantly changing from one block type to another. You might think it was a glitch right? Unless it was programmed that way!
This code creates what looks like a ‘glitch’ (error) in the game. What’s actually happening is that the block we are placing is a watermelon for 0.1 seconds, and air for 0.1 seconds. It cycles through these 2 states while the program runs because we are using the “while” loop command. This creates an infinite loop. Because it changes states so quickly, it looks like a glitch. Check out still images of the 2 states below:
State 1: Watermelon
State 2: Air
Ask the students: Which part of the code tells the block to be Watermelon? Which part of the code tells it to be air? Can you play with these pieces of information? Can you slow down the switch between the two blocks?
Where are the variables?
In the above hack we haven’t declared the X, Y and Z positions as variables. “Why not?” you might ask. Well, we don’t always have to. Programs can be designed in a number of ways to fulfill the same purpose, so in this example we’re just inputting the coordinates rather than creating variables. We won’t be changing the numbers in the program, so variables aren’t actually needed.
Block ID’s
Every block in the Minecraft world has its own “Block ID”. So far we have used 2 different Block ID’s:
103 = Melon
00 = Air
We can however use any block type we want, all you need to know is what type of block you would like to use, and what number their ID is. You can find this out by looking at our Block ID guide. The guide is displayed in the lesson slides, but if you'd like to print copies for your students, open the document and select File > Print.
Challenge 9: A Little Stack!
Now let’s use mc.setBlock to build a little tower of ice blocks.
Ask the students What do you think yPos = yPos + 1 does in the code above? To answer that we’ll take a small step back. On the line before that command (Line 9) the first block is set at our current X, Y and Z coordinate. We’ve also used a variable called blockType, in this case its value is set to 79 (the ID code for ice).
Once the first block has been placed we then see the code yPos = yPos + 1 on line 10. This declares a new value for the variable yPos. It takes the old value of yPos and adds 1 to it. So if yPos was previously 1 it will now be 1+1 (2).
Then the new value is then used on line 11 when the next block is set. This means the new block will appear 1 space higher on the Y axis (just above the previous block). Lines 12, 13, 14 and 15 repeat this process so we again move up one space on the Y axis and end up with a small stack of four ice blocks.
Finally, ask the students to experiment with different block types using the Block ID sheet.
Plenary
How did we use coordinates today? All the way through we’ve used coordinates to specify where we want to place blocks in the Minecraft world.
How did we make use of a ‘random’ command? We used this to set a block within a randomly chosen range of coordinates in the ‘hide and seek’ challenge.
How could you get it to randomly choose which type of block is set as well? You could create a fourth variable called blockType and also set that to a random range using the numbers from the Block ID sheet (be aware not all consecutive numbers are used on the sheet), then use the variable’s name in the code when the block is set, e.g:
What was actually happening when it looked like there was a glitch of the flashing block? There was no glitch, the code was just using a loop to switch between two different block types at the same coordinates, one of which was air, so it looked like the block kept disappearing.