Lesson 4

Introducing functions and building strategies

At the end of this lesson, you will have your first project to complete. You will write one or more functions that create a building.

Building strategy 1: Hollow Box method

The idea for this method, or algorithm, is to create a cuboid of one type of block, then make a cuboid of air that is one block smaller. That gives you an empty box. Then you can add more to create windows and other details.

Things to notice:

  • The keyword "def" is for defining a function. Here you are defining a function called makeBuilding().
  • Everything indented under def makeBuilding(): is part of that function. In general, indents are VERY IMPORTANT in Python. In other other languages, like Arduino, JavaScript, and Java, indents are more for readability than syntax. Python does away with curly braces so it uses indents to determine the contents of code blocks.
  • Line 10: The makeBuilding() statement is OUTDENTED because you are "calling" that function to make it run. In Python you must define a function sequentially in the program before you can call, or run, it.
  • This empty box of wood is surrounding you! Next you will make a door and windows, which is more user-friendly.

Here I'm adding windows and a door. Things to notice:

  • I added comments to keep track of what each part of code is doing
  • Doors are tricky. You have to set the bottom of the door first and the top second. They have an ID number (the 0 and 8 at the end) that set the door's position.

Building Strategy 2: Wall by Wall

The idea for this approach, or algorithm, is to create each wall separately. This approach would be more useful if you want to have a more interesting shaped house than a simple box shape.

Things to notice:

  • Okay, sure, this is a weird house...
  • The keyword "def" is for defining a function. Here you are defining a function called makeBuilding().
  • Everything indented under def makeBuilding(): is part of that function.
  • The ceiling is slanted glass! A for loop is used to change the y (height) of each row of glass.

Lesson 4 is done!

Minecraft Project 1: Now choose a building strategy and make your own house!