Python3 programming actual Tetris robot (multi-threaded problem)


Series entry

"Python3 Programming Actual Tetris Robot"

problem found

During the test, it was found that there was a program error, but the timer was turned off and there would be no problem without automatic falling. The reason is that the Timer will open a new thread, which will cause resource conflicts with the main thread.


The first thing that comes to mind is locking. The game logic is very simple, and locking should be easy to solve the problem. But no matter if I add coarse-grained or fine-grained as much as possible, it will deadlock in the end. Finally, I printed it and found that the program stopped at tkinter.Canvas.move. I personally think this is a bug in tkinter.
This road fails, so change your mind. Open a worker thread to complete all operations. The main thread and timer operations are just submitting tasks to the worker thread. That is, only one worker thread is allowed to do the task, thus avoiding the problem of resource conflicts.


Analysis of Locking Scheme

Keyboard response lock

tickLock[0] = True
with curTetrisLock:
    print("-------+++---00000000--- get lock", tickLock)
    if ke.keysym == 'Left':
    if ke.keysym == 'Right':
    if ke.keysym == 'Up':
    if ke.keysym == 'Down':
    if ke.keysym == 'space':
print("-------+++---00000000--- lose lock", tickLock)

Timer response lock

def tickoff(self):
    if self.gameRunningStatus == 1:
        if not tickLock[0]:
            with curTetrisLock:
                print("------------------ get lock", tickLock[1])
            print("================== lose lock", tickLock[1])
        self.tick = Timer(self.gameSpeedInterval / 1000, self.tickoff)

identify the problem

The program finally stopped at tkinter.Canvas.move in the Block class, which was triggered by the timer each time and could not be released.

Interested students can go to the project and switch to the lockbug branch to study. I wrote a lot of printouts to facilitate problem location.

Increase worker threads

Task unit design

A new Queue is added. The keyboard response and timer response add task units to the queue, and the worker threads process these tasks one by one. The task unit is designed as follows:


Each task unit is a two-tuple (convenient for data deconstruction), the first is a string, which is a command; the second is a tuple, which is a data packet (also designed in a convenient way to deconstruct), by each Each command is self-defined.

Worker thread

def opWork(self):
    while True:
        if not opQueue.empty():
            cmd,data = opQueue.get()
            if op == "Left":
            elif op == "Right":
            elif op == "Up":
            elif op == "Down":
            elif op == "space":
            elif op == "quit":

Keyboard response transformation

def processKeyboardEvent(self, ke):
    if == 1:
        if ke.keysym == 'Left':
        if ke.keysym == 'Right':
        if ke.keysym == 'Up':
        if ke.keysym == 'Down':
        if ke.keysym == 'space':

Timer transformation

The main function of game control. After the cube drops to the bottom, it will eliminate the layer, count the score, determine the speed level, determine whether the game is over, and move the next cube into the game space and generate a cube to display in the next cube display space.

def tickoff(self):
    if self.gameRunningStatus == 1:
        self.tick = Timer(self.gameSpeedInterval / 1000, self.tickoff)

project address

Operation method

1. install python3, git
2. git clone (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:
阅读 419

自由程序员,技术路线c,delphi, c++,c#,java,php,node.js,python,golang,typescript;超喜欢re...


2.6k 声望
1.2k 粉丝
0 条评论


2.6k 声望
1.2k 粉丝