Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi: Amazing Projects from Scratch

You're reading from   Raspberry Pi: Amazing Projects from Scratch Explore the powers of Raspberry Pi and build your very own projects right out of the box

Arrow left icon
Product type Course
Published in Sep 2016
Publisher
ISBN-13 9781787128491
Length 593 pages
Edition 1st Edition
Arrow right icon
Authors (4):
Arrow left icon
Matthew Poole Matthew Poole
Author Profile Icon Matthew Poole
Matthew Poole
Ashwin Pajankar Ashwin Pajankar
Author Profile Icon Ashwin Pajankar
Ashwin Pajankar
Richard Grimmett Richard Grimmett
Author Profile Icon Richard Grimmett
Richard Grimmett
Arush Kakkar Arush Kakkar
Author Profile Icon Arush Kakkar
Arush Kakkar
Arrow right icon
View More author details
Toc

Chapter 2. Minecraft Pi

In the previous chapter, we learned how to set up the Pi and looked at Python and turtle modules briefly. In this chapter, we are going to explore the Pi version of a popular computer game, Minecraft. We will also learn how to program Minecraft with Python as the programming interface and use it to script user actions and build wonderful things.

We will cover the following topics in this chapter:

  • Introducing Minecraft Pi
  • Playing Minecraft Pi
  • Programming Minecraft Pi with Python

Introduction to Minecraft Pi

Minecraft is a very popular open world game. Like all other open world games, the player can freely explore the virtual world in a Minecraft Pi game. Minecraft was created by Markus Persson, and the game was later developed and published by the company Mojang, which is a Microsoft Game Studios subsidiary now. Jens Bergensten is the current lead designer and lead developer for the game. The alpha version of the game was released for PCs in 2009, and the complete version was released in 2011. The game is available for various platforms, which include PCs, Linux, Mac OS, iOS, Android, PlayStation, Xbox, and Raspberry Pi.

Note

You can get more information on Minecraft at https://minecraft.net/.

The version of this game for the Raspberry Pi is known as Minecraft Pi, and it was released in 2013. It was developed by Aron Nieminen and Daniel Frisk. Minecraft Pi focuses on the creative and building aspect of the game and does not include gathering resources and combat.

Playing Minecraft Pi

Minecraft Pi comes preinstalled in the latest version of Raspbian. So, there is no need for an additional installation. Minecraft Pi can be found by navigating to Menu | Games. Alternatively, we can start it by typing minecraft-pi in lxterminal. The following screen will appear once we start the game:

Playing Minecraft Pi

Click on the Start Game button. Then, the following window will appear:

Playing Minecraft Pi

Click on Create New and it will start generating a new world for the gameplay:

Playing Minecraft Pi

Once the new world for the gameplay is generated, the player character is placed in the virtual world. The default view in the game is the first-person view. It will look as follows:

Playing Minecraft Pi

Note

In Minecraft Pi, a new world is randomly generated in a procedural manner. This means that the world is randomly created with the algorithm rather than using predetermined components. So, no two worlds in Minecraft Pi will be the same. You can learn more about procedural generation from https://en.wikipedia.org/wiki/Procedural_generation.

We can switch from the first-person view to the third-person view by pressing the Esc key or by clicking on the following button for a view change:

Playing Minecraft Pi

Once this button is clicked on, it will change to the following button:

Playing Minecraft Pi

At this point, once we return to the game by pressing the Esc key or clicking on the Back to game button, we will see our Minecraft Pi character in the third person, as follows:

Playing Minecraft Pi

We can change this view to the first person view again by following the preceding steps.

Movement control in Minecraft Pi

We can use a mouse to look around and also make use of the following keys for the movement:

Key

Action

W

Forward

S

Backward

A

Left

D

Right

Spacebar

Jump

Double spacebar

Fly or fall

Like most first/third-person games, movement is controlled by WSAD keys on the keyboard. The character jumps if spacebar is pressed once. If we hit spacebar twice, the character is lifted in the air, as shown in the following screenshot:

Movement control in Minecraft Pi

At this point, WSAD keys can be used to fly in the air. We can increase the altitude by pressing spacebar while flying. The following screenshot shows the game world view from a higher altitude:

Movement control in Minecraft Pi

If spacebar is pressed twice while flying, then the character will fall on the ground. While the character is falling, if we press spacebar twice, it will stop falling.

Action control in Minecraft Pi

By default, the player character starts with a sword in hand. If we right-click with the sword in hand, it will remove the block in front of the character.

There is a quick draw panel at the bottom of the screen, as follows:

Action control in Minecraft Pi

Any item can be selected by scrolling the mouse wheel or using the numbers on the keyboard. The quick draw panel always hold eight items, and the current item in the hand is highlighted as follows:

Action control in Minecraft Pi

More items can be accessed by pressing the E key. This will open the inventory, as follows:

Action control in Minecraft Pi

Items in the inventory can be navigated by WSAD keys. While the inventory is open, if the Esc key is pressed, it will cancel the inventory without choosing an item. Items in the inventory can be chosen by clicking on the item, and the item will be added to the quick draw panel as well as the character's hand, as follows:

Action control in Minecraft Pi

With a block in the character's hand, right-clicking will place the block in front of the character and left-clicking will remove it. The number of blocks available to the player character is infinite.

Minecraft Pi features a creative mode, so the character does not take environmental damage (for example, when the character falls, it does not die) and is not affected by hunger. This game mode helps players focus on building and creating large projects.

Unlike other editions of Minecraft, Minecraft Pi does not feature any other gameplay modes.

Other controls in Minecraft Pi

When a game is running, pressing the Tab key will take the focus away from the game, and the mouse cursor will be freed to enable interaction with other windows on the desktop. Pressing Esc will pause the game and will take us to the game menu, where we can toggle views (as seen earlier), enable/disable sound, and quit the main window.

Note

Now, with all this information on the movements and the actions in the Minecraft Pi virtual game world, try to create a few things before continuing with the remainder of this chapter.

Movement control in Minecraft Pi

We can use a mouse to look around and also make use of the following keys for the movement:

Key

Action

W

Forward

S

Backward

A

Left

D

Right

Spacebar

Jump

Double spacebar

Fly or fall

Like most first/third-person games, movement is controlled by WSAD keys on the keyboard. The character jumps if spacebar is pressed once. If we hit spacebar twice, the character is lifted in the air, as shown in the following screenshot:

Movement control in Minecraft Pi

At this point, WSAD keys can be used to fly in the air. We can increase the altitude by pressing spacebar while flying. The following screenshot shows the game world view from a higher altitude:

Movement control in Minecraft Pi

If spacebar is pressed twice while flying, then the character will fall on the ground. While the character is falling, if we press spacebar twice, it will stop falling.

Action control in Minecraft Pi

By default, the player character starts with a sword in hand. If we right-click with the sword in hand, it will remove the block in front of the character.

There is a quick draw panel at the bottom of the screen, as follows:

Action control in Minecraft Pi

Any item can be selected by scrolling the mouse wheel or using the numbers on the keyboard. The quick draw panel always hold eight items, and the current item in the hand is highlighted as follows:

Action control in Minecraft Pi

More items can be accessed by pressing the E key. This will open the inventory, as follows:

Action control in Minecraft Pi

Items in the inventory can be navigated by WSAD keys. While the inventory is open, if the Esc key is pressed, it will cancel the inventory without choosing an item. Items in the inventory can be chosen by clicking on the item, and the item will be added to the quick draw panel as well as the character's hand, as follows:

Action control in Minecraft Pi

With a block in the character's hand, right-clicking will place the block in front of the character and left-clicking will remove it. The number of blocks available to the player character is infinite.

Minecraft Pi features a creative mode, so the character does not take environmental damage (for example, when the character falls, it does not die) and is not affected by hunger. This game mode helps players focus on building and creating large projects.

Unlike other editions of Minecraft, Minecraft Pi does not feature any other gameplay modes.

Other controls in Minecraft Pi

When a game is running, pressing the Tab key will take the focus away from the game, and the mouse cursor will be freed to enable interaction with other windows on the desktop. Pressing Esc will pause the game and will take us to the game menu, where we can toggle views (as seen earlier), enable/disable sound, and quit the main window.

Note

Now, with all this information on the movements and the actions in the Minecraft Pi virtual game world, try to create a few things before continuing with the remainder of this chapter.

Action control in Minecraft Pi

By default, the player character starts with a sword in hand. If we right-click with the sword in hand, it will remove the block in front of the character.

There is a quick draw panel at the bottom of the screen, as follows:

Action control in Minecraft Pi

Any item can be selected by scrolling the mouse wheel or using the numbers on the keyboard. The quick draw panel always hold eight items, and the current item in the hand is highlighted as follows:

Action control in Minecraft Pi

More items can be accessed by pressing the E key. This will open the inventory, as follows:

Action control in Minecraft Pi

Items in the inventory can be navigated by WSAD keys. While the inventory is open, if the Esc key is pressed, it will cancel the inventory without choosing an item. Items in the inventory can be chosen by clicking on the item, and the item will be added to the quick draw panel as well as the character's hand, as follows:

Action control in Minecraft Pi

With a block in the character's hand, right-clicking will place the block in front of the character and left-clicking will remove it. The number of blocks available to the player character is infinite.

Minecraft Pi features a creative mode, so the character does not take environmental damage (for example, when the character falls, it does not die) and is not affected by hunger. This game mode helps players focus on building and creating large projects.

Unlike other editions of Minecraft, Minecraft Pi does not feature any other gameplay modes.

Other controls in Minecraft Pi

When a game is running, pressing the Tab key will take the focus away from the game, and the mouse cursor will be freed to enable interaction with other windows on the desktop. Pressing Esc will pause the game and will take us to the game menu, where we can toggle views (as seen earlier), enable/disable sound, and quit the main window.

Note

Now, with all this information on the movements and the actions in the Minecraft Pi virtual game world, try to create a few things before continuing with the remainder of this chapter.

Other controls in Minecraft Pi

When a game is running, pressing the Tab key will take the focus away from the game, and the mouse cursor will be freed to enable interaction with other windows on the desktop. Pressing Esc will pause the game and will take us to the game menu, where we can toggle views (as seen earlier), enable/disable sound, and quit the main window.

Note

Now, with all this information on the movements and the actions in the Minecraft Pi virtual game world, try to create a few things before continuing with the remainder of this chapter.

Python programming for Minecraft Pi

Minecraft Pi comes with the Python programming interface. This means that it's possible to script the action of a character in the game using Python. In this section, we will learn how to use Python programming to script the character actions as well as create amazing effects in the Minecraft world.

Start Minecraft Pi and create the world. Once it's done, while the game is running, free the mouse by pressing the Tab key and opening lxterminal. Create the /home/pi/book/chapter02 directory. Navigate to the directory and write the following code in the prog1.py file:

import mcpi.minecraft as minecraft

mc = minecraft.Minecraft.create()

mc.postToChat("Hello Minecraft World!")

The preceding program first imports the Minecraft Pi Python API. The second statement creates the connection to Minecraft, and the third statement posts the message to the game chat.

Run the preceding code; the following is its output:

Python programming for Minecraft Pi

Let's take a look at some of the most important functions of the Minecraft Pi Python API.

We can see the player's current coordinates in the game window in the top-left corner of the window. We can retrieve these coordinates with getPos(). The following is the code to retrieve the player character's current coordinates and print them:

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
cur_pos = mc.player.getPos()
print cur_pos.x ; print cur_pos.y ; print cur_pos.z

This will print the current coordinates to the screen. Alternatively, the following syntax can be used to achieve this:

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
cur_x , cur_y , cur_z = mc.player.getPos()
print cur_x ; print cur_y ; print cur_z

The setPos() function can be used to set the character's position. We need to pass the desired coordinates of the character to this function, as follows:

import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
cur_x , cur_y , cur_z = mc.player.getPos()
mc.player.setPos(cur_x+10, cur_y, cur_z)

The preceding code will displace the character by 10 blocks in the x axis.

The following code will set the character position 100 blocks higher than the current position, and as a result, the character will start falling:

mc.player.setPos(cur_x, cur_y + 100 , cur_z)

We can place the blocks of our choice with the setBlock() function. We need to pass the coordinates of the block (the first three arguments) and the type of the block (the fourth argument) we need to set to this function as follows:

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
cur_x , cur_y , cur_z = mc.player.getPos()
mc.setBlock(cur_x + 1 , cur_y , cur_z , block.ICE.id )

You will find an ice block placed in front of you. If you do not find the block, then try to look around; it will be just beside or behind you.

Some blocks (such as wool and wood) have additional properties. We can pass this additional property as the fifth argument to the function for the wool and wood block types. The following code creates a column of a wool block with all the possible colors:

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
cur_x , cur_y , cur_z = mc.player.getPos()

for i in range (0 , 15):
  mc.setBlock(cur_x + 1 , cur_y + i , cur_z , block.WOOL.id, i )

Run the preceding code and check the output for yourself.

We can use setBlocks() to place multiple blocks for a given volume. The following example places multiple gold blocks:

import mcpi.minecraft as minecraft
import mcpi.block as block
mc = minecraft.Minecraft.create()
cur_x , cur_y , cur_z = mc.player.getPos()

mc.setBlocks(cur_x + 1 , cur_y + 1 , cur_z + 1 , cur_x + 6 , cur_y + 6 , cur_z  + 6 , block.GOLD_BLOCK.id )

We can also use mathematical equations to draw geometric shapes in the game world. Now we know that setBlock() is used to place a single block. We can use this function in a single for loop to create a line of blocks. Calling this function in a double for loop will set a two-dimensional geometric shape. We can further extend this by introducing one more for loop. This will create a three-dimensional shape. The following code places a golden sphere near the player's position. We will place the gold block in the positions where the coordinates satisfy the equation of the sphere with a radius of 10 blocks:

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

mc = minecraft.Minecraft.create()

r = 10

cur_x , cur_y , cur_z = mc.player.getPos()

for x in range(r*-1,r):
  for y in range(r*-1, r):
    for z in range(r*-1,r):
      if x**2 + y**2 + z**2 < r**2:
        mc.setBlock(cur_x + x, cur_y + ( y + 20 ) , cur_z - ( z + 20 ) , block.GOLD_BLOCK)

The following is the output of the preceding program:

Python programming for Minecraft Pi

The following code places a gold block below the player's current position until the execution of the code is terminated by pressing Ctrl + C:

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

mc = minecraft.Minecraft.create()

while 1:
  cur_x, cur_y , cur_z = mc.player.getPos()
  mc.setBlock(cur_x,cur_y-1,cur_z,block.GOLD_BLOCK.id)
  time.sleep(0.1)

The following is the output of the preceding code:

Python programming for Minecraft Pi

Now, we will conclude the chapter with some explosions. We can place a TNT block near us. It can be activated by left-clicking while holding the sword in hand. It will explode in a few seconds. The explosion will destroy some blocks in the blast radius:

cur_x, cur_y , cur_z = mc.player.getPos()
mc.setBlocks(cur_x+1,cur_y,cur_z, block.TNT.id,1)
This can be made more spectacular by placing multiple TNT blocks, as follows:
import mcpi.minecraft as minecraft
import mcpi.block as block

mc = minecraft.Minecraft.create()

cur_x, cur_y , cur_z = mc.player.getPos()
mc.setBlocks(cur_x+1,cur_y,cur_z,cur_x+4,cur_y+3,cur_z+3,block.TNT.id,1)

Once blocks are placed, activate the TNT block by the sword. The following is the screenshot of an explosion after activating the TNT blocks placed by the preceding code:

Python programming for Minecraft Pi

Note

The detailed Minecraft Pi Python API can be found at http://www.stuffaboutcode.com/p/minecraft-api-reference.html.

Summary

In this chapter, we got ourselves familiarized with the Minecraft Pi gameplay and learned how to program it using the Python API. We learned how to place and remove a block. Also, we implemented the code required to create some wonderful geometric shapes. In the end, we learned how to activate and explode TNT blocks to blow the large areas in the in-game world.

In the next chapter, we will learn how to use the Pygame library in order to create a game for the Pi.

You have been reading a chapter from
Raspberry Pi: Amazing Projects from Scratch
Published in: Sep 2016
Publisher:
ISBN-13: 9781787128491
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image