【问题标题】:Problem detecting collisions involving rotated pieces in game检测游戏中涉及旋转件的碰撞问题
【发布时间】:2021-06-01 08:34:08
【问题描述】:

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

【问题讨论】:

  • 你的工件旋转代码是什么?
  • @itprorh66 根据要求,我已在主帖中添加了轮换代码
  • 有人有什么建议吗?

标签: python python-3.x python-2.7 numpy tetris


【解决方案1】:

我应该已经解决了这个问题,错误是在一个索引中,要在板上检查。

    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

【讨论】:

    猜你喜欢
    • 2019-04-30
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多