APC Australia

Playing with Minecraft blocks

-

As a learning tool, Minecraft is awesome. Kids play the game and learn how to create blocks of lava, diamond and water. But for younger players, the syntax of the commands is difficult. So for our project this issue we shall create an easy-to-use series of commands, cheekily called Minecraft Zero after GPIO and GUI Zero libraries, that enable kids to simply have fun, and learn a little Python along the way.

On your Raspberry Pi, open the Python 3 editor, found in the Programmin­g menu. Once opened, click File>New to create a new blank document. Now click File>Save and call the file mczero.py. Remember to save your progress regularly.

Our project is made up of a series of functions, blocks of Python code that we can use by calling their name. The first of these is the most simple, and that’s to post a message to the Minecraft chat window. As with all functions we need to define a name, and we shall call it chat , but you will see msg in parenthesi­s. This msg is the text we want to display in the chat window. def chat(msg): Inside the function we then use Minecraft’s library, which we shall later import and rename to mc , postToChat function. Note that we also use a str() helper to ensure that any data used for messages is converted to a string because the postToChat function only works with strings:

mc.postToChat(str(msg))

TAKE A TRIP

The next function is called teleport and it’s used to send the player to any point in the world. All it needs are x, y, z coordinate­s, found in the top left of the screen. This function works in a similar manner as chat in that we invoke the function with a parameter which is the x, y, z position. Then in the function we set the position of the player (setPos) to those coordinate­s: def teleport(x,y,z):

mc.player.setPos(x, y, z) Kids love to soar high into the sky, and then fall back to the world. Well, with the drop function we can do that. This function takes a parameter, the height (in blocks) to drop Steve from. This is then used in the function after we first learn of the players current position using getPos to update the x, y, z coordinate­s. Then using setPos we change the value of y so that it’s increased by the value of height . def drop(height):

x, y, z = mc.player. getPos()

mc.player.setPos(x, y+height, z)

The next important part of Minecraft is changing blocks. Using Python we can edit any part of the world, changing grass to flowing lava, diamond or gold. To do that we need to alter the blocktype and our setblock function handles this by changing the block on which we’re stood to anything that we pass as a parameter. If you don’t know the blocktypes (numbers) don’t worry, we cover that in our blocks() function later. def setblock(blocktype):

x, y, z = mc.player. getPos()

mc.setBlock(x, y, z, blocktype)

CUBES ON DEMAND!

Our cube function takes two parameters: the size of the cube (in blocks) and the type of blocks it’s made from. So we can create cubes of diamond and use them to obstruct our enemies! In this function we use setBlocks and give two x,y,z positions. The first being the player’s position plus one block on each axis, and the second position being the player’s position plus the size of the cube. Minecraft then fills in the values between them to create a solid cube, like so: def cube(size,blocktype):

x, y, z = mc.player. getPos()

mc.setBlocks(x+1, y+1, z+1, x+size, y+size, z+size, blocktype)

But what if you want a big, hollow cube? Well, our hollow_cube function will do that for you. It creates a solid cube of diamond blocks (blocktype 57), and then we create a smaller hollow cube inside it using a cube of air blocks ( blocktype 0): def hollow_ cube(size):

x, y, z = mc.player. getPos()

mc.setBlocks(x+1, y+1, z+1, x+size, y+size, z+size, 57) size = size - 2 mc.setBlocks(x+2, y+2, z+2, x+size, y+size, z+size, 0)

TICK, TICK, BOOM!

This function creates a trail of active TNT that follows your every step. When you hit it with a sword, it explodes! For this we need to get the players position, then set the block at their feet to TNT (blocktype 46), but we need to also pass the value 1 so that the TNT can be explosive. Then we sleep for 0.1 to slow down the number of TNT blocks on screen. This function is best used inside a while True: loop so that it continuous­ly runs. def tnt_trail():

x, y, z = mc.player. getPos() mc.setBlock(x, y, z, 46,1) sleep(0.1)

Our tnt_cube function creates a cube that’s full of TNT! Just pass the size parameter, hit the cube and run! Note that on older Pi, the maximum size should be 10x10x10. For Pi 3 you can risk 20x20x20, but bear in mind that it may lock up the Pi: def tnt_ cube(size):

x, y, z = mc.player. getPos() tnt = 46 mc.setBlocks(x+1, y+1, z+1, x+size, y+size, z+size, tnt ,1)

Building long lines of blocks is boring! But with our new slab function we can create large slabs of blocks by passing the length (l), width (w) and the blocktype. Essentiall­y, this is the same as our cube function, but the value of y never changes, which forces the slab to be built in only two axes: def slab(l,w,blocktype):

x, y, z = mc.player. getPos()

mc.setBlocks(x+1, y+1, z+1, x+w, y+1, z+l, blocktype)

The last in-game function is called fire_feet and it places flaming torches (blocktype 50) at your feet wherever you go. This is best used with a while True loop, just like the tnt_trail function. def fire_feet():

x, y, z = mc.player. getPos()

mc.setBlock(x+1, y, z+1, 50,1)

sleep(0.1)

The next two functions are built-in help tools. The first is called blocks() and it stores a dictionary (called block_dictionary), a data type to store large amounts of data that are referenced via a key:value relationsh­ip. In this case the keys are the names of the blocks, and the values are the blocktype numbers. So, for example the key AIR return the blocktype 0. There are loads of different blocks in the dictionary, and to show them all we use a for loop that will iterate over each of them and print the key and value, with a 0.1 second pause between each line. You can find the code listing for this function in the download for this project.

The final function is called commands() and it simply prints out all of the commands and what they do. Handy to remind ourselves when in the middle of battle! You can find the code listing for this function in the download for this project.

To run all of the code we use an exception handler, that will try to run the code. In this case it will import the Minecraft library, the block library and import sleep from the time library. Then it creates a connection to the running Minecraft game. If this works then text is displayed in the Python shell and

Minecraft chat window advising the player of the help functions ( blocks() and commands() ), but if the connection fails then an exception is raised and an error is printed to the shell.

Save the code, launch a Minecraft game and when you’re in the world, click Run>Run Module in the Python editor. Now try out the commands and prepare to cause havoc and mayhem!

“Kids love to soar high into the sky, and then fall back to the world. Well, with the drop function we can do that.”

 ??  ?? When the mczero library is first used, it’ll print helpful messages to the Python shell and theMinecra­ft chat window.
When the mczero library is first used, it’ll print helpful messages to the Python shell and theMinecra­ft chat window.
 ??  ?? Using the commands() function we found the function to generate a cube, so we made a 10x10x10 of furnaces, because you can! Try it
Using the commands() function we found the function to generate a cube, so we made a 10x10x10 of furnaces, because you can! Try it

Newspapers in English

Newspapers from Australia