为什么我的游戏会忽略我的真实错误陈述?

时间:2019-09-27 18:49:01

标签: python python-3.x if-statement pygame

我在pygame上做了一个游戏。我希望玩家的健康状况达到一定程度后才能改变速度。我尝试了if / else语句和true / false语句的组合-速度仍然不会改变。这是相关的代码:

class Player(pg.sprite.Sprite):
    def __init__(self, game, x, y):
        self.health = PLAYER_HEALTH
        self.speed = True
        if PLAYER_HEALTH <= 20:
            self.speed == False
        if PLAYER_HEALTH >= 20:
            self.speed == True

    def get_keys(self):
        PLAYER_RUN = 55
        PLAYER_RUNS = 300
        self.rot_speed = 0
        self.vel = vec(0, 0)
        keys = pg.key.get_pressed()
        if keys[pg.K_UP] or keys[pg.K_w]:
            if self.speed == True:
                self.vel = vec(PLAYER_RUNS, 0).rotate(-self.rot)
            else:
                self.vel = vec(PLAYER_RUN, 0).rotate(-self.rot)
        if keys[pg.K_DOWN] or keys[pg.K_s]:
            if self.speed == True:
                self.vel = vec(-PLAYER_RUNS / 2, 0).rotate(-self.rot)
            else:
                self.vel = vec(-PLAYER_RUN / 2, 0).rotate(-self.rot)

这是我用来定义精灵的文件中的代码。我以为导入此文件及其更改足以进行速度更改,但是它不起作用。我什至在处理玩家伤害的主循环中添加了代码:

if self.player.health <= 20:
    print(CONSTANT[hit_count])
    self.player.speed == False
    self.player.health += CONSTANT.popleft()
    hit_count += 1
    hit.vel = vec(0, 0)
elif self.player.health >= 20
    print(CONSTANT[hit_count])
    self.player.speed == True
    self.player.health += CONSTANT.popleft()

您看到什么可以解释为什么在玩家健康达到特定点后无法切换速度了吗?

1 个答案:

答案 0 :(得分:1)

我的猜测:我相信您不是在游戏循环中更新player.speed

据我了解,player.speed用作标志。如果为True,则播放器快速移动。否则,它会缓慢移动。根据您显示的代码,该值仅在构造函数中设置一次。

这是假设的 player.speed在您省略的代码的其他部分中未设置。