我之前发布过关于我正在从事的这个项目(你的回答有帮助!)但我仍然有点模糊为什么我的碰撞检测报告不完全正确。对于游戏所走的每一个图块,它都会返回 true,包括当我将我的玩家角色移动到可碰撞的图块上时。我只想知道我的碰撞检测算法是否有问题,或者其他地方是否有问题。
def checkPlayerEnvCollision(self,player):
rowCount = 0
for row in self.cLayer:
colCount = 0
for col in row:
print("col: " + str(col))
#print(str(self.Level.tileLayer[row][col]) + ": " + str(c[row][col]))
if col:
grid_position = (colCount*32, rowCount*32)
collide_x = False
collide_y = False
print(str(grid_position))
# check x axis for collision
if player.x + 32 > grid_position[0]:
collide_x = True
elif player.x < grid_position[0] + 32:
collide_x = True
print("Player x: " + str(player.x) + " Player y : " + str(player.y))
# check y axis for collision
if player.y < grid_position[1] + 32:
collide_y = True
elif player.y + 32 > grid_position[1]:
collide_y = True
print("collide_x: " + str(collide_x) + " " + "collide_y: "+ str(collide_y))
# act on a collision on both axis
if collide_x and collide_y:
print("COLLISION DETECTED")
else:
print("NOCOLLISION___")
colCount += 1
rowCount += 1
谢谢您的帮助!