Going loopy

Python Minecraft - Lesson 6

Objectives

  • To use and understand 3D coordinates to create structures
  • To use logical reasoning to debug errors in a program
  • Recognise and use different types of loop
  • To use loops to make a program more efficient

 

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

Ask the students to set up their Raspberry Pi as we did in the previous lessons. Once set up, they should open and arrange the three windows we require. 

Recap the last lesson and the block building challenges. What structures did you have to code?  What were the key principles of how they were created?  

 

 

Take another look at the staircase challenge and ask them to discuss with a partner how the code worked before asking some students to share their thoughts with the class. 

In all of the challenges they used the set block commands to place a block and adjusted the coordinates before the next block was added. This was done by using a variable for the X, Y and Z coordinates and adding to, or subtracting from these variables before another block was placed. 

 

Using loops

In this lesson we’ll be continuing with creating structures, but making them bigger, faster by making use of loops to improve the efficiency of our code. 

Ask the students What is a loop?  What different types can there be? Where have you used them before? 

Students will have used loops in the past as they are an essential element of coding, even at a basic level. Let’s look back to Scratch for a moment where most students should have come across three types of loop:

 

Counted loops

Counted loops repeat commands a certain number of times, like this example from Scratch, can you tell what it would do? (draw a square)

 

 

Each repetition of the code inside the loop is called an iteration.

 

 

Repeat while true

Sometimes you don’t want to repeat code a predetermined number of times. Instead you want to repeat until or while some condition is TRUE.  In Scratch this can be accomplished using a ‘Repeat Until’ block.  The hexagon shape after the word “until” means we need some type of condition to determine when the loop will stop.

In this example, the code would make a sprite move around until their lives ran out in a game. 

 

 

 

Infinite loops

The third type of loop is an infinite, or Forever loop. This type of loop will keep repeating the code inside the loop until the user stops the code running. In this example with Scratch, it means that every time the W key is pressed the Sprite would move up the Y axis.

 

 

The same concepts can be used in Python to help us build things in Minecraft. We’ve actually already used a loop in our Python code back in lesson 1. We used this code to randomly repeat phrases appearing in the chat pane:

 

 

The ‘While True:’ element is the loop, and in this case it's an infinite loop, like a Forever block in Scratch. 

 

 

Building with loops

Now let’s look at some examples of using loops to build bigger things quickly in Minecraft. 

Show the students this image of a whole wall of melons! Can anyone suggest how it was created? We just want their pseudocode at this point (what the code would do in their own words). 

 

 

Here’s the Python code that created the wall, with notes to explain what’s happening:

 

Challenge 11

Ask the students to have a go at creating their own never-ending wall of whatever block type they like using this method. 

But that has the obvious limitation of being never-ending, what if we only want it to be a certain size?  Well for this a different type of loop is needed. 

Take a look at this very tall tower. It’s 50 blocks high but was built very quickly. 

 

 

This is the code that built the tower. 

 

 

It works a bit like the repeat until block in Scratch.

 

It adds a condition to the While loop that keeps checking if the current value of yPos is less than 50. While it IS less than 50, a melon block is placed, then one space above it a Nether Core Reactor block is placed. So the tower is built very quickly as these two blocks are repeatedly added until yPos = 48 (the last time it will be below 50 as it increases by two with each iteration of the loop).

This flowchart summarises what the code does:

 

 

Challenge 12

Ask the students to have a go at building their own tall columns. They should make each one a different size and pattern and try and place them all fairly close together. 

 

 

How can you change the height of each column? Adjust the number used with ‘while yPos <      :’ 

How can you change the pattern? Change the block type numbers used on lines 9 and 11. You can also make it a longer repeating pattern by repeating lines 11 and 12 with new block types below line 12.  

How can you change where each one is built? Adjust the xPos and/or zPos variable values on lines 4 and 6 to another number close to your current position. 

 

Challenge 13

Have a look at this glass cube, we’ve made a greenhouse!

 

 

Using what we’ve already learned, how could we create it? Can we decompose the problem into smaller steps? 

We could separate the cube into its individual sides. Think back to the melon wall, we know how to create a number of columns side by side to form a wall. What we need to know now is how to specify exactly how many columns we want (how big the sides are).

In Scratch that would be a simple repeat block:

 

 

In Python, the same thing can be achieved with the command:

 

 

The second number (10 in this case) is the number of times it will repeat the code below the command. Anything in the loop will be indented on the page. 

In our Python code we’re actually going to need two loops, one to build the column quickly and then a second to create multiple columns and make them into a wall. Here’s the code and an explanation:

 

 

Running this code would create one wall like this:

 

 

Here’s the same code again, with another wall added (lines 16 - 21) notice that for the second wall we’re changing the Z coordinate by -1 after each column is built (line 21). This sends the wall in a different direction to the first.

 

 

We add more code (lines 23- 38) to repeat the process and add a third wall. This time we move -1 along the X axis after each column is built, this creates the third wall opposite the first one:

 

 

Lines 29 - 35 are added to create a fourth wall. Moving +1 on the Z axis after each column is created:

 

 

The last code to be added creates the roof. However, before it starts building, 9 is added to the yPos variable. This is so the roof section is added at the same height as the top level of the box.

The rest of the code is also slightly different to what’s come before as we’re building rows instead of columns this time, to form the roof. After each block is placed (line 43) 1 is added to the X position (instead of Y previously), then the X position is reset to where it began on line 44 and we move one space back on the Z axis (line 45) so we’re in place to start building the next row.

All of this is done 11 times, instead of 10, as each side of 10 blocks is added onto the previous side, so the box is actually 11 x 11 x 11.

 

 

The finished greenhouse looks like this!  You could, of course, change the block type for different sides, and make the sides different lengths. You can use this method to create all kinds of different structures, so let the students get creative! 

 

 

Challenge 14: Build it, blow it up! 

Before we try this final challenge, let’s learn something new about TNT blocks. In this version of Minecraft they don’t do much. But when you apply a data attribute to them, they become explosive! Place one and then give it a hit with your sword (left click) and it’ll explode in seconds! 

Try it with this code. ‘46’ on line 8 sets the block to TNT, the extra ‘1’ afterwards is the data attribute that makes it explosive. (You can alter other blocks in a similar way to change how they look or behave. Have a look at the table of examples at the end of this plan.)

 

     

 

 

 

Quite destructive isn’t it?!  But what would it do if we had a massive pile of TNT blocks?! Let’s find out! 

You can create large solid structures with this simple code. It has a similar effect to the greenhouse we built but fills in the middle of the box too. 

Notice mc.setBlock(x, y, z) now becomes mc.setBlocks(x ,y ,z). The other values on line 9 are stating how many blocks to add in each direction (10x10x10) plus the type of block (46 = TNT) and the 1 to make it explosive.

 

 

It should look a bit like this (below) when the code runs. Go and activate one of the blocks with your sword and then run away to watch the show! It’ll probably be really slow to render the graphics as so many things are changing at once, but it's definitely fun! 

 

 

Challenge 15: Just build it!

You can make anything you like using this method, such as this lovely diamond structure created by placing a small cube on top of a larger one. But change where the blocks are placed, change the dimensions of the object, change the block types used, and have fun! 

 

 

Plenary

Finish by giving the students a chance to have a look around the room at what they’ve been creating (if they’ve had the opportunity to get creative and build their own structures). 

Then discuss these questions to summarise the lesson and this unit of work. 

  • What different types of loops are there? 
  • How have you made use of these in your Python coding with Minecraft? 
  • What have been the biggest challenges with using a text based programming language?
  • What have you enjoyed the most?
  • What would you like more time to practise?

 


Other notes for this unit

Data values for Blocks in Minecraft

Some blocks have additional data attributes, meaning we can change their colour, orientation or how they react. Below you can see the blockType, and how we can change them:

 

 

Commenting

Programs are meant to be revisited and shared. But how do you know what a program is doing if you haven’t looked at it for a while, or if somebody else actually created it?

This is when the programmers’ comments can be really useful.

Because the code in this book has explanations wrapped around them, none of the code you are given has been “Commented”, however it is a really good idea to comment on your code so you can refer to it at a later date.

It might be a good idea at this point to revisit the hacks you have done so far and add your own comments to them.

There are 2 different ways we can comment on our code in Python:

  1. Using a hash.
  2. Using Triple Inverted Commas

Hashes are used for brief notes (Usually about a sentence). The addition of a hash tells Python to ignore the writing after it. These comments will appear as red, and are terminated by a return (Pressing enter).

# This is a short comment

Triple inverted commas are used for longer commenting (A few sentences). This type of comment needs to be opened and closed. These comments will appear in green and are terminated with another 3 inverted commas.

“”” This is a longer comment. It is used to give a deeper explanation about the code. They usually appear over multiple lines, and appear as green. The computer understands not to use them when executing the program because of the three inverted commas. BUT YOU MUST REMEMBER TO CLOSE THIS COMMENT WITH 3 INVERTED COMMAS!!!! ”””

 

Useful Links

 

< Previous Lesson