How to build a train for Minecraft using Python – a lesson for children

How to build a train for Minecraft using Python – a lesson for children

Love to play Minecraft and want to learn how to make mods? It’s simple and possible with Python. We suggest you figure 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 the 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 elementary school students and teenagers aged 9 to 13 and older can successfully cope with Python programming.

Today we want to prove it and offer to create a small train mod in the Minecraft game. 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 for the example of creating a virtual train

Let’s make an integral element of the railway simulator – a train. The main steps are presented in the form of text instructions. 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 necessary modules

If you’re new, be sure to watch one of the videos on installing Minecraft and Python on PCs with 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 explained how to download, install and run the necessary software environments.

If everything is ready, let’s start.

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 building blocks in the Minecraft game: with their help, we will create buildings.

This is the code we need:

Start of work on the script

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

2. We create a connection to the virtual universe

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

Minecraft train code

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

3. Control the coordinates of the player

For this we need the getTilePos() function. It is necessary 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 car, set its dimensions and add additional cars

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. We do not touch the y and z coordinates: in another situation, the location of the train in relation to the player will be disturbed. We write:

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

To get a step closer to understanding how to build a train in the game “Minecraft”, let’s put its dimensions. Here’s the code you’ll need:

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

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

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

5. We make the body, wheels and other details

We have almost figured out how to make a train in the game “Minecraft” using the block and minecraft modules, the create() function and additional tools. The matter remains to be seen: the final details must be worked out. It:

  • Corps;

  • Wheels;

  • Fastening.

Here is the code for the body:

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 use the setBlock() function to make the wheels. Place the iron blocks (block.IRON_BLOCK.id) in the required coordinates. Let’s take into account that each car has 4 wheels: 2 in front on the left and right, 2 in the back on the same sides. Here is some code to 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 whether the current car is not the first. If not, that is, the position is not initial, you can start creating fasteners. Let’s take the block block.RAIL.id. Code:

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

The finishing touch is to update the x-coordinates for the next car so that the width of the current car and the additional block are added to it. Code:

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

How the final script should look 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 didn’t work out, be sure to watch it educational video. In it, the Pixel school teacher told how to build a train for the game “Minecraft” and visually showed parts of the code.

In conclusion, we will present the code in its entirety: 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 a selection of videos. They will help sharpen the skills of writing code in Python and bring them to at least a good level.

We also ask you to share your successes and tell us how everything went. Did you manage to make the train? Did the instructions seem difficult?

And we would like to ask parents of children to express their opinion about the Python language: is it suitable for teaching junior high school students and teenagers programming?


The lesson was prepared by the Pixel programming school for children. We teach Python programming to children from 9-10 years old and older, check out our online courses.

Related posts