background

In this series of articles, Python3 is used to record the writing process of Tetris game step by step. The game features include manual game, game playback (database operation), automatic game (AI robot), reinforcement learning, and optimization of AI robot. Has completed C++ version, QT5 QGraphicSitem written Tetris Tetris game.

planning

  • The project structure
  • Interface design
  • Block class, the smallest square definition
  • Tetris class, Tetris definition
  • Tetris cube movement and rotation
  • Game class, game flow control
  • Multithreaded transformation
  • Game Pause
  • Design universal database operation encapsulation, based on SQLite3
  • Design ORM to realize automatic query
  • Store historical data to achieve game playback
  • Simple AI
  • Memory leak correction
  • Reinforcement learning
  • Enhanced AI

Design ideas

The game tries to avoid using third-party libraries and focuses on algorithms, so the interface library is built into Python’s Tkinter. The design concept also takes the traditional approach of using a two-dimensional array to control the game space, similar to a maze. One advantage of this approach is that the game’s data is intuitive and easily accessible.

rendering

Size of Game Space

10 x 20

Score design:

  • One level is worth one point
  • Eliminate two floors and score three points
  • Seven points for three floors
  • Eliminate four levels and score ten points

Control keys

  • Up direction key: Rotate
  • Direction Left: Move left
  • Direction right: move right
  • Down key: Move down
  • Space bar: move down to the bottom

The project schedule

Manual game, game playback, simple AI, manual automatic switch at any time have been completed.

Project Label (Mileage Plate)

  • V1.0.1 (manual-play) – Manual Game Features Milestone Edition
  • V1.1 (Auto-play) – Automatic game feature milestone version, memory leaks are found
  • V1.2 (less-block) – Resolves memory leaks

The project address

https://gitee.com/zhoutk/ptetris or 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 projects

C++ version has been implemented, project address:

https://gitee.com/zhoutk/qtetris