python - 检测游戏中涉及旋转件的碰撞问题
问题描述
我正在尝试为大学项目创建俄罗斯方块。但是我在进行碰撞时遇到了相当困难的时间。实际上,它们有效,直到我尝试旋转一块。在我旋转一块后,碰撞总是会返回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)
解决方案
我应该实际上解决了这个问题,错误在一个索引内,可以在板上检查。
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
推荐阅读
- vb.net - 将 Like 函数与 CDate 函数一起使用
- ruby-on-rails - 如何将多个文件发送/上传到 Rails API?
- php - 如何通过删除所有属性来清理菜单 li,但为当前菜单项保留 `class="active"`?
- c# - 如何使用 .NET Core 在 Db2 数据库中设置属性 CharBitDataAsString?
- jquery - css:当由jquery创建的元素后不起作用
- python - Numpy exp 函数
- mysql - 如何按相应名称遍历表列表?
- odoo - 如何在 docker 中安装 odoo 12 企业版?
- swift - 使用 alamofire multipart 上传图像数组
- angular - 如何从多选中获取整个(选定的)对象 - Angular