我正在尝试与我的矩形进行良好的碰撞,但我觉得我的方法不好,因为每当我工作并与我的其他平台发生碰撞时,我的播放器总是卡在上面,有没有办法让它在不卡住的情况下进行良好的碰撞我只是在寻找良好的碰撞谢谢
VIDEO<;正如你所看到的,我的播放器一直卡在平台上,当我与平台发生碰撞时,左右两侧的情况都是一样的,这只会使我的播放器卡在平台上,而不会发生适当的碰撞
# collisions
for platform in platforms:
if playerman.rect.colliderect(platform.rect):
collide = True
playerman.isJump = False
if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom) or
platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom)):
playerman.y = platform.rect.top - playerman.height + 1
playerman.moveright = True
playerman.moveleft = True
if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.top) or
platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom - 10)):
playerman.moveright = False
elif (platform.rect.collidepoint(playerman.rect.left, playerman.rect.top) or
platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom - 10)):
playerman.moveleft = False
else:
playerman.moveright = True
playerman.moveleft = True
我的完整代码:script
我一直在到处寻找合适的碰撞,但我找不到任何有效的
从你的视频来看,如果你从一个街区下面走上来,碰撞检测似乎不起作用
总的来说(参见下面的例子):我在游戏中遇到了相同的“难题”,就我所知,有两种可能的方法
你检查玩家离最近的“块”有多远,让玩家只移动这么远。这包括:
你根据玩家当前的速度移动他,然后检查是否有碰撞。如果发生碰撞,则将播放器向后移动,移动的像素量是播放器边框和块边框之间的交点
如果你想了解更多关于它的信息,并有深入的代码示例(如果你不想自己尝试错误,直到你弄明白为止),我建议在youtube上搜索pygame-2D碰撞检测,那里有很棒的老师
下面是我的collisiondetection_x_axis()方法的摘录(自我引用玩家!)
碰撞检测y轴:
编辑:这要求您在碰撞检查之间的移动小于块的宽度,否则,如果您的角色有足够的速度,他可能会通过块“故障”
注意:在进行碰撞测试之前,您应该考虑玩家的移动方向,这样可以更容易地确定玩家的哪一侧可能首先与方块碰撞。例如,如果您向右移动,则播放器的右侧将与块的左侧碰撞。然后为这两点编写碰撞检测以及后续操作(例如,重置到块前面的位置和
speed_x = 0
)PS:尝试使用函数pygame.Rect.colliderect,它测试两个矩形是否重叠(=碰撞),我感觉设置collidepoint函数的方式不会在所有可能的情况下返回碰撞
相关问题 更多 >
编程相关推荐