You’ve probably heard of Minecraft, but have you ever thought about writing one yourself in Python? Too difficult, too complicated? Maybe, but how do you know if you can’t do it until you try?

A developer named Fogleman made Minecraft in Python, and with just 900 lines of code, it looked like a decent game to play:

Here, we’ll take you through the project and make a few simple changes to this little open source game to make it “your” world.

1. Prepare

Before you begin, make sure Python and PIP are successfully installed on your computer. If not, please visit this article: Super Detailed Python Installation Guide to install Python. If you are using Python for data analysis, you can install Anaconda directly: Python data analysis and mining helper – Anaconda

In Windows, open Cmd(Start – Run – Cmd). In Apple, open Terminal(command+ space enter Terminal).

Of course, I recommend that you use the VSCode editor, Copy this code, and run commands in the terminal below the editor to install dependency modules.

Enter the following command from the terminal to install the dependency modules we need:

pip install pyglet
Copy the code

If Successfully installed XXX is displayed, the installation is successful. Then you need to download the source code of the game, which you can download by entering the command:

git clone https://github.com/fogleman/Minecraft.git
Copy the code

It can also be downloaded from the Python utility public account: MC.

2. Operation and operation

Running the project is as simple as entering the source folder and typing the following command:

python main.py
Copy the code

To successfully run the game project, then play the game:

mobile

  • W: forward
  • S: backward
  • A: to the left
  • D: the right
  • Mouse movement: Perspective
  • Space: jump
  • Tab: Switch to airplane mode

building

  • Select build type:
    • 1: bricks
    • 2: grass
    • 3: sand dunes
  • Left mouse button: Remove building
  • Right mouse button: Add building

exit

  • ESC: Close the window

Take a look at my actual drawing:

The “real” character was really difficult to draw, and I left out the words “treasure book” because there was not enough space on the right side of the place I chose.

3. Code interpretation and customization

Let’s take a look at the game code, which is only 902 lines long.

The default window size can be set in the red box above. The authors also give some parameters for customizing speed, gravity, jump height, and so on:

# Frames per second
TICKS_PER_SEC = 60

# Brick size
SECTOR_SIZE = 16

# Walking speed vs. flight speed
WALKING_SPEED = 5
FLYING_SPEED = 15

# Gravity and jump height
GRAVITY = 20.0
MAX_JUMP_HEIGHT = 1.0
Copy the code

Can we customize the brick type? Note that there is a texture image in the source folder:

In the source code, there are only 3 lines of code that involve the user adding blocks, such as SAND:

SAND = tex_coords((1, 1), (1, 1), (1, 1))
#... .
t = random.choice([GRASS, SAND, BRICK])
#... .
self.inventory = [BRICK, GRASS, SAND]
# 1.brick, 2.grass, 3.sand
Copy the code

In other words, it is entirely possible for us to increase our own block, so what does this tex_coords((1, 1), (1, 1)) mean?

def tex_coord(x, y, n=4):
    """ Return the bounding vertices of the texture square. """M is 1.0 over n, dx is x times m, dy is y times mreturn dx, dy, dx + m, dy, dx + m, dy + m, dx, dy + m


def tex_coords(top, bottom, side):
    """ Return a list of the texture squares for the top, bottom and side. """
    top = tex_coord(*top)
    bottom = tex_coord(*bottom)
    side = tex_coord(*side)
    result = []
    result.extend(top)
    result.extend(bottom)
    result.extend(side * 4)
    return result


TEXTURE_PATH = 'texture.png'

GRASS = tex_coords((1, 0), (0, 1), (0, 0))
SAND = tex_coords((1, 1), (1, 1), (1, 1))
BRICK = tex_coords((2, 0), (2, 0), (2, 0))
STONE = tex_coords((2, 1), (2, 1), (2, 1))
Copy the code

You should probably know by looking at the parameters of the tex_coords function, where the first parameter represents the image of the top of the brick, the second parameter represents the image of the bottom, and the third parameter represents the image of the four edges. The (1,0) parameter is denoted as the (1,0) graph of texture.png:

For example, the top, bottom and four sides are the same (1,1), so it is:

SAND = tex_coords((1, 1), (1, 1), (1, 1))
Copy the code

After you understand this principle, it is not difficult to add a custom shape brick. You can try it.

So that’s the end of our article, if you’d like to see our Python tutorial today, please stay tuned and give us a thumbs up/check out if it was helpful. If you have any questions, please leave them in the comments below and we’ll be patient to answer them!


The Python Utility Guide is more than just a guide

Come and try minecraft, a game written in Python