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 develop exquisite Tetris by Python

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

Share

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

Python how to develop exquisite Tetris, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Preface

Recently, I was looking for some examples of game development related to Python. I happened to wander around on itch.io and saw this Tetris project.

1. Introduction to the game

The following is to introduce the specific functions of this Tetris written in Python. It has seven game modes:

Single player mode

Ordinary two-person mode

Mirror double mode (that is, all falling squares are the same)

Double acceleration mode (each line deleted will speed up the other party)

Two-person exchange control mode (one time to control one's own box, one time to control the other party)

Double balance mode (will change each other's capacity)

Double single container mode

Yes, are you shocked by the rich game mode? And the interface design of the game is quite simple and comfortable (and you will never be annoyed by advertising when playing such an Python open source game):

Not only that, in addition to wasd and the up and down keys, it also supports the use of the game console (only one) to play the game, and can switch the button settings of two players:

The double mode is similar to the following, which is very suitable for spending time between young couples (if you all like to play Tetris).

Especially in the two-person collaboration model, two people solving problems together is also a good way to improve the relationship:

two。 Source code analysis

Of course, our primary goal is to learn the source code of this game, so here are some of the core code of this game. After downloading the open source project, you will find that its py files are distributed as follows:

│ base.py │ game_modes.py │ main.py │ run_to_release.py │ tetris.py │....... ├─ assets │...... │ └─ screens......

Among them, main.py is the entry point of the program, first look at the introduction of the module, the introduction of sys is of course, the game needs to be called at the end of the game.

Sys.exit ()

Causes the Python program to exit normally. The game engine uses pygame, an older Python-based 2D game engine, but because it has been around for a long time, it has become a very mature and easy-to-use 2D game development library.

The author wrote two modules, one is base, which has a series of basic game settings such as canvas configuration, button configuration, player configuration and so on. Game_modes, on the other hand, is the logical module of the seven game modes, which also refers to tetris.py, which defines the method of rotating the entire Tetris and its data structure.

However, due to time constraints, let's focus on the main loop main_loop in base:

Pygame.time.Clock () is used to configure the number of frames in the game, and Clock.tick (frameate) represents a framerate frame run per second, that is, no more than framerate cycles are performed per second. The value of this parameter, we can see in the main function, is 600. That is, this cycle will not exceed 600 times per second.

Pygame.event.get () is the event handler of the pygame game engine, which is used to handle all events, as if opening the door for all people to enter, and then we can do corresponding actions according to the different properties of the event, such as event.type = = pgl.KEYDOWN indicates that when the keyboard is pressed, execute the statements in the condition:

Pressed_keys.add (event.key)

It is added to the pressed_keys collection, and yield returns the generator, which means that as long as the program is not terminated, the function continues to return four values: events, time_passed, pressed_keys, or {(0,0)}. Events is the event triggered by the user, time_passed is the duration of these events, and pressed_keys is the key pressed by the user.

This completes the delivery of a user event, and then the back end handles the user event, which is displayed in the game. Of course, more complexity lies ahead, here is just a simple event handling, of course, is the most important part of event handling.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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