Learn How to Program in Python with Minecraft


Who doesn’t like to use cheat codes every now and then when playing video games? They certainly make gaming simpler and more fun. In this tutorial, we are going to show you how to implement some cheat codes for Minecraft using Python, one of many programming languages.


If you do not already have these parts, then you can simply get this kit that includes all of the cables to get started with the Raspberry Pi:


Raspberry Pi Setup

The first thing that you need to do is install the operating system for the Pi. If your Pi already has an OS, then you can skip this section. Otherwise, follow the instructions under Operating System for the Raspberry Pi from this tutorial:  


New Game

Once the Pi is ready to be used go to Menu > Games > Minecraft Pi > Start Game > Create New to generate a new world.


Use the mouse to look around, and the following keys to move around and do stuff:



Okay so now we are ready to start programming. First go to Menu > Programming > Python 3 (IDLE) to open a Python Shell.


Hello World

For testing purposes, let’s make “Hello World” appear on our screen by typing the following code in the Python Shell:

from mcpi.minecraft import Minecraft mc=Minecraft.create() mc.postToChat(“Hello World”)

You should get something like this:


The first line imports the Minecraft library that contains the tools needed to interact with the game. The second line creates a connection between the Python shell and the game. The third line makes the text appear on our screen. You can change the text inside the parenthesis to whatever you want to display.  


Now that we know that it works, let’s teleport. In order to be able to teleport, we first need to know our location. When we look at the top left corner of our screen we can see three numbers labeled “pos.” Yes, this is our location in the form of x, y, z, where x and z are walking directions and y refers to the elevation of our character from the ground. Even though we know our position, Python doesn't know it. In order to make Python know where we are located in the map we can use the following command in the Python shell:


This command will store each value of our location in the variables x, y, and z. Now if we type x in the shell, we will get the x value that appears on our game screen.


The same happens with y and z.


Once Python knows our location, we can use the following command to set our new location:


In this example, we move 10 units to the front in the x direction. We can change the number to whatever number of units we want to advance. This also work for the y and z coordinates.

Place Blocks

To place a block you can simply use the following command:


This places a new block one unit ahead of your current x location.  


We can see that besides the x, y, and z coordinates, we have a “1” in the argument of the function. In Python, each block is associated with a number. The number “1” is used to tell the program that we want to use stone. Go ahead and try different numbers to see what other blocks are created. Some blocks have special properties that allow you to modify the appearance of the block. For example, if we type the following command:


An orange wool block is placed one unit ahead of your x position.


In this case the number 35 is used to specify that we want a wool block, while the number 1 is used to specify the color of the block. Change the last parameter of the function to see what other colors are available. Some other special blocks are  wood (17), grass (31), and torch (50).

There are two alternative ways to select the block that we want to use. The first one is to assign the number that identifies a specific block to the actual  name of the block, and then use it in the function:

wool=35 mc.setBlock(x+1,y,z,wool,1)

The second method is to import the following library at the beginning of your code:

From mcpi import block

This library contains the name of all the blocks, so you can just use their names instead of numbers like in this example:


Note that the name is of the form:


We are really just replacing “NAME_OF_THE_BLOCK” with the block’s name that we want to use. If you don’t know the name of the blocks, you can refer to the following link:


You want to use the name that is after “minecraft:” exactly the way it is written but in caps.

Multiple Blocks

To set multiple blocks you can use the following command:

stone=1 x,y,z = mc.player.getPos() mc.setBlocks(x+1,y,z+1,x+11,y+10,z+11,1)

This creates a 10x10x10 stone cube.  


The function is telling the program to place a stone at location x+1 and keep filling with stones until it reaches location x+11. The same happens in the y and z directions.

Now let’s say that we want to drop blocks as we walk. In the python shell click on File > New Window  and type the following code:

from mcpi.minecraft import Minecraft from time import sleep mc=Minecraft.create() flower=38 while True: x,y,z =mc.player.getPos() mc.setBlock(x,y,z,flower) sleep (0.1)

Now click on Run  > Run Module. You will be asked to save the script before being able to run it. Go ahead and save it wherever you want. After you save the script, it will run in the python shell. This is just another way of executing code without having to use the shell’s command line. So now everytime we walk, we leave a flower behind us.


The second line of our code imports the library required to use delays, or pauses in the code such as sleep(0.1). Once the code reaches this function, it waits for 0.1s and then keeps executing.

The “while True:” is called a while loop. This loop executes whatever code is inside it repeatedly when the value next to “while” is veridic. In this case the value is “True,” therefore the code will keep executing forever. Everything that is indented after “while True:” is considered to be part of the while loop, so this is the code that will be executed repeatedly. First we get our current position, and then we place a flower in that position. Next we wait for 0.1s and repeat the process. As we walk, our position coordinates will be updated every 0.1s, and flowers will be placed in all the locations we have been. If you want to stop dropping flowers everywhere you go, then just hit Ctrl+c.

Now let’s drop blocks only when we are in a specific terrain such as grass. Follow the same procedure from the previous example to write the code in a new window and execute it in the shell:  

from mcpi.minecraft import Minecraft from time import sleep mc=Minecraft.create() grass=2 flower=38 while True: x,y,z = mc.player.getPos() block_beneath=mc.getBlock(x,y-1,z) if block_beneath==grass: mc.setBlock(x,y,z,flower) sleep(0.1)

First we identify each block with their respective number. Then we open a while loop that will execute forever. Inside the while loop we get our current position and the id number of the block that we are standing on. The id number gets stored in a variable called “block_beneath.” Notice that we are using y-1 since all the blocks that we stand on are under us so we need to subtract 1 from our current y position. Then we open an if statement. This is a function that executes a part of the code only if “block_beneath,” which is the block id we are standing on, is equal to the block id of grass. When we want to check if one thing is equal to another, such as in this case, we have to use double equal signs “==.” If the case is true, we use the function mc.setBlock(x, y, z, flower) to place a flower in the current location.

In other words, we are simply updating our location every 0.1s like in the previous example, and checking what blocks we are standing on. We are also constantly checking if the block we are standing on is grass. If it is not grass, then nothing happens. If it is grass, then a flower is placed in that location.


There you go. Now you can use your coding knowledge in Python to make playing Minecraft a little bit easier and faster.  

If you have any questions about this tutorial, then please do not hesitate to leave a comment, contact us here .

This article was published by the Jaycon team. Learn more about how we can take your product design and hardware idea to the next level here.