Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize my World Game by Python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces Python how to achieve my world game, the article introduces in great detail, has a certain reference value, interested friends must read it!

1. Prepare for

Before you start, make sure that Python and pip have been successfully installed on your computer. Oh, if you use Python for data analysis, you can install Anaconda directly.

Open Cmd (start-run-CMD) in Windows environment, and open Terminal (command+ space input Terminal) in Apple system environment, ready to start entering commands to install dependencies.

Of course, I recommend that you use the VSCode editor to Copy the code of this article and run the command to install the dependency module in the terminal below the editor. How comfortable it is.

Enter the following command at the terminal to install the dependent module we need:

Pip install pyglet

If you see Successfully installed xxx, the installation is successful.

Git clone https://github.com/fogleman/Minecraft.git

Download locally:

Http://xiazai.jb51.net/202111/yuanma/Minecraft_jb51.rar

two。 Operation and operation

Running this project is very easy, you just need to go to the source code folder and enter the following command:

Python main.py

You can successfully run the game project, and then experience the game:

move

W: forward

S: back up

A: to the left

D: to the right

Mouse movement: perspective

Spaces: jumping

Tab: switch to flight mode

Architecture

Select the construction type:

1: bricks

2: grass

3: sand dunes

Left mouse button: remove buildings

Right mouse button: add buildings

Quit

ESC: closing window

Let's take a look at my real painting:

The word "real" is really difficult to draw, and I omitted the word Baodian because there is not enough space on the right side of the place I chose.

3. Code interpretation and customization

Next, let's take a look at the code of this game. The whole game code is only 902 lines, which is really excellent:

The default window size can be set at the position of the red box in the image above. Not only that, the author also gives some parameters for customizing speed, gravity, jump height, etc.

# frames per second TICKS_PER_SEC = 6 "Brick size SECTOR_SIZE =" Walking speed and flying speed WALKING_SPEED = 5FLYING_SPEED = "Gravity and jumping height GRAVITY = 20.0MAX_JUMP_HEIGHT = 1.0

Can we customize the brick type? There is a texture picture under the source folder:

In the source code, there are only three lines of code involving 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

In other words, it is entirely possible for us to add our own blocks, so what does this tex_coords ((1,1), (1,1), (1,1)) mean? Take a look at these two functions:

You will probably know the parameters of the tex_coords function, where the first parameter represents the top image of the brick, the second parameter represents the bottom image, and the third parameter represents the four-sided image. Among the parameters passed, (1) is represented as the (1) graph of texture.png:

For example, sand, the top edge, the bottom edge, and the four sides are all the same (1) this image, so it is:

SAND = tex_coords ((1,1), (1,1), (1,1))

The above is all the content of the article "how to realize my World Game with Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report