Setting Block Challenges

Python Minecraft - Lesson 5

Objectives

  • To use and understand 3D coordinates to create structures
  • To understand and make use of variables in a program
  • To create programs using a text based programming language
  • To use logical reasoning to solve programming challenges

 

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. 

Show them the final code from the last lesson and ask them to Recap building the stack of ice blocks in the last lesson. How did we make the stack get taller and build upwards?

 

When X, Y and Z were set to the player’s position, a block was placed at those coordinates (line 9). 1 was then added to the Y variable to move vertically upwards one space, then another block was set to that position. This was then repeated to create a stack of three blocks. 

Explain that this code and method of setting a variable, placing a block, adding to the variable and placing another block,  will be the framework for a number of challenges they will need to try and solve over the next couple of lessons. All involve creating different structures and patterns. 

 

Main tasks

Introduce the challenges that they’ll be tackling. These are available as a worksheet if it's easier for the students to have a copy in front of them: 

 

Ask the students: Can you edit the previous code to create:

  • A stack of 5 melon blocks?


 

  • A stack that has gaps in it?


 

  • A 9 block stack with a repeating pattern of 3 block types?


 

  • A sandstone staircase?

 

 

Set the students to work solving these problems either in their pairs or as teams.

 

Plenary

Go through each of the challenges asking the students to explain how they solved each one, and how they adapted the code to achieve the different structures. Solutions to each challenge are below. 

 

Solutions:  10A - A stack of 5 melons

What’s happening here? 

Just like the ice stack in the last lesson, this melon tower is created in the same way. The X, Y and Z coordinates of the player’s current position are used as our three positional variables (xPos, yPos and ZPos).  A variable for ‘blockType’ is also declared and the number for melon (103) assigned.  

A block is set to the player's position, then 1 is added to the yPos variable. This then allows the next block to be placed one space up on the Y axis and on top of the first block. This is repeated three more times so each time 1 is added to the yPos variable and the blocks get placed one place higher each time, creating a tower of five melon blocks.

 

 

Solutions: 10B - A Stack with gaps

What’s happening here? 

The same as the last challenge really, we’ve just done away with the blockType variable so we can alter the type of block  each time one is set. There’s no magic secret to creating the gaps either, just remembering that ‘air’ is a block type (number 0) and can be used like any other block, just, in this case, to leave what looks like an empty space (remember the ‘glitch’?). The block number is added after the final comma, inside the brackets each time a block is set. 

 

 

Solutions: 10C - A repeating stack of three different blocks

What’s happening here? 

Again, very similar to the last challenge. Having no blockType variable allows for a pattern to be created using any three block types you choose. In this example we used melon (103), brick (45) and ice (79).

 

 

Solutions: 10D - A sandstone staircase

What’s happening here? 

The solution to this problem again follows a very similar principle to the previous ones. The same technique is used to add blocks that get gradually higher, however as we go up we are also moving forwards on the X axis this time.  Each time a block is set we add 1 to the yPos and xPos variables. This creates the staircase effect. 

Changing the direction of the stairs can be done by changing all thexPos = xPos +1’ commands to ‘xPos = xPos -1’.  This will take away 1 from the xPos variable each time and therefore move in the opposite direction on the X axis.

 

 

You can also rotate the staircase by 90 degrees by changing all thexPos = xPos +1’ commands to zPos = zPos +1’ (or -1). And moving along the Z axis (front to back)  instead of the X axis (left to right). 

Or try making this archway by first creating the staircase (as above) and then adding another six blocks, each time changing the yPos variable by -1 and the xPos variable by +1.

 

 

< Previous Lesson

Next Lesson >