如何解决检测游戏中涉及旋转棋子的碰撞问题
我正在尝试为大学项目创建俄罗斯方块。但是我在进行碰撞时遇到了相当困难的时间。实际上,它们起作用,直到我尝试旋转一块。在我旋转一块后,碰撞总是返回 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]],
是上一格下三格的棋子。
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)
解决方法
我应该真的解决了这个问题,错误是在一个索引内,要在板上检查。
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。