【发布时间】:2021-11-03 15:57:17
【问题描述】:
所以我已经在这个项目上工作了一周左右,我还没有能够完善矩形对象的碰撞。它适用于块上方和下方的碰撞,甚至当你在块的右侧时,但我无法弄清楚当玩家在左侧时如何修复碰撞。
当你靠墙移动时效果很好,但是当你在两个共享相同 y 坐标的块之间向左移动时,玩家会停下来,好像有墙挡住了。
这是与玩家右侧的块发生碰撞的代码,它按预期工作:
# character to the left of the block
if p1.x + p1.width / 2 < block.x and p1.dx > 0:
p1.dx = 0
p1.x = block.x - p1.width
而这是导致问题的代码:
# player is to the right of the block
elif p1.x + p1.width/2 > block.x + block.width and p1.dx < 0:
p1.dx = 0
p1.x = block.x + block.width
使用轴对齐边界块方法检查碰撞,块的 x 和 y 坐标位于左上角。
对于任何试图解决这个问题的人, 谢谢:)
【问题讨论】: