玩家一直坚持在游戏的平台上

2024-09-30 22:26:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试与我的矩形进行良好的碰撞,但我觉得我的方法不好,因为每当我工作并与我的其他平台发生碰撞时,我的播放器总是卡在上面,有没有办法让它在不卡住的情况下进行良好的碰撞我只是在寻找良好的碰撞谢谢

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

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


Tags: orrectrightfalsetrueiftop平台
1条回答
网友
1楼 · 发布于 2024-09-30 22:26:00

从你的视频来看,如果你从一个街区下面走上来,碰撞检测似乎不起作用

总的来说(参见下面的例子):我在游戏中遇到了相同的“难题”,就我所知,有两种可能的方法

  1. 预检查

你检查玩家离最近的“块”有多远,让玩家只移动这么远。这包括:

  • 检查哪些方块离玩家最近
  • 检查到每个闭合块的距离,并计算播放器可以沿z方向移动的剩余可能的x像素
  • 将播放器沿z方向精确移动x像素
  1. 事后检查(我用这个,因为那时我自己更容易弄清楚)

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

  • 根据球员的速度最大限度地移动球员
  • 检查所有闭合块(或地图上的所有块)的碰撞,您可以从中改进性能
  • 如果发生碰撞,请根据玩家与碰撞块相交的程度进行计算。这很容易,如果你的玩家的点击框是一个矩形,你使用一个由矩形块组成的tilemap,你可以简单地减去player.x和block.x坐标
  • 将播放器向后移动(在更新屏幕之前)该像素量

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

下面是我的collisiondetection_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

碰撞检测y轴:

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,它测试两个矩形是否重叠(=碰撞),我感觉设置collidepoint函数的方式不会在所有可能的情况下返回碰撞

相关问题 更多 >