首页 > 解决方案 > 检测游戏中涉及旋转件的碰撞问题

问题描述

我正在尝试为大学项目创建俄罗斯方块。但是我在进行碰撞时遇到了相当困难的时间。实际上,它们有效,直到我尝试旋转一块。在我旋转一块后,碰撞总是会返回True

这是代码:

def constraint(self):
    if self.shape_y + len(self.shape) == configuration.config['rows']:
        return True

    for shape_row, row in enumerate(self.shape):
        column_index = -1
        for x in range(self.shape_x, self.shape_x + len(self.shape[0])):
            column_index += 1
            if self.shape[shape_row][column_index] != 0:
                if shape_row+1 < len(self.shape):
                    if self.shape[shape_row+1][column_index] == 0:
                        if self.board.board[self.shape_y + 1][x] != 0:
                            return True
                else:
                    if self.board.board[self.shape_y + len(self.shape)][x] != 0:
                        print("qui")
                        return True
    return False

shape_y 是形状所在的行。len(self.shape)返回形状的行数,因为它像矩阵一样编码:

例子:

 [[0, 1, 0],
  [1, 1, 1]],

是上一格下三格的棋子。 Shape是代表这块的矩阵。 Shape_x是形状所在的列。

板是这样的矩阵:

self.board = np.array([[0 for _ in range(configuration.config["cols"])]
                            for _ in range(configuration.config['rows'])])

其中 0 是空闲的,其他数字是不空闲的块。

这是显示问题的屏幕截图:

图片

蓝色和绿色的碎片像发生碰撞一样被卡住,但在“半空中”,没有真正发生。

编辑1:

这是旋转的代码

def rotate(self):
    self.board.remove_piece(self)
    self.shape = np.rot90(self.shape)
    self.board.add_piece(self)

在哪里self.board.remove_piece(self)self.board.add_piece(self)只需删除并添加板内的值,以便我可以再次绘制它。所以,基本上,旋转代码只是self.shape = np.rot90(self.shape)

标签: pythonpython-3.xpython-2.7numpytetris

解决方案


我应该实际上解决了这个问题,错误在一个索引内,可以在板上检查。

    def constraint(self):
    if self.shape_y + len(self.shape) == configuration.config['rows']:
        return True

    for shape_row, row in enumerate(self.shape):
        column_index = -1
        for x in range(self.shape_x, self.shape_x + len(self.shape[0])):
            column_index += 1
            if self.shape[shape_row][column_index] != 0:
                if shape_row+1 < len(self.shape):
                    if self.shape[shape_row+1][column_index] == 0:
                        if self.board.board[self.shape_y + shape_row + 1][x] != 0:
                            return True
                else:
                    if self.board.board[self.shape_y + len(self.shape)][x] != 0:
                        return True
    return False

推荐阅读