Meeting 2020-03-03

Looping in Python Part 3

This is the last class of Term 2 - I hope to see you all next term for the continuation of Programming Minecraft in Python!

Last week we introduced the instant house project, which makes use of for loops, range() functions, and the mc.setBlock command. I think that most people didn't get close to finishing, and this is really important and useful stuff, so I'd like to go back to that project and make sure everyone has it working.

Project: Build an Instant House

My step-by-step comments weren't as helpful as I had hoped so I will write an outline of what needs to be done as well:

    1. Minecraft Initialization

    2. Program constants

    3. Get player's current location

    4. Build house around player:

      1. Build four walls by looping over Y coordinate (height): playerY to (playerY+HOUSEHEIGHT)

        1. Loop over X coordinates: (playerX-HOUSERADIUS) to (playerX+HOUSERADIUS)

          1. Place a wall block at Z=playerZ-HOUSERADIUS

          2. Place a wall block at Z=playerZ+HOUSERADIUS

        2. Loop over Z coordinates: (playerZ-HOUSERADIUS) to (playerZ+HOUSERADIUS)

          1. Place a wall block at X=playerX-HOUSERADIUS

          2. Place a wall block at X=playerX+HOUSERADIUS

      1. Build solid roof by looping over X coordinates: (playerX-HOUSERADIUS) to (playerX+HOUSERADIUS)

        1. For each X coordinate, loop over Z coordinates: (playerZ-HOUSERADIUS) to (playerZ+HOUSERADIUS)

          1. Place roof block at (X, playerY+HOUSEHEIGHT, Z)

      1. Place blocks for door opening

This week we will use for loops and the mc.setBlock() function to build an instant house around where the player is standing in the Minecraft world. To make it a little easier, I will give you a Python file that gives you step-by-step instructions for building as comments, and you just have to figure out the Python code to make the steps happen. Feel free to review the information from last week on for loops and the range()function if you need a reminder.

### 1. Minecraft Initialization

from mcpi.minecraft import Minecraft

mc = Minecraft.create()

### 2. Program constants - names in UPPERCASE


ROOFBLOCK = 3 # also DIRT but you can change it


HOUSEHEIGHT = 3 # height of house walls

HOUSERADIUS = 3 # half of house length and width

### 3. Get the player's current location

# save location as playerX, playerY, playerZ for convenience

### 4. Build the house around the player

# for loop in Y from playerY to playerY+HOUSEHEIGHT levels to build the walls

# for loop in X from (playerX - HOUSERADIUS) to (playerX + HOUSERADIUS)

# place a wall block at (X,Y,(playerZ-HOUSERADIUS))

# place a wall block at (X,Y,(playerZ+HOUSERADIUS))

# for loop in Z from (playerZ - HOUSERADIUS) to (playerZ + HOUSERADIUS)

# place a wall block at (playerX-HOUSERADIUS,Y,Z)

# place a wall block at (playerX+HOUSERADIUS,Y,Z)

### nested loop over X and Z to build the roof at Y=playerY+HOUSEHEIGHT

# for loop in X from playerX-HOUSERADIUS to playerX+HOUSERADIUS

# for loop in Z from playerZ-HOUSERADIUS to playerZ+HOUSERADIUS

# place a roof block at (X,playerY+HOUSEHEIGHT,Z)

### Make a doorway by placing air blocks

# place a DOORBLOCK at (playerX, playerY, playerZ-HOUSERADIUS)

# place a DOORBLOCK at (playerX, playerY+1, playerZ-HOUSERADIUS)

Once you have added all the lines of code, save you program in the %appdata%\roaming\.minecraft\mcpipy folder and try running it!

You may find that your house is missing some corners and edges - Python coordinate ranges strike again! In a range(start,end) function, the for loop doesn't actually get to the end value. You will need to add one to many of the ending coordinates to build a perfectly square house.

Project: Add Multiple Stories

If you have finished the instant house program and want another challenge, add another level of for loops outside the house building routine to make a multi-story house. You will need to add an offset to all the y-coordinates that depends on the floor of the house you are building.

You can also experiment with different block types like wood and cobblestone, and adding windows on walls that don't have doors.