background
This series of articles uses Python3 to record the entire process of writing Tetris games step by step. The game functions include manual games, game playback (database operations), automatic games (AI robots), reinforcement learning, and optimized AI robots. The C++ version has been completed, QGraphicsItem of Qt5 writes Tetris Tetris game .
planning
- Project structure
- interface design
- block class, the smallest block definition
- tetris class, Tetris definition
- tetris block movement and rotation
- game category, game flow control
- Multi-threaded transformation
- Game pause function
- Design general database operation package, based on sqlite3
- Design ORM to realize automatic query
- Store historical data to realize game playback
- Simple AI
- Memory leak correction
- Reinforcement learning
- Enhanced AI
Design ideas
The game does not use third-party libraries as much as possible, and mainly focuses on algorithms, so the interface library chooses tkinter built in python. The design concept also adopts the traditional way, using a two-dimensional array to control the game space, similar to the way of a maze. One advantage of choosing this method is that the game data is intuitive and easy to obtain.
Effect picture
Game space size
10 x 20
Scoring design:
- One level is scored
- 3 points for the second floor
- Eliminate three levels to get seven points
- Eliminate four levels to get ten points
Control keys
- Up arrow key: Rotate
- Left arrow key: Move left
- Right direction: move right
- Down arrow key: Move down
- Space key: move down to the end
project progress
The manual game, game playback, simple AI, manual and automatic switching at any time have been completed.
Item label (mileage tag)
- v1.0.1(manual-play)-Milestone version of manual game function
- v1.1(auto-play)-Milestone version of automatic game function, memory leak found
- v1.2(less-block)-Solve memory leaks
project address
https://gitee.com/zhoutk/ptetris
或
https://github.com/zhoutk/ptetris
Operation method
1. install python3, git
2. git clone https://gitee.com/zhoutk/ptetris (or download and unzip source code)
3. cd ptetris
4. python3 tetris
This project surpport windows, linux, macOs
on linux, you must install tkinter first, use this command:
sudo apt install python3-tk
Related items
C++ version has been implemented, project address:
https://gitee.com/zhoutk/qtetris
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。