玩家坚持平台碰撞Pygame



我正试图与我的矩形进行良好的碰撞,但我觉得我的方法很糟糕,因为每当我工作并与其他平台发生碰撞时,我的玩家总是被卡住,有没有办法让它在不被卡住的情况下进行良好的冲突我只是在寻找一个良好的碰撞谢谢!

视频<正如你所看到的,我的玩家一直卡在平台上,当我与平台碰撞时,左右方向都是一样的,这只会让我的玩家卡在没有适当碰撞的上

# 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


我的完整代码:脚本

我一直在到处寻找合适的碰撞,但我找不到任何有效的

从您的视频中看,如果您从块下面上来,碰撞检测似乎不起作用。

一般来说(见下面的例子(:我遇到了同样的";谜题;就我的比赛而言,就我所见,有两种可能的方式。

  1. 预检查

您检查玩家离最近的";块";并且让玩家只移动这么远。这包括:

  • 检查哪些区块离玩家最近
  • 检查到每个近距离块的距离,并计算玩家可以在z方向上移动的剩余可能x像素
  • 将玩家在z方向上精确移动x个像素
  1. 后期检查(当时我用这个是因为我自己更容易弄清楚(

根据玩家当前的速度移动玩家,然后检查是否发生碰撞。如果发生碰撞,则将玩家向后移动像素量,即玩家边界和块边界之间的交点。

  • 根据球员的速度最大限度地移动球员
  • 检查所有封闭区块的冲突(或者对于初学者来说,地图上的所有区块,你可以从那里提高性能(
  • 如果发生碰撞,请计算玩家与碰撞块的相交量。如果你的玩家的点击框是一个矩形,并且你使用由矩形块组成的tilemap,你可以简单地减去player.x和block.x坐标,这很容易
  • 将播放器向后移动(在更新屏幕之前(该像素量

如果你想了解更多关于它的信息,并有深入的代码示例(如果你不想在弄清楚之前自己尝试错误(,我建议在youtube上搜索pygame-2D碰撞检测,那里有很棒的老师。

以下是我的collision_x_axis((方法的摘录(自行参考玩家!(

# move player etc ...
for tile in map_tiles: # for all tiles on the map
if pygame.sprite.collide_rect(self.hitboxBody, tile):
if self.SpeedX < 0 and tile.rect.right > self.hitboxBody.rect.left:  # moving left and collision
# move char back to "in front of the wall"
self.rect.x += tile.rect.right - self.hitboxBody.rect.left
self.SpeedX = 0  # set speedX to zero, as we cannot move in that direction anymore
elif self.SpeedX > 0 and self.hitboxBody.rect.right > tile.rect.left:  # moving right and collision
# move char back to "in front of the wall"
self.rect.x -= self.hitboxBody.rect.right - tile.rect.left
self.SpeedX = 0  # set speedX to zero, as we cannot move in that direction anymore

碰撞检测轴:

for tile in map_tiles: # for all tiles on the map
if pygame.sprite.collide_rect(self.hitboxBody, tile):
if self.SpeedY < 0 and tile.rect.bottom > self.hitboxBody.rect.top:  # moving up
self.rect.y += tile.rect.bottom - self.hitboxBody.rect.top # move char back to "below the wall"
self.SpeedY = 0
elif self.SpeedY > 0 and self.hitboxBody.rect.bottom > tile.rect.top:  # moving downwards
self.rect.y -= self.hitboxBody.rect.bottom - tile.rect.top # move back to "on top of the wall"
self.SpeedY = 0
self.jumping = False  # on ground

编辑:这需要你在碰撞检查之间的移动小于一个方块的宽度,否则如果你的角色有足够的速度,他可能会在方块中"出故障"。

注意:在进行碰撞测试之前,您应该考虑玩家的移动方向,这样可以更容易地确定玩家的哪一侧可能首先与块碰撞。例如,如果你向右移动,那么玩家的右侧将与方块的左侧碰撞。然后写入这两个点的碰撞检测以及随后的操作(例如重置到块和speed_x = 0前面的位置(

PS:试着使用pygame功能。Rect.colliderect,它测试两个矩形是否重叠(=碰撞(,我有一种感觉,你设置碰撞点函数的方式不会在所有可能的情况下都返回碰撞。

最新更新