You are on page 1of 25

Minecraft Pi using Python

Student Names :
Nikhil Deo
Neel Paigude
Devang Datri
Introduction

 Raspberry Pi OS (Desktop version) comes with Minecraft installed. And it’s not
(only) to play.
The main goal for the Raspberry Pi Foundation is to help young people learn how
to compute and program. They installed Minecraft Pi to help kids achieve this
goal while having fun.
Tools

 Minecraft is a sandbox game where the entire world is generated with blocks of the same
size/
The player spawns in this environment and must survive while doing anything that he wants.
The game includes a mix/of exploration, building, crafting, and combat
Yes, you can also fight with passive or hostile mobs (like zombies or cows).
 Here is what it looks like when you start the game on a Raspberry Pi:
Python Language

 Python is a programming language often used on Raspberry Pi, but also in a lot of systems (Google
frequently uses Python in its projects).
Python offers a simple syntax and allows us to add libraries (like the Minecraft library we’ll use later).
 Python and Raspberry Pi stories are nested. It’s normal to try your project first in Python on a Raspberry Pi.
It’s one of the few ways to code things in Minecraft Pi
Thony Language

 As I said in the introduction, Raspberry Pi OS offers several Python editors already included on a fresh
install.
For this tutorial, we’ll use “Thonny”.
If you prefer another one, you can do the same thing.
But I like keeping each exercise in a single file, saved on my disk, rather than typing each line into the
Python Shell and losing it later.
Here is what it looks like ….
Creation of new world in Pi

 The first thing to do is to start the game and create a new world

Start the game (Start Menu > Games > Minecraft Pi).
The window is a little buggy. It’s normal if you can see the console behind.
• Try to put the game on a side of your screen, you’ll need space for the Python editor later
To move the window, click on the console blue bar with the small cursor (yes you have two …)
• Then click on “Start Game”
• You’re now in the “Select world” menu. Click on “Create New” to create your world.
Wait a few seconds for the world to generate
 You can now control your player using the mouse to see the world around you.
Minecraft logics

 Minecraft exists in several game modes:


• Survival: you need to gather blocks and resources, craft stuff, and survive during the night
• Creative: you get all the blocks you want for free and can’t die
• Adventure: for map creators, you can’t break blocks but you can use levers and buttons
• Spectator: no interaction, you are always flying and can go through blocks
 On Minecraft Pi, as it’s mostly an educative game, you are in the creative mode.
You already get a sword and some blocks in the quick bar at the bottom of the screen.
It’s possible to get more blocks , it’s always sunny, and you can’t die.
 In creative mode, it’s possible to break blocks in one shot. In survival mode, it depends on the tools you use
How to control our player

 Here are all of the controls you need to know:


• Camera: Move the mouse
• Break block: Left-mouse
• Place block: Right-mouse
• Moving: W,S,A,D (Z,S,Q,D on an AZERTY keyboard)
• Jumping: Space
Auto jump is enabled when moving
Double-space enables the fly mode and then use flying to gain altitude
• Access inventory: E
• Pause/Quit: ESC
Coding part

 Firstly ,
 Keep Minecraft open on one side and start Thonny.
Open the App Menu > Programming > Thonny Python IDE.
Then keep the new window open on the other side.
 Under the menu, the big space is for your code (it’s like a file editor)
And under it, you can see what happens when you run or debug the code using Python Shell.
Interact with Minecraft from Python script

 In each new language, the first thing to learn is how to code the renowned “Hello World!”
 To start, type this code in Thonny:

•Click on “Save” in the top menu


Save it where you want it (ex: /home/pi/minecraft1.py)
•Go back to Minecraft to see the game
Then type “TAB” to free the mouse pointer
•In the Thonny top menu, click on “Run”
Check what happens in the Minecraft window:
Minecraft API explanations

 First line

In this line, we include the Minecraft library for Python. So now, Python knows how to speak with
Minecraft.
You must add this line to every code when usi

n
Second line

In this one, we initialize the mc variable.


This variable is the representation of the library in our code.
We’ll use this to do thin

Third line

And the last one is to send a message to Minecraft.


We use the mc variable defined previously and call the function postToChat from the Minecraft
library.
This function allows us to send the message between brackets.
Change a Minecraft block with Python

 Minecraft coordinates
 Minecraft uses coordinates to know each player’s position, and each block has a different position.
The player’s position is visible in the top left of the Minecraft window:

X: 4.6 – It’s my east/west position


• Y: 0.0 – It’s my altitude
• Z: -0.7 – It’s my north/south position
Minecraft blocs id’s

Basically, each block in the game has an ID. Stone is 1, Grass is 2, Dirt is 3, etc …
Change the block near the player

 It’s almost the same code as before.


The only difference is that we use the setBlock function.
Parameters are: x, y, z (position), and the block id.
I choose the diamond ore block (ID 56) and the 0/0/0 position (near my player position).
But you can choose what you want, try to set a position close to the player.
Building of a house

 in this part we want to build a house around the player by placing no blocks ourselves.
You already know how to get the player positions and how to place blocks near him.
So, it’s pretty easy!

 Find a flat place and run the code!


Yes, I cheated
You can use setBlock 25 times if you want.
But setBlocks (with an “s”) allow us to define a zone to directly fill with one block type, like this:
Entire House Code
Here’s the small house built using previous script
Block iteration

 The last thing I want to show you are the block interactions.
There is a way to detect player interaction through the use of block and trigger actions.
 This code is difficult to guess for a beginner, so I’ll give it directly and explain each line afterward.
Here are some explanations

• This code turns each block you touch in the game (right-click) into diamond ore.
• pollBlockHits is the main function we use in this code.
You already know the other Minecraft functions.
• As we don’t know when the player will touch a block, we need to create an infinite loop: while True.
The try/except thing is mandatory when you create an infinite loop, to allow an exit (here it’s CTRL+C).
• We add a timer in this loop to not overload the Raspberry Pi: sleep(1)
• At the beginning of the loop, we call pollBlockHits to get all the blocks hit by the player.
• If there is a result, we create another loop (for) to browse all results (blockEvent).
• Finally, for each event, we post a message in Minecraft (postToChat)
And change the block (setBlock).

You might also like