How to Build a Train in Minecraft Using Python – A Tutorial for Kids

Do you like playing Minecraft and want to learn how to create mods? It's easy and possible with the Python language. We suggest figuring out how to build in the Minecraft universe using the example of creating a train and programming it in Python.

Minecraft is a game popular among children and teenagers. They spend time in a virtual universe, communicate, and under certain conditions get the opportunity to create buildings. And although the game was originally written in Java and then rewritten using C++, Python is perfect for beginners and experienced modders. Its syntax only seems complicated: even primary school children and teenagers from 9 to 13 years old and older can successfully cope with Python programming.

Today we want to prove it and offer to create a small mod for a train in the game “Minecraft”. The lesson was prepared by the programming school for children Pixel. Let's get down to the main part.

How to learn to build in the game “Minecraft”: a detailed guide with code using the example of creating a virtual train

Let's make an integral element of the railroad simulator – a train. The main steps are presented below in the form of a text instruction. If you can't figure it out, watch the video about creating a train in the Minecraft universe, which is presented at the end of today's tutorial.

1. Getting started: download the software and import the required modules

If you are a beginner, be sure to watch one of the videos on installing Minecraft and Python on PC operating systems:

This is a conditional zero step, that is, preparation for working with the tools necessary to create today's mod. The Pixel school teacher told how to download, install and run the required software environments.

If everything is ready, let's get started.

The first step is to import the minecraft and block modules from the mcpi library. They will be needed to interact with the game and use the basic blocks for construction in the Minecraft game: with their help we will create buildings.

This is the code we need:

Getting Started with the Script

Getting Started with the Script

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

2. Create a connection to the virtual universe

In order to create our own conditional world with a train, we will use the create() function. Thanks to this, we will be able to access Minecraft and perform some operations on its server. We declare the action using the code:

Minecraft Train Code

Minecraft Train Code

# Подключение к Minecraft
mc = minecraft.Minecraft.create()

3. Manage the player's coordinates

For this we need the function getTilePos(). It is needed to determine the position of the train relative to the player. We write:

# Координаты игрока
player_pos = mc.player.getTilePos()

4. Set the coordinates of the first wagon, set its dimensions and add additional wagons

You need to take the x coordinate of the player, add 2. This will allow you to build wagons to the right of the character. Do not touch the y and z coordinates: otherwise, the position of the train relative to the player will be disrupted. We write:

# Координаты первого вагона
wagon_x = player_pos.x + 2
wagon_y = player_pos.y
wagon_z = player_pos.z

To get one step closer to understanding how to build a train in Minecraft, let's set its dimensions. Here's the code you'll need:

# Размеры вагона
wagon_width = 4
wagon_length = 4
wagon_height = 2

Now you need to run the cycle and repeat it 4 times to create the same number of wagons. Here is the code you need:

# Создание 4 вагонов
for_in range(4):

5. We make the body, wheels and other parts

We have almost figured out how to make a train in the game “Minecraft” using the modules block and minecraft, the create() function and additional tools. The only thing left is to work on the final details. These are:

  • Frame;

  • Wheels;

  • Fastenings.

Here is the code for the case:

mc.setBlocks(wagon_x, wagon_y, wagon_z, wagon_x + wagon_width - 1, wagon_y + wagon_height, wagon_z + wagon_length - 1, block.STONE.id)

Now we will use the setBlock() function to make wheels. Place the iron blocks (block.IRON_BLOCK.id) in the required coordinates. We will take into account that each wagon has 4 wheels: 2 in front on the left and right, 2 in the back on the same sides. Here is the code that will help program this condition:

# Создание колес
mc.setBlock(wagon_x, wagon_y, wagon_z - 1, block.IRON_BLOCK.id) # Переднее колесо слева
mc.setBlock(wagon_x + wagon_width - 1, wagon_y, wagon_z - 1, block.IRON_BLOCK.id) # Переднее колесо справа
mc.setBlock(wagon_x, wagon_y, wagon_z + wagon_length, block.IRON_BLOCK.id) # Заднее колесо слева
mc.setBlock(wagon_x + wagon_width - 1, wagon_y, wagon_z + wagon_length, block.IRON_BLOCK.id) # Заднее колесо справа

Now let's check if the current car is the first one. If not, that is, the position is not the initial one, we can start creating fasteners. Let's take the block.RAIL.id block. Code:

# Создание крепления между вагонами
if wagon_x != player_pos.x + 2:
	mc.setBlock(wagon_x - 1, wagon_y, wagon_z + 2, block.RAIL.id)

The final touch is to update the x coordinate for the next wagon so that it adds the width of the current wagon and the extra block. Code:

# Обновление координат для следующего вагона
wagon_x += wagon_width + 1

What the final script should look like is shown on the screenshot:

So, we managed to make a train in the game “Minecraft” without developing a complex mod. Try running the program:

  1. Click on Run.

  2. Select Run Module.

  3. Go to Minecraft and click the return to game button.

If everything worked out, congratulations!

Bonus: Tutorial Video and Full Code

If something doesn't work, be sure to check it out training video. In it, a teacher from the Pixel school explained how to build a train for the game Minecraft and clearly showed parts of the code.

In conclusion, we will present the code in full: it will be useful for self-testing or in case the video does not help.

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

# Подключение к Minecraft
mc = minecraft.Minecraft.create()

# Координаты игрока
player_pos = mc.player.getTilePos()

# Координаты первого вагона
wagon_x = player_pos.x + 2
wagon_y = player_pos.y
wagon_z = player_pos.z

# Размеры вагона
wagon_width = 4
wagon_length = 4
wagon_height = 2

# Создание 4 вагонов
for _ in range(4):

	# Создание корпуса вагона
	mc.setBlocks(wagon_x, wagon_y, wagon_z, wagon_x + wagon_width - 1, wagon_y + wagon_height, wagon_z + wagon_length - 1, block.STONE.id)

	# Создание колес
	mc.setBlock(wagon_x, wagon_y, wagon_z - 1, block.IRON_BLOCK.id) # Переднее колесо слева
	mc.setBlock(wagon_x + wagon_width - 1, wagon_y, wagon_z - 1, block.IRON_BLOCK.id) # Переднее колесо справа
	mc.setBlock(wagon_x, wagon_y, wagon_z + wagon_length, block.IRON_BLOCK.id) # Заднее колесо слева
	mc.setBlock(wagon_x + wagon_width - 1, wagon_y, wagon_z + wagon_length, block.IRON_BLOCK.id) # Заднее колесо справа
 
	# Создание крепления между вагонами
	if wagon_x != player_pos.x + 2:
  	mc.setBlock(wagon_x - 1, wagon_y, wagon_z + 2, block.RAIL.id)

	# Обновление координат для следующего вагона
	wagon_x += wagon_width + 1

And to learn how to control a train in the game “Minecraft”, build beautiful stations, create rails and virtual surrounding objects – houses, trees, etc. – check out selection of videos. They will help you hone your skills in writing Python code and bring them to at least a good level.

We also ask you to share your successes and tell us how it all went. Did you manage to make a train? Did you find the instructions difficult?

We would like to ask parents of children to express their opinion about the Python language: is it suitable for teaching programming to younger students and teenagers?


The lesson was prepared by the children's programming school Pixel. We teach programming in Python to children aged 9-10 and older, check out our online courses.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *