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:
Get player's current location
Build house around player:
Build four walls by looping over Y coordinate (height): playerY to (playerY+HOUSEHEIGHT)
Loop over X coordinates: (playerX-HOUSERADIUS) to (playerX+HOUSERADIUS)
Place a wall block at Z=playerZ-HOUSERADIUS
Place a wall block at Z=playerZ+HOUSERADIUS
Loop over Z coordinates: (playerZ-HOUSERADIUS) to (playerZ+HOUSERADIUS)
Place a wall block at X=playerX-HOUSERADIUS
Place a wall block at X=playerX+HOUSERADIUS
Build solid roof by looping over X coordinates: (playerX-HOUSERADIUS) to (playerX+HOUSERADIUS)
For each X coordinate, loop over Z coordinates: (playerZ-HOUSERADIUS) to (playerZ+HOUSERADIUS)
Place roof block at (X, playerY+HOUSEHEIGHT, Z)
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.
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.