Hello world!
# Some commands for better formatting in the website, no need to look at this
from IPython.display import Image
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
Hello world example¶
To get started with Python and Minecraft, let's create a code which will print Hello world! to the chat window.
To connect and interact with Minecraft we will use the mcpi library, so make use the mcpi
folder you downloaded from RaspberryJam Mod's website is present in the same directory as this notebook. As an example, let's say you have the following folders:
- Desktop
- My Scripts
- mcpi
- Hello_world.ipynb <---- this notebook!
- My Scripts
Let's start coding!
1. Get Minecraft ready¶
It is a good idea to tell Minecraft not to go into Settings Menu when you switch to another window, for example this browser, using maybe alt+tab
. To do so, go to your Minecraft installation folder and edit the options.txt
file.
Find the pauseOnLostFocus
value and replace true
by false
. Now your Minecraft game won't go into Settings Menu when you are writing your code here, and you can see everything in real time!
Now open your Minecraft game with RaspberryJam and PythonTool mods loaded, and load your favourite world, both creative or survival modes work great!
2. Load the required libraries¶
We have to tell Python which libraries we're going to use, so that it can import them.
import mcpi.minecraft as minecraft
import mcpi.block as block
If executing the previous cell gives you errors, it means that the mcpi
folder is not in the same directory as this notebook, so Python cannot find it! Make sure you've placed it there.
3. Establish a connection to your loaded Minecraft world¶
Now that your world is up and running, and you have the mcpi
library loaded in Python, we can make a connection to your game!
mc = minecraft.Minecraft.create()
When you execute the previous cell, it should connect without any complains or output. If an error is thrown, check that your Minecraft game has RaspberryJam and PythonTool mods loaded, and that you have opened a world.
4. Post "Hello world!" to the chat window!¶
mc.postToChat("Hello world!")
You should see Hello world! in your Minecraft chat window. Congratulations, you've created your first script!
Exporting all this code as a Python script to use with PythonTool-Mod¶
The Jupyter Notebook is a great way to test and explore the possibilities Python offers to make Minecraft even cooler, but once you have your code ready and working, you'll want to put it together as a standard Python script so that you can use it in-game with PythonTool-Mod.
Luckily, this is very easy! Just click on File
, then Download as
and select Python (.py)
.
It will download your Python script to your Donwloads
folder, as with any other file downloaded form the Internet. Just move this file to your script folder and you will find it inside the Computer Block in Minecraft, ready to be executed with one click!