How to Create a Lucky Chest Game in Minecraft Using Python – Tutorial for Kids

We'll show you how to create a chest hunting game within the Minecraft universe. The Python programming language will help us with this. The lesson is intended for children aged 9-10 years and older.

We will write a script that will create chests in the world of Minecraft. The player's goal is to find the lucky one among many different chests in a limited amount of time.

Lesson prepared by a programming school for children Pixel. At the end of the material, we left a video where the code for the game is also discussed in detail, if this format is more comfortable for you.

Games with chests in Minecraft

Games with chests in Minecraft

Installing programs

First you need to download all the necessary applications. We have detailed guides for users of different operating systems:

If you find it difficult to understand the topic, look first step by step lessons on Minecraft and Python for beginners.

Creating a program for a chest in Minecraft

Let's start writing the program. First of all, we import the necessary libraries: the Minecraft library for communication with the game, the Block library from MC5 for creating blocks, as well as the Random library for random spawning of chests and Time for keeping track of time.

from mcpi.minecraft import Minecraft
from mcpi import block
import random
import time

Next we create the MC variable. It will keep the connection between our program and the world of Minecraft. We will also get the player’s coordinates, the Player X, Y and Z variables.

mc = Minecraft.create()
player_x, player_y, player_z = mc.player.getTilePos()

Let's define the number of chests in the MC variable, and then the time limit for completing the game, the TimeLimit variable. Let's designate the maximum spawn zone of chests in MaxRange and create an empty list in which the coordinates of our chests will be stored.

num_chests = 50
time_limit = 30
max_range = 30
chests = []

Using a For loop, we will generate random coordinates for the chests. We will generate X and Z using RunInt, a function that can produce a random integer. We will pass negative MaxRange parameters and positive MaxRange parameters. For example, so that the chest is formed from minus 10 to 10 blocks from us along the X and Z axes. To prevent our blocks from flying in the air, we will get Y using the GetHeight function. It determines the height of the block in the world. And add the coordinates to the Chats list, creating a chest.

for _ in range(num_chests):
   x = player_x + random.randint(-max_range, max_range)
   z = player_z + random.randint(-max_range, max_range)
   y = mc.getHeight(x, z)
   chests.append((x, y, z))
   mc.setBlock(x, y, z, block.CHEST.id)

Next, we will select a random lucky chest using the Random.Choice function. She will select one of the coordinates of our chests in the list.

lucky_chest = random.choice(chests)

Now we need to create a function that will check whether we clicked on the lucky chest and whether we met it within the time period. At the beginning of the function, we determine our start time using the time.time library. In the while loop we will keep track of the difference between the current time and the start time. And if the time elapsed is less than the limit, we will check the hits on the blocks. If the coordinates of the impact coincide with the coordinates of the lucky chest, then it turns out that we have found the lucky chest. And so let's return true. Otherwise, our time will run out and we will return false as a parameter that the game is over with a loss.

def chech_for_chest():
   start_time = time.time()
   while time.time() - start_time < time_limit:
       events = mc.events.pollBlockHits()
       for event in events:
           hit_x, hit_y, hit_z = event.pos
           if (hit_x, hit_y, hit_z) == lucky_chest:
               mc.postToChat("Вы нашли счастливый сундук!")
               return True
   mc.postToChat("Время вышло! Попробуйте снова.")
   return False

Now we can display a start message in the chat, run our function, writing its result to the gameWin variable. After our function runs, we'll use a for loop to remove all the chests so they won't remain in future games. And if, after all, our gameWin variable is true, then we will congratulate the player on his victory and spawn the diamond block. Otherwise our game will be over.

mc.postToChat(f"Найдите счастливый сундук за {time_limit} секунд!")
game_win = chech_for_chest()
for chest in chests:
   mc.setBlock(chest[0], chest[1], chest[2], block.AIR.id)
if game_win:
   mc.postToChat("Поздравляем! Заберите приз!!!")
   mc.setBlock(player_x, player_y, player_z, block.DIAMOND_BLOCK.id)
else:
   mc.postToChat("Игра окончена.")

Game result

Let's test our program. To make the game interesting, you can choose your own values ​​for variables, such as the number of chests, the time in which we will have to complete this game, as well as the maximum spawn radius. Let's say we can make a very large area and spawn a lot of chests there. And we will need more time for this. Or, on the contrary, we will spawn only a few chests in a small area.

Video: how to make a chest in Minecraft

If you do not fully understand the text instructions, watch the video tutorial where the teacher breaks down the code for the game.

The lesson was prepared by Pixel children's IT school. We help children from 5 to 17 years old master IT skills. In our courses, students study programming, web design, game development, 3D modeling and computer animation. Children aged 9 to 13 years can learn program in Minecraft using the Python language, revealing your talents in the IT world.

Similar Posts

Leave a Reply

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