In this article, I will show you how to create a Minecraft game in just under 40 lines of code using Python. Because the game is written in less than 40 lines of code, it will be very rudimentary and not fully featured, but it is still basically like Minecraft.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
How to create Minecraft game in less than 40 lines of code in Python
First, you need to download python to your computer by accessing here. If your computer already has python installed, you can skip this step.
Step 1: You open cmd and install ursina game engine with the following command: pip install ursina
Step 2: Create files with the following structure. I created a minecraft folder containing an assets folder and a minecraft.py file. In it the assets folder to store images of blocks and weapons in the game.
Step 3: Open the file minecraft.py and copy the following code.
from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController app = Ursina() Sky(texture="sky_sunset") player = FirstPersonController()#Tạo người chơi #Tạo đối tượng vũ khí sword = Entity(model="assets\\blade", texture="assets\sword", rotation=(30,-40), position=(0.6,-0.6), parent=camera.ui, scale=(0.2,0.15)) #Hàm có nhiệm vụ lặp lại liên tục nhằm kiểm tra event click chuột để thay đổi vị trí của vũ khí def update(): if held_keys['left mouse']: sword.position = (0.6,-0.5) elif held_keys['right mouse']: sword.position = (0.6,-0.5) else: sword.position = (0.7,-0.6) #Vòng lặp for và mảng boxes có nhiệm vụ tạo sàn 12x12 trong game boxes = [] for n in range(12): for k in range(12): box = Button(color=color.white, model="cube", position=(k,0,n), texture="assets\grass",parent=scene, origin_y=0.5) boxes.append(box) #Hàm input có nhiệm vụ kiểm tra event chuột để đặt và xóa khối def input(key): for box in boxes: if box.hovered: if key == 'left mouse down': new = Button(color=color.white, model="cube",position= box.position + mouse.normal, texture="assets\grass", parent=scene, origin_y=0.5) boxes.append(new) if key == 'right mouse down': boxes.remove(box) destroy(box) #Lệnh chạy game app.run()
Step 4: Open cmd and access the path containing the file minecraft.py and run the game with the command: python minecraft.py
So you have successfully played Minecraft game with just under 40 lines of code in python. In addition, you can also download an entire folder of photos, weapons, and code files here. If you don’t like making minecraft games then I think you will like how to create Keylogger in Python here.