image

BUILDING THINGS IN Minecraft is great fun. You can build almost anything you can dream up, limited only by the size of the Minecraft world and your imagination. You can build houses, castles, underground waterways, multi-storey hotels with swimming pools, and even whole towns and cities! But it can take a lot of time and hard work to build complex objects with many different types of blocks, especially if there is a lot of repetition. What if you could automate some of your building tasks? Wouldn’t that look like magic to your friends?

A programming language like Python is ideal for automating complex tasks. In this adventure, you learn some magic that allows you to automatically build large numbers of blocks inside the Minecraft world and then loop through those instructions to build huge repeating structures, such as a whole street of houses (see Figure 3-1). Your friends will be amazed at your huge creations as they walk through rows and rows of houses and wonder how you built such complex structures! You also learn how to read numbers from the keyboard when your program first runs, which enables you to write programs that your users can vary without changing your program code!

image

FIGURE 3-1 A house built from Minecraft blocks

Creating Blocks

Each type of block in the Minecraft world has its own block number. If you are a regular Minecraft gamer, you probably already know a lot of block types by their numbers. However, the Minecraft programming interface allows you to refer to block types by name rather than number, which makes things much easier. The name is just a constant (you met constants in Adventure 2) that holds the number of the block type. There is a list of the most common block types and their names and numbers in the reference section in Appendix B.

The Minecraft world is divided into invisible cubes (each with its own unique coordinate), and the computer remembers the number of the type of block that’s in that invisible cube at any one time, even if it’s just a block of type AIR. As a Minecraft programmer, you can ask the Minecraft programming interface for the block type at any particular coordinate, and you can change the block type of the block at any coordinate. This changes the block that is displayed there. You can, for example, build bridges by changing the block type from AIR to STONE, and you do this in Adventure 4.

You are now going to start by writing a simple program that demonstrates this, by automatically placing a single block immediately in front of your player. Once you can place a single block in the Minecraft world, you can build anything you can possibly imagine, automatically!

  1. Start Minecraft, IDLE, and if you are working on a PC or a Mac, start the server, too. You should have had a bit of practice with starting everything up by now, but refer to Adventure 1 if you need any reminders.
  2. Open IDLE, the Python Integrated Development Environment (IDE). This is your window into the Python programming language and where you type and run all your Python programs.
  3. Choose File ⇒ New File from the menu. Save this program by choosing File ⇒ Save As and name the file block.py. Remember to store your programs inside the MyAdventures folder, otherwise they won't work.
  4. Import the modules you need for this adventure. You use an extra module called block, which holds all the constant numbers for all the block types that Minecraft supports. Do this by typing

    import mcpi.minecraft as minecraft
    import mcpi.block as block

  5. Connect to the Minecraft game by typing

    mc = minecraft.Minecraft.create()

  6. Get your player’s position into the pos variable. You use this position to calculate the coordinates of a space in front of your player, where you are going to create your new block:

    pos = mc.player.getTilePos()

  7. Now create a block in front of the player, using coordinates that are relative to your player’s position. You can read all about relative coordinates after this section. By using pos+3 in your program it ensures that the stone block doesn’t appear right on top of your player! Do this by typing

    mc.setBlock(pos.x+3, pos.y, pos.z, block.STONE.id)

  8. Choose File ⇒ Save to save your program and then run it by choosing Run ⇒ Run Module from the Editor menu.

You should now see a block of stone very close to your player! (If you don't see the block of stone, you may have to turn your player round to see it.) You have just programmed the Minecraft world to create a block in front of you. From these simple beginnings, you can now create some really interesting structures automatically by programming Minecraft.

Building More Than One Block

From this basic beginning, you can build anything! You can now extend your program to build more than one block. When you are building with Minecraft blocks, the only thing you need to remember is that you need to use some simple maths to work out the coordinates of each block that you want to create.

You are now going to extend your build.py program to create five more blocks in front of your player, using one setBlock() for each block that you want to create. The program you’re about to write creates a structure that looks a bit like the dots on a dice. To do this, work through the following steps:

  1. Start by choosing File ⇒ Save As from the Editor menu and saving your program as dice.py.
  2. Add these lines to the end of the program:

    mc.setBlock(pos.x+3, pos.y+2, pos.z, block.STONE.id)
    mc.setBlock(pos.x+3, pos.y+4, pos.z, block.STONE.id)
    mc.setBlock(pos.x+3, pos.y, pos.z+4, block.STONE.id)
    mc.setBlock(pos.x+3, pos.y+2, pos.z+4, block.STONE.id)
    mc.setBlock(pos.x+3, pos.y+4, pos.z+4, block.STONE.id)

  3. Save the program and run it. You should see a simple structure that looks like six dots on a dice, in front of your player! (See Figure 3-2.)
image

FIGURE 3-2 The Minecraft dice in front of the player shows six stone dots.

Using for Loops

Building with individual blocks allows you to build anything you like, but it’s a bit like drawing a complex picture on a computer screen by hand, one dot at a time. What you need is some way to repeat blocks, so that you can build bigger structures without increasing the size of your program.

Fortunately, like any programming language, Python has a feature called a loop. You encountered loops already in Adventure 2 with the while True: game loop. A loop is just a way of repeating things multiple times in a programming language like Python. The loop you use here is called a for loop, sometimes called a counted loop because it counts a fixed number of times.

Building Multiple Blocks with a for Loop

To understand how a for loop works, use the Python Shell to try one out by following these steps:

  1. Click the Python Shell window, just to the right of the last >>> prompt.
  2. Type the following and press the Enter key on the keyboard:

    for a in range(10):

    Python won’t do anything yet because it is expecting the program statements that belong to the loop (these are often called the loop body).

  3. The Python Shell automatically indents the next line for you by one level, so that Python knows that your print() statement belongs to the for loop. Now type this print() statement:

    print(a)

  4. Press the Enter key twice. Your first press marks the end of the print() statement; your second tells the Python Shell that you have finished the loop.

You should now see the numbers 0 to 9 printed on the Python Shell screen.

Building a Huge Tower with a for Loop

How would you like to build an enormous tower out of blocks inside the Minecraft world? Now that you know all about for loops, you can. Just follow these steps:

  1. Start writing a new program by choosing File ⇒ New File from the menu. Choose File ⇒ Save As from the menu, and name it tower.py.
  2. As usual, import the modules you need:

    import mcpi.minecraft as minecraft
    import mcpi.block as block

  3. Connect to the Minecraft game:

    mc = minecraft.Minecraft.create()

  4. If you build your tower where your player is standing it is easier to find, so your first job is to identify the position of your player by typing

    pos = mc.player.getTilePos()

  5. Your tower is going to be 50 blocks high, so start building it with a for loop that counts 50 times. Don’t forget the colon (:) at the end of the line:

    for a in range(50):

  6. The next line is indented because it belongs to the body of the for loop. The y coordinate controls height within the Minecraft world, so add the loop control variable a onto the player’s y position. This tells the program to build at an increasing height in the Minecraft world each time round the loop:

    mc.setBlock(pos.x+3, pos.y+a, pos.z, block.STONE.id)

  7. Save the program and run it. Has it worked? Your for loop should have created a massive tower in front of your player, similar to the one in Figure 3-3. Start counting—is it 50 blocks high?
image

FIGURE 3-3 This huge tower was created inside the Minecraft world using a for loop.

Clearing Some Space

Sometimes, finding enough space in the Minecraft world to build your structures can be a bit frustrating. There are usually a lot of trees and mountains around your player, meaning there is often not enough space to build big structures. You can solve this problem by writing a program that clears some space for you to build whatever you want.

Using setBlocks to Build Even Faster

All the blocks in the Minecraft world have a block identity (id) number, and this includes the blank spaces that you see in front of you, called block.AIR.id. So, if you make sure every block in a large area is set to block.AIR.id, it clears a nice space ready for building other objects. The id number (which is the block type number discussed earlier) is listed in Appendix B for the most common blocks you use. For example, block.AIR.id is 0 and block.STONE.id is 1. Remember, the empty space you see in the Minecraft world is really just a block with a block id of block.AIR.id—it is a 1 metre cube space filled with air!

The Minecraft programming interface has a setBlocks() statement that you can use to give all the blocks inside a three-dimensional (3D) rectangular space the same block id. Because this is a single request to the Minecraft game, Minecraft can optimise how it does this work, meaning it runs much quicker than if you used a setBlock() inside a for loop like you did with the tower.

Because setBlocks() works on a 3D area, it needs two sets of coordinates—the coordinates of one corner of the 3D rectangle, and the coordinates of the opposite corner. Because each 3D coordinate has three numbers, you need six numbers to completely describe a 3D space to Minecraft.

You are now going to write a useful little utility program that you can use any time you want to clear a bit of space in front of you to do some building:

  1. Start a new program by choosing File ⇒ New File from the menu.
  2. Save the program using File ⇒ Save As and choose the name clearSpace.py.
  3. Import the modules that you need:

    import mcpi.minecraft as minecraft
    import mcpi.block as block

  4. Connect to the Minecraft game:

    mc = minecraft.Minecraft.create()

  5. You want to clear space in front of the player, and you use relative coordinates to do this. First get the player’s position:

    pos = mc.player.getTilePos()

  6. Now clear a space that is 50 by 50 by 50 blocks. The bottom-left corner of your space is the position of your player, and the top-right corner will be 50 blocks away in the x, y and z dimensions:

    mc.setBlocks(pos.x, pos.y, pos.z, pos.x+50, pos.y+50, ↩
      pos.z+50, block.AIR.id)

  7. Save your program.

The fantastic thing about the program you’ve just created is that it allows you to walk to any location inside Minecraft and clear a 50 by 50 by 50 area any time you like; just run the program by choosing Run ⇒ Run Module from the menu, and the trees and mountains and everything nearby magically vanish before your eyes!

Move around the Minecraft world and re-run your program to clear lots of space in front of your player.

Reading Input from the Keyboard

Another useful thing you can do to your clearSpace.py program is to make it easy to change the size of the space that is cleared. That way, if you are only going to build a small structure, you can clear a small space but if you know you are going to build lots of big structures, you can clear a huge space first.

To do this, you use a new Python statement called input() to read a number from the keyboard first. You can then go to any part of the Minecraft world, run your program and simply type in a number representing the size of the space you want to clear; your program clears all that space for you. This means you don’t have to keep modifying your program every time you want to clear a different size of area in the Minecraft world.

Now that you understand what input() does, it’s time to add it to your program. To do this, you have to make only two small modifications to your clearSpace.py program. Follow these steps:

  1. Use input() to ask the user to type a number that represents the size of the space to clear, and store that number inside a variable called size. Just like in Adventure 2, where you used str() to convert something to a string, here you use int() to convert the string from input() into a number that you can perform calculations with. Try this line without the int() to see what happens! Type only the new lines that are marked in bold:

    pos = mc.player.getTilePos()
    size = int(input("size of area to clear? "))

  2. Modify the setBlocks() line so that instead of using the number 50, it uses your new variable size:

    mc.setBlocks(pos.x, pos.y, pos.z,
    pos.x + size, pos.y + size, pos.z + size,
    block.AIR.id)

  3. Save your program and run it by choosing Run ⇒ Run Module from the menu.

Now move to somewhere in the Minecraft world where there are lots of trees or mountains. Type a number into the Python Shell Window when prompted—perhaps choose 100 or something like that—and press the Enter key on the keyboard. All the trees and mountains near your player should magically disappear and, as in Figure 3-4, you will have a nice space to build other structures!

image

FIGURE 3-4 After running clearSpace.py you have a great clear area to build more exciting structures!

Keep this little utility program, as it will be useful later when you need to clear a bit of space in the Minecraft world.

Building a House

When playing Minecraft in survival mode, one of the first things you need to is build a shelter for your player to protect him from the dangers lurking in the Minecraft night. What if you could build a house with the touch of a button? Fortunately, when programming with Minecraft, you can turn complex tasks into just that—the touch of a button.

In the clearSpace.py program, you learned how to set a large number of blocks to the same block type, by using just one programming statement. Now you are going to learn how to use the same techniques to build a house really quickly.

One way to build a house might be to build each wall using a separate setBlocks(). For that, you need to use four separate setBlocks() and quite a lot of maths to work out all the coordinates of every corner of every wall. Fortunately, there is a quicker way to build hollow rectangular structures: All you have to do is build a huge cuboid space, and then use setBlocks() again with slightly different coordinates to carve out the inside with the AIR block type.

Before you go any further, you need take some time to make sure you understand the maths associated with the design of the house you are going to build, as you need to get the coordinates right to write the program. Figure 3-5 shows a sketch of the house design, labelled with all the important coordinates that you use to build it. When you are building something complex, it is important to sketch it out on paper and work out all the important coordinates first. This is a very special house because your Python program calculates the size and position of the doorway and windows automatically. A little later you see a little later why this is important.

image

FIGURE 3-5 A design of your house on paper, with all the important coordinates worked out

Now that you have a design for your house, try to build it by following these steps:

  1. Start a new program by choosing File ⇒ New File from the menu.
  2. Save this program by using File ⇒ Save As from the menu, and call your program buildHouse.py.
  3. Import the required modules:

    import mcpi.minecraft as minecraft
    import mcpi.block as block

  4. Connect to the Minecraft game:

    mc = minecraft.Minecraft.create()

  5. Use a constant for the size of your house. You use this SIZE constant quite a lot in your house builder code. Using a constant here, instead of using the number 20 all over your code, makes it much easier to alter the size of your house later:

    SIZE = 20

  6. Get the player’s position so you can build the house just nearby:

    pos = mc.player.getTilePos()

  7. Store the x, y and z coordinates of the player in new variables. This makes the next few program statements easier to type and read and helps when you come to do some other clever construction tasks with your house design later on. The x variable is set to two blocks away from the player position, so that the house is not built right on top of the player:

    x = pos.x+2
    y = pos.y
    z = pos.z

  8. Calculate two variables called midx and midy, which are the midpoints of the front of your house in the x and y directions. This makes it easier for you to work out the coordinates of the windows and doorway later on, so that if you change the size of your house, the windows and doorway will change their position too and fit properly.

    midx = x + SIZE/2
    midy = y + SIZE/2

  9. Build the outer shell of the house as a huge rectangular 3D area. You can choose any block type here, but cobblestone is a good one to start with, making it an old house:

    mc.setBlocks(x, y, z,
    x + SIZE, y + SIZE, z + SIZE,
    block.COBBLESTONE.id)

  10. Now carve out the inside of the house by filling it with air. Note how you have used the simple maths from your design to work out the coordinates of the air inside the house, as relative coordinates to the outer corners of the cobblestone of the house:

    mc.setBlocks(x + 1, y, z + 1,
    x + SIZE - 1, y + SIZE - 1, z + SIZE - 1,
    block.AIR.id)

  11. Carve out a space for the doorway, again using the AIR block type. You won’t use a normal Minecraft door here, because this is a huge house. Instead you create a large doorway that is three blocks high and two blocks wide. Your doorway needs to be in the middle of the front face of the house, so midx gives you that middle x coordinate:

    mc.setBlocks(midx - 1, y, z,
    midx + 1, y + 3, z,
    block.AIR.id)

  12. Carve out two windows using the block type GLASS. As this is a large house, you build the windows three blocks from the outer edge of the house and three blocks from the middle point of the front of the house. If you change your SIZE constant and run the program again, all the calculations in the program automatically adjust the positioning of everything and the doorway and windows will be in the right place so it still looks like a house.

    mc.setBlocks(x + 3, y + SIZE - 3, z,
    midx - 3, midy + 3, z,
    block.GLASS.id)
    mc.setBlocks(midx + 3, y + SIZE - 3, z,
    x + SIZE - 3, midy + 3, z,
    block.GLASS.id)

  13. Add a wooden roof:

    mc.setBlocks(x, y + SIZE - 1, z,
    x + SIZE, y + SIZE, z + SIZE,
    block.WOOD.id)

  14. Now, add a woollen carpet:

    mc.setBlocks(x + 1, y - 1, z + 1,
    x + SIZE - 2, y - 1, z + SIZE - 2,
    block.WOOL.id, 14)

    You’ll see there is an extra number at the end of setBlocks(). This number sets the colour of the carpet; in this case, it is number 14, which is red. This is explained in the following Digging into the Code sidebar.

Save your program, then move to somewhere in your Minecraft world where there is a bit of space and run your program by choosing Run ⇒ Run Module from the menu. You should see your house miraculously materialise in front of your eyes! Walk inside and explore it. Look up at the roof and through the windows, and marvel at how quickly you built this house from scratch! (See Figure 3-6.)

image

FIGURE 3-6 This house was built automatically by a Python program.

Don’t forget that buildHouse.py always builds the house relative to your player’s position. Move around the Minecraft world and run buildHouse.py again to build another house. You can build houses all over your Minecraft world—how cool is that?

Building More Than One House

Building one house is fun, but why stop there? Thinking back to your tower.py program from earlier in this adventure, it’s easy to write a for loop that repeats program statements a fixed number of times. So, it must therefore be possible to build a whole street of houses, or even a whole town, just by looping through your house-building program many times.

Before you do this, though, you’re going to improve your house-building program a little, to make sure it doesn’t get too big and complex to manage.

Using Python Functions

One of the things you might want to do later is build a whole town with houses of many different designs. The program for a whole town could become quite big and complex, but fortunately there is feature inside the Python programming language that helps you package up that complexity into little chunks of reusable program code. It is called a function.

Before you change your working buildHouse.py program to use a function for the house, try out some functions at the Python Shell to make sure you understand the idea:

  1. Click the Python Shell window to bring it to the front.
  2. Type the following into the Shell window, which defines a new function called myname:

    def myname():

    The def means ‘define a new function and here is its name'. Just like earlier with the while, if and for statements, you must put a colon (:) at the end of the line so that Python knows to expect you to provide other program statements as part of the body of this function.

  3. The Python Shell automatically indents the next line for you, so that Python knows they are part of the function. Type a few lines of print statements that print your name and information about yourself. Don’t be surprised that nothing happens as you type each line; that is correct. Be patient, all will become clear in a moment!

    print("my name is David")
    print("I am a computer programmer")
    print("I love Minecraft programming")

  4. Press the Enter key on the keyboard twice, and the Python Shell recognises that you have finished typing in indented statements.
  5. Ask the Python Shell to run this function by typing its name with brackets after it:

    myname()

  6. Try typing myname() a few more times to see what happens.

At first it might seem a little strange that you typed instructions into the Python Shell but nothing happened. Normally when you type at the Python Shell, things happen as soon as you press the Enter key, so why didn’t it work this time? This time, you did something different, however: You “defined” a new function called myname and asked Python to remember the three print statements as belonging to that function. Python has stored those print statements in the computer memory, rather than running them straight away. Now, whenever you type myname() it runs those stored statements, and you get your three lines of text printed on the screen.

Let’s put this to good use by defining a function that draws your house. Then, whenever you want a house made from cobblestone, all you have to do is type house(), and it is built automatically for you!

  1. So that you don’t break your already working buildHouse.py, choose File ⇒ Save As from the Editor menu, and call the new file buildHouse2.py.
  2. At the top of the program after the import statements, define a new function called house:

    def house():

  3. Move the midx, midy and all of your setBlocks() statements so that they are indented under the def house(): line. Here is what your program should now look like. Be careful to get the indents correct:

    import mcpi.minecraft as minecraft
    import mcpi.block as block
    mc = minecraft.Minecraft.create()
    SIZE = 20
    def house():
    midx = x + SIZE/2
    midy = y + SIZE/2
    mc.setBlocks(x, y, z, x+SIZE, y+SIZE, z+SIZE, ↩
    block.COBBLESTONE.id)
    mc.setBlocks(x+1, y+1, z+1, x+SIZE-1, y+SIZE-1, ↩
    z+SIZE-1, block.AIR.id)
    mc.setBlocks(x+3, y+SIZE-3, z, midx-3, midy+3, z, ↩
    block.GLASS.id)
    mc.setBlocks(midx+3, y+SIZE-3, z, x+SIZE-3, midy-3, z, ↩
    block.GLASS.id)
    mc.setBlocks(x, y+SIZE, z, x+SIZE, y+SIZE, z+SIZE, ↩
    block.SLATE.id)
    mc.setBlocks(x+1, y+1, z+1, x+SIZE-1, y+1, z+SIZE-1, ↩
    block.WOOL.id, 7)
    pos = mc.player.getTilePos()
    x = pos.x
    y = pos.y
    z = pos.z
    house()

  4. Notice how, in the last statement of your program, you have just put the name house(). This line runs the code that is now stored in the computer’s memory, which was set up by the def house(): statement.
  5. Save your program, move to a new location and run your program. You should see a house get built in front of you.

Building a Street of Houses with a for Loop

You are now ready to put together all the things you have learned in this chapter and build a huge street of houses. If you were building all of these houses manually by choosing items from the inventory, it might take you hours to build them, and you might make mistakes in building them or make some of them slightly smaller or larger by accident. By automating the building of houses and other structures with Minecraft programming, you can speed things up and make sure that they are perfectly built to a very precise size!

To build lots of houses, you need to add a for loop to your program as follows:

  1. So that you don’t break your existing buildHouse2.py program, use File ⇒ Save As from the menu and save a new file called buildStreet.py.
  2. At the end of the program, add a for loop above the final house()along with a line that changes the x position where the house is built. Each house is built SIZE blocks away from the previous house. The new lines are marked in bold:

    for h in range(5):
    house()
    x = x + SIZE

Save your program, then move to a place in the Minecraft world where there is a bit of space, and run the program. As shown in Figure 3-7, you should get a huge street of five houses as far as the eye can see! Walk your player into each of the houses and make sure that they have been built properly.

image

FIGURE 3-7 A street of five identical houses built automatically with a Python program

Adding Random Carpets

At this point, you should have a huge number of houses inside your Minecraft world, and things are looking pretty awesome. From a small number of lines of Python code, you’ve built some large engineering structures already!

However, you’re probably thinking that having a street of identical houses is starting to look a little boring. How can you make the houses slightly different to add interest to your street?

One way to do this is to write a few different house() functions like cottage(), townHouse() and even maisonette(), and modify your program to use these different functions at different places to add some variety to your street designs.

Another way to make your structures more interesting is to slightly change part of them, such as the carpets, in a way that is different every time you run the program. This way even you, the programmer, won’t know quite what you have created until you explore all the houses!

Generating Random Numbers

Computers are very precise machines. In many aspects of everyday life, we all rely on computers to be predictable and to do the same thing every time a program is run. When you pay £10 into your bank account, you want to make sure that exactly £10 makes it into the part of the computer’s memory that holds your balance, every time, without fail. So the concept of randomness might seem quite unusual to such a precise system.

However, one area where randomness is really important is in game design. If games did everything exactly the same every time, they would be too easy to play—not fun or challenging at all. Almost every computer game you play has some kind of randomness in it to make things slightly different each time and hold your interest.

Fortunately for your Python programming, the Python language has a built-in module that generates random numbers for you so you don’t have to write the code for this yourself; just use this built-in random number generator instead.

To make sure you know what these random numbers look like, try this out at the Python Shell:

  1. Click the Python Shell window to bring it to the front.
  2. Import the random module so that you can use the built-in random function, by typing:

    import random

  3. Ask the program to generate a random number between 1 and 100 and print it to the screen:

    print(random.randint(1,100))

    You should see a number between 1 and 100 appear on the screen. Type the print statement again. What number do you get this time?

  4. Now use a for loop to print lots of random numbers. Make sure you indent the second line so that Python knows that the print statement is part of the for loop:

    for n in range(50):
    print(random.randint(1,100))

The two numbers inside the brackets of the randint() function tell it the range of numbers you want it to generate; 1 is the smallest number you should expect it to generate, and 100 the largest.

Laying the Carpets

Earlier in this adventure you used an extra number in the setBlocks() function to make the colour of the woollen carpet red, using the extra data of WOOL. The allowed range of extra data numbers for WOOL is between 0 and 15—in other words, there are 16 colours you can choose from. Now use the following steps to change your house-building program to generate a random number and use that as the colour of WOOL for the carpet in the house:

  1. Save your buildStreet.py with File ⇒ Save As from the menu, and call it buildStreet2.py.
  2. Add this import statement at the top of the program to gain access to the random number generator:

    import random

  3. Change your house() function so that it generates a random carpet colour each time it is used. As WOOL can have an extra data value ranging between 0 and 15, that is the range of random numbers you need to generate. Store the random number in a variable called c so that the carpet-building line doesn’t become too long and hard to read, and make sure that both of these lines are indented correctly, as they are both part of the house() function:

    c = random.randint(0, 15)
    mc.setBlocks(x+1, y+1, z+1, x+SIZE-1, y+1, z+SIZE-1, ↩
    block.WOOL.id, c)

  4. Save your program.

Now you need to find somewhere in your Minecraft world that has plenty of space to build more houses! Move around the world and find somewhere to build, and then run your new program. It should generate another street of houses, but this time, when your player explores inside the houses, the carpet in each house should be a different, random colour. See Figure 3-8 to see what these new houses look like, but your player needs to actually go into each house and look at the carpets so that you can check they are indeed all different!

image

FIGURE 3-8 Each house has a random carpet colour.

Quick Reference Table

Command

Description

import mcpi.block as block

b = block.DIRT.id

Importing and using the block name constants

mc.setBlock(5, 3, 2, block.DIRT.id)

Setting/changing a block at a position

block.AIR.id

block.STONE.id

block.COBBLESTONE.id

block.GLASS.id

block.WOOD.id

block.WOOL.id

block.SLATE.id

Useful block types for houses

mc.setBlocks(0,0,0,5,5,5,block.DIRT.id)

Setting/changing lots of blocks in one go

All of the new Python and Minecraft statements you learned in this chapter are listed in the reference section in Appendix B.

You can find a complete list of block id numbers on the Minecraft wiki: http://minecraft.gamepedia.com/Blocks. Minecraft extra data values are taken from https://minecraft.gamepedia.com/Data_values.

Wool is a very useful block type to build with, as you discovered when laying the random carpets in your street of houses. Here is a reference to the different colours that can be used with the block.WOOL.id block type. Look back at your buildStreet2.py program to see how to provide this extra data to the Minecraft API.

0 white

1 orange

2 magenta

3 light blue

4 yellow

5 lime

6 pink

7 grey

8 light grey

9 cyan

10 purple

11 blue

12 brown

13 green

14 red

15 black

Further Adventures in Building Anything

In this adventure, you’ve learned how to create single blocks and whole areas of blocks inside the Minecraft world using a single line of a Python program. You’ve built some pretty impressive structures already. You’ve learned that with functions you can split up your program into smaller logical units and, with for loops, repeat things over and over again. With this knowledge you should be able to build almost any structure you could possibly imagine!

  • Using the techniques learned in this adventure, write one function for each of the six faces of a dice. Write a loop that spins round a random number of times, showing a different dice face each time round. Use random.randint() to stop on a random pattern, and challenge yourself and your friends to try and guess the number the Minecraft dice will stop on.
  • Make a list of other things you could randomise about your street. Perhaps do some research by walking down your own street and seeing how the houses differ from each other. Define more house functions, one for each type, and try to build more complex streets with houses in lots of different styles.
  • Get together with some friends and join all your different house-building programs into one big program. Use it to build a whole community of houses of different styles inside your world. Marvel, as you walk around your new town, at how much variety there is in house design!
image

Achievement Unlocked: Designer of remarkable buildings and builder of amazing huge structures inside Minecraft!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset