我怎样才能阻止我的播放器在我的平台上出现故障?

问题描述

所以我正在做一个小项目,我不明白为什么我的播放器在与我的平台碰撞时会一直上下移动,即使我希望它是静态的。问题出在下面的代码中,我找不到修复方法

project_name/project_name/settings.py
project_name/project_name/urls.py
project_name/imagem/urls.py
project_name/imagem/views.py
project_name/imagem/models.py

我不明白为什么它不像我这样做的边框系统那样无缝动画。

def Collision_Manager():
    if player.rect.colliderect(platform.rect):
        if abs(player.rect.bottom >= platform.rect.top):
            player.y = platform.y - player.height

解决方法

player.yplatform.y 是浮点坐标。 pygame.Rect 对象只能存储积分坐标。

该问题是在 .rect 属性更新为玩家位置并且坐标被截断时引起的。例如:

self.rect.topleft = int(self.x),int(self.y)

一个可能的解决方案可能是round坐标:

self.rect.topleft = round(self.x),round(self.y)

或四舍五入 y 坐标:

self.rect.topleft = round(self.x),int(math.ceil(self.y))

根据您代码的控制流,可能还需要在检测到碰撞时更新 .rect 属性:

if player.rect.colliderect(platform.rect):
    if player.rect.bottom >= platform.rect.top:
        platform.rect.bottom = platform.rect.top
        player.y = platform.rect.y

顺便说一下,abs() 在这里完全没用:

if abs(player.rect.bottom >= platform.rect.top):

if player.rect.bottom >= platform.rect.top:

不过,它没有任何危害,因为它是 abs(True)abs(False),结果为 1 或 0。