Using 3D coordinates

Python Minecraft - Lesson 3

Objectives

  • understand several key algorithms that reflect computational thinking [for example, ones for sorting and searching]; use logical reasoning to compare the utility of alternative algorithms for the same problem
  • use 2 or more programming languages, at least 1 of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables or arrays]

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. 

 

Recap the last lesson and main points that were covered. What is the name of the programming language we’ve been using? (Python). What are the main differences between using a text based programming language and a block based language like Scratch?  What feature of the game did we affect with our code last time? (the chat pane in the game). Where can you find information about errors you make to help you debug your code? The Python shell is where error messages are reported.

 

Coordinates for positioning the player

One major problem with Minecraft is getting around the world quickly. Flying helps, but wouldn’t it be great if we could teleport around the world using code?  Today we’re going to learn how to do just that using code, and coordinates! 

 

Ask the students what is a coordinate? They should have experience of using coordinates in maths lessons, with maps, games such as Battleships and in their coding if they have used things like Scratch where 2D (X and Y) coordinates are commonly used to position sprites. 

 

(Battleship images courtesy of: https://www.flickr.com/photos/jking89/1363983490)

 

If we look very closely at the top left hand corner of the Minecraft screen we can see some text. It says ‘pos: a number, a number, a number’. 

 

x,y and z values for Steve in the Minecraft window.

 

This is our character’s current position in the Minecraft world. In fact, they are X, Y and Z coordinates

 

Because Minecraft is a 3 dimensional environment , there are 3 axes we always have to consider. In addition to the X and Y coordinates students should be used to, the Minecraft environment also has a Z axis that deals with depth (See the diagram below). In order to teleport around the Minecraft world, we need to set the character’s  X, Y and Z coordinates. 

The three axes represent:

 

In the above example the character is at 0.5 on the X axis, 5.0 on the Y axis, and 0.5 on the Z axis. You can simplify the coordinates for the students by telling them to ignore the number beyond the decimal place, it’s not essential to include that level of detail when describing the position or using it in your code later. Also take note that sometimes the coordinates will be a negative number.

Ask the students, what are your character’s current coordinates?  Compare them with the people working next to you.

Because we all generated new worlds (that will all be a bit different) it’s likely that all our coordinates will be different. Knowing where you are standing in the game now can be really useful though. 

Let’s try getting airborne!

 

Challenge 4: Take off!

The code below will teleport Steve to a different part of your Minecraft world. So let’s go straight up in the air to start with. 

 

In this code we are using the mc.player.setPos command. This command finds the player and sets it’s position. In order to set the player’s position in the Minecraft world we need to assign 3 values:

  1. A position on the X axis
  2. A position on the Y axis
  3. A position on the Z axis

 

In this example rather than assigning numbers directly, we have assigned the coordinates to the variables (xPos, yPos, zPos), but the following code would have worked exactly the same:

mc.player.setPos(0, 125, -105)  

When you run the code above, your character will teleport up in the sky above your current position, but get ready, because what goes up must come down! (hence the ‘Aaaaaargh!’).

The Y axis deals with height, so when we set the character’s position Y position to 125 we’re lifting them up as high as they can go in the Minecraft world, but there’s gravity in this game (apart from when you’re in flying mode) so you’ll drop back down to earth. 

 

Did you know... ? - Variables

A variable is a space in the computer’s memory that can be used to store something. It can store words, whole numbers or decimal numbers. The program will take the data, store it, recall it, change it, pretty much do whatever we want with it. 

Every variable has 3 parts:

  • Name - it’s important to choose a name that helps you recognise the variable in your program
  • Type - is it a string (text), an integer (number) or a float (fractional value/decimal)?
  • Value - This is the data you are going to save, the whole point of the variable.

 

In the example above we have declared 3 variables (xpos, ypos and  zpos) and given them integer values (numbers). We use these values in the mc.player.setPos command, and this teleports the player to the X, Y and Z coordinates we have given in those 3 variables.

 

Challenge 5: Teleport

Let’s try teleporting somewhere else in our world, but consider that Minecraft randomly creates worlds. Because of this, what could go wrong if we teleport to a random coordinate in the world?

 

 

If you teleport in a new world, you might not know what is at the position you decide to teleport to. For example,  we might not know what is at:

 

X = 10, Y = 11, Z = 12. 

 

It could be an open area, which will be fine, but it could equally be the inside of a mountain or something else. So, if your screen goes black once you run the program, you have teleported inside a block and  are stuck

This isn’t a huge problem. All you need to do is change the coordinates and run the program again to get unstuck.

Ask the students to try teleporting to four or five different locations in their world. Just change the values for xPos, yPos and zPos and re-run the code. 

The only limits they need to be aware of are the size of the world. Minecraft Pi edition only creates worlds that are -125 to 125, so you can’t go past these extremities!

 

So….Why is this important?

In the modern world of technology we use 3 Dimensional coordinates quite a lot. Every time we type a destination into a Sat Nav, the GPS coordinates for a location all have 3 dimensions.

Our location on Google Maps does the exact same thing… Geocaching, Pokemon Go… They all use 3D coordinates to record our position and do different things. So… If you know how to use variables for positions you can do lots of funky stuff!

 

Setting blocks

So we can control the console, and the position of the player, but what you really want to do is start building and playing with blocks. 

As well as using coordinates to move Steve around, we can also use them to place blocks in our Minecraft world, like this melon block! 

 

 

Ask the students to take a note of your current coordinates and then try this code out:

 

Ask them can you explain what this code does? The command  ‘mc.setBlock’ places a block at the coordinates that have been specified in the brackets. The fourth number (103) is not a coordinate, but we’ll get to what that controls later! 

 

 

Once that first block has been placed ask the students How could you alter the code to add another block on top of the first one?  Change the Y value by 1

 

How could you add another to the right? Take 1 back off the Y value and add 1 to the X value.

 

And then add a fourth to make a 2 x 2 stack? Leave the X value as it is and add 1 back onto the Y value.

 

Plenary

What is a coordinate?  They are a set of values that can be used to specify an exact location.

How have we used them today?  To place our character and blocks in exact locations in the Minecraft world.

What could go wrong when teleporting around the Minecraft world?  You could place yourself inside a block, but that can be solved with another teleport.

What is different about the coordinates we’ve used today to those you might use in Scratch or on a map?  Today’s coordinates have been for a 3D environment, which uses three axes (X, Y and Z) and requires three numbers. 

< Previous Lesson

Next Lesson >