“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
Review past
Python to achieve “little rabbit and Bun” game
Python eight notes to achieve small games
Python to achieve the puzzle game
Python to achieve ski games
Python for classic 90 tank wars
Python FlappyBird small game
Python dinosaur jump a small game
Python to achieve tower defense games
Python to achieve fruit and gold coins small games
Python simple version of the aircraft war games
Preface:
Work boring with PyQT5 to write a small game of Tetris, let us happily begin ~
The effect
The development tools
Python version: 3.6.4
Related modules:
Pyqt5 module;
And some modules that come with Python.
Environment set up
Install Python and add it to the environment variables. PIP installs the required modules.
Introduction of the principle
Rules of the game:
Different shaped pieces of small squares successively fall from the top of the screen, and players adjust the position and orientation of the pieces so that they form one or more complete lines at the bottom of the screen. These complete bars then disappear, making room for new slabs to fall, while the player is rewarded with points. Blocks that have not been eliminated keep piling up, and once they reach the top of the screen, the player loses and the game is over. (PS Baidu Encyclopedia)
Control mode:
←→ key to control the position, ↑ key to control the direction, the space bar to quickly drop the plate to the bottom, P key to pause the game.
Step by step:
Step1: define a tetris
First, let’s define a Tetris class that uses small squares to form different shaped blocks:
In order to obtain the location information of each small square in a Tetris, let’s define some intra-class methods to obtain the location information, as shown in the figure below:
Step2: make the main interface of the game
We defined the data recording and data processing in the game in the internal plate class, and defined the game data visualization in the external plate class. The specific implementation is as follows.
(1) Define an inner plate class:
We can move the currently controllable Tetris left/right/down (the downward movement is automatically operated by the system) and fall quickly in the inner plate:
Of course, when moving, we should judge whether the current controllable Tetris has “landed” in real time:
After “landing”, merge the currently controllable Tetris into the uncontrollable ones at the bottom and remove those rows with small squares throughout the row (i.e. complete bars) :
Then create a new Tetris:
And so on.
(2) Define an external plate class
Its function is to use the pyQT5 module to display the real-time data information of the internal plate in real time.
Step3: draw a side board
Let’s draw a side panel on the right side of the main screen to show the next tetris shape in real time:
Step4: implement the main loop of the game
Define the Tetris game class and initialize it:
The definitions of game start and game pause are as follows:
Key monitoring is defined as follows:
Then call to execute it and it’s OK:
That’s the end of this article, thank you for watching Python24 mini games series, the next article to share the push box mini games
To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.
Dry goods are mainly:
① More than 2000 Python ebooks (both mainstream and classic)
Python Standard Library (Chinese version)
(3) project source code (forty or fifty interesting and classic practice projects and source code)
④Python basic introduction, crawler, Web development, big data analysis videos (suitable for small white learning)
⑤Python Learning Roadmap
⑥ Two days of Python crawler boot camp live permissions
See profile or private message for complete source code.