Series entry

"Python3 Programming Actual Tetris Robot"

Block movement

For convenience, an absolute positioning function is designed, and the relative motion mode is changed to absolute positioning.

Block absolute positioning function

In our Block class, the tkinter.move function is used to move the blog. This function provides a relative distance method, and we need to calculate the displacement difference. This method is equivalent to changing the relative distance movement method into the absolute positioning function of the block, and all movement operations use this function.

def relocate(self, x, y):
    for block in self.objs:        # 遍历方块的block
        block.relocate(x - self.x, y - self.y)    # 移动操作
    self.x = x
    self.y = y

Move the box to the left

def moveLeft(self):
    if self.canPlace(self.x - 1, self.y):    # 判断是否能左移
        self.relocate(self.x - 1, self.y)    # 移动
        return True
    else:
        return False

Move the box to the right

def moveRight(self):
    if self.canPlace(self.x - 1, self.y):    # 判断是否能右移
        self.relocate(self.x + 1, self.y)    # 移动
        return True
    else:
        return False

Move the box down

When the box moves down, it needs to be judged whether it has reached the bottom, and the position of the box will be fixed after the bottom.

def moveDown(self):
    if self.canPlace(self.x, self.y + 1):     # 判断是否能下移
        self.relocate(self.x, self.y + 1)     # 下移
        return True
    else:                                     # 已经到底
        for i in range(TETRISDIMENSION):
            for j in range(TETRISDIMENSION):
                if self.data[i][j]:           # 固定方块,改游戏空间格局
                    GameRoom[self.y + i][self.x + j] = 1
        return False

Cube rotation

The square rotation is essentially a rectangular rotation. We set to press the up button once and the square rotates 90 degrees clockwise.

Algorithm principle

As shown in the figure, press the icon to rotate the matrix. The normal matrix rotation algorithm is to exchange rows and then transpose. We need to control the block to continuously rotate clockwise, because the initial state is fixed, and the real-time shape of the block can be known as long as the number of rotations is remembered, so we choose the rotation algorithm.

Algorithm formula

                   a[i,j]      ------------->  a[j,N-i-1]

                      ↑                            ↓

                      ↑                            ↓

                  a[N-j-1,i]  <-------------  a[N-i-1,N-j-1]

Algorithm implementation

def rotate(self):
    for i in range(TETRISDIMENSION // 2):
        lenJ = TETRISDIMENSION - i - 1                 # 增加变量,简化下标
        for j in range(i, lenJ):
            lenI = TETRISDIMENSION - j - 1
            t = self.data[i][j]
            self.data[i][j] = self.data[lenI][i]        # 按公式轮换
            self.data[lenI][i] = self.data[lenJ][lenI]
            self.data[lenJ][lenI] = self.data[j][lenJ]
            self.data[j][lenJ] = t
    self.rotateCount += 1                               # 旋转次数记录
    self.redraw()                                       # 重绘

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

zhoutk
2.6k 声望1.2k 粉丝

自由程序员,技术路线c,delphi,c++,c#,java,php,node.js,python,golang,typescript;超喜欢react.js的设计思路,全栈开发成为我的终极目标。开发机器macbook pro,或装ubuntu、fedora的机器,编程用vim...