Minecraft Pi Using Python
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
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:
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
Third line
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:
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
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!
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).