Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: No Current Assignments

Computer Programming I :: Lessons :: Minecraft While Loops

Turtles

MinecraftEdu and ComputerCraft are Minecraft mods that introduced programmable turtles to Minecraft. You can program these turtles to do just about anything you could do on your own in Minecraft such as building, mining, or fighting. To interact with a turtle you must use a turtle remote. Once you interact with a turtle using the remote, you will see a window similar to the following:

MinecraftEdu Turtle Controls

The controls above do the following:

MinecraftEdu Move Turtle Up Command Move the turtle up one block

MinecraftEdu Move Turtle Down Command Move the turtle down one block

MinecraftEdu Move Turtle Forward Command Move the turtle forward one block

MinecraftEdu Move Turtle Backward Command Move the turtle backward one block

MinecraftEdu Rotate Turtle Counterclockwise Turn the turtle to the left once

MinecraftEdu Rotate Turtle Clockwise Turn the turtle to the right once

MinecraftEdu Turtle Vision Use turtle vision to see what the turtle sees

MinecraftEdu Place Block Place a block from the currently-selected slot in front of the turtle

MinecraftEdu Undo Program Undo the most recent program

MinecraftEdu Step Program Run the next line of the program

MinecraftEdu Run the Turtle Program Run the entire program

MinecraftEdu Turtle Inventory Open the turtle's inventory as seen below and transfer items from your own inventory

MinecraftEdu Turtle Inventory

MinecraftEdu Turtle Customization Customize your turtle

MinecraftEdu Program Your Turtle Open the code window to edit your turtle's program

Turtle Visual Editor

When you enter the turtle's code editor, you will enter the visual editor. You can also use the code editor to edit the actual code using the progamming language Lua, but that is beyond the scope of this class. When you open the visual editor you will see the following screen.

MinecraftEdu Visual Editor

You will see the following areas in the visual editor:

MinecraftEdu Turtle Commands

This scrollable area contains all of the turtle commands you can use for your program. Some of the commands are things you could control manually using the turtle controls from the previous screen, but most of the commands are only available here. You can drag-and-drop these commands to add them to your program.

MinecraftEdu Turtle Visual Editor

This area is the visual editor where you can place the commands you drag-and-drop from the turtle command window. The commands are executed from left to right and from top to bottom.

MinecraftEdu Turtle Program Locking

This area lets you switch between the visual editor and the code editor. It also lets you lock and unlock your turtle. By default, a turtle can only be edited by its creator. You can change it to demo mode to allow anyone to run but not edit the program. You can also unlock the turtle to allow anyone to run or edit the program.

MinecraftEdu Save Turtle Program

This area lets you save your program. Once you give your program a name, you can access this program from any turtle. This is helpful for situations where you lose your current turtle or need to use your program on more than one turtle. You can also hit the disk icon to save your program to a disk that you can share with other players. When you are holding a disk, you can hold down right-click to "eat" the disk to add it to your library of programs. This area also has commands to undo, step through, and run your program.

While Loops

MinecraftEdu Turtle Canyon

Loops are a powerful tool in programming. A loop is a command that allows a segement of code to repeat based on a given condition. A while loop runs while the given condition is true. Let's create a program that has a turtle build a set of stairs along the wall in the image below to the right.

Let's assume that the turtle is already facing the wall where we want to build the stairs. Let's also assume we want to build the stairs to the left of the turtle's location. Below are the steps of our algorithm to build the stairs:

  1. While there is no block in front of the turtle, move forward one block.
  2. Once the turtle reaches the wall, turn left.
  3. Set the current height of the turtle to 1.
  4. While the height of the turtle is less than 5, repeat steps five through eight.
  5. Move up one block.
  6. Place a block from slot 1 below the turtle.
  7. Move forward one block.
  8. Increase the current height by 1.

Now that we have written an algorithm for the stairs program, we need to translate it into turtle command blocks. The following image shows the entire stairs program, which we will examine step-by-step.

MinecraftEdu Turtle Visual Editor

Step 1: While there is no block in front of the turtle, move forward one block.

MinecraftEdu While Loop This code runs while the block in front of the turtle is air. We use the inspect block command to see if the block in front of the turtle is an air block, which would mean there is nothing in front of the turtle. If the block in front of the turtle is an air block, we run the code in between do and end. The only command within the do and end blocks is a single move forward command.


Step 2: Once the turtle reaches the wall, turn left.

MinecraftEdu Rotate Counterclockwise After we exit the while loop, we know we've reached the wall because the while loop will only stop when the block in front of the turtle is not air. That means we can run a turn left command immediately after the while loop.

Step 3: Set the current height of the turtle to 1.

MinecraftEdu Variable This step creates a variable, which is used to store data. In this case, we are storing the current height of the turtle and assumming the bottom of the wall represents a height of 1. The variable is named "height," but the visual editor only shows the first two letters of a variable name.

Step 4: While the height of the turtle is less than 5, repeat steps five through eight.

MinecraftEdu While Loop In this step, we create another while loop. This one runs while our variable, height, is less than 5. Remember that the do statement indicates the start of a loop's code and the end statement, which isn't shown in this image, represents the end of the loop's code.

Step 5: Move up one block.

MinecraftEdu Move Up Command This is a simple step that just uses the move up command. Remember that the commands are executed from left to right so it is okay to have multiple commands on the same line.

Step 6: Place a block from slot 1 below the turtle.

MinecraftEdu Place Block Command The place block command allows you to place a block in front of the turtle (which is the default), below the turtle, or above the turtle. It takes a block from the currently-selected slot, which is slot 1 by default. There is a select slot command that allows you to change the currently-selected slot, but that isn't necessary for this program.

Step 7: Move forward one block.

MinecraftEdu Move Forward Command This is another simple command that simply moves the turtle forward one block in preparation to potentially place another block.

Step 8: Increase the current height by 1.

MinecraftEdu Increase a Variable by One This command sets the height variable to itself plus one, which essentially increases the variable by one. There are shortcuts for doing this in some programming languages, but in Minecraft you must specify that you are setting a variable to its current value plus one.

After the second while loop ends, the turtle should have created a set of stairs using blocks from its first slot like the one shown below.

Minecraft Stairs
Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram