首页 > 解决方案 > AttributeError:我用来制作这个的教程视频中的一切都是正确的,但仍然存在错误

问题描述

我使用 youtube 视频制作了这款平台游戏。定义了所有属性和参数,但仍然出现错误。这是我的代码块:

def update(self):
        # Game Loop Update
        self.all_sprites.update()

        # Check if player hits a platform only if falling
        if self.player.vel.y > 0:
            hits = pg.sprite.spritecollide(self.player, self.platforms, False)
            if hits:
                self.player.pos.y = hits[0].rect.top + 1
                self.player.vel.y = 0

        # If player reaches top 1/4 of screen
        if self.player.rect.top <= HEIGHT / 4:
            self.player.pos.y += abs(self.player.vel.y)
            for plat in self.platforms:
                plat.rect.y += abs(self.player.vel.y)
                if plat.rect.top >= HEIGHT:
                    plat.kill()

这是玩家类的额外信息:

class Player(pg.sprite.Sprite):
    def __init__(self, game):
        pg.sprite.Sprite.__init__(self)
        self.game = game
        self.image = pg.Surface((30,40))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2, HEIGHT/2)
        self.pos = vec(WIDTH/2, HEIGHT/2)
        self.vel = vec(0,0)
        self.acc = vec(0,0)

在代码中给出的第 6 行,控制台给出错误: AttributeError: 'bool' object has no attribute 'vel' 有人可以帮忙吗?告诉我是否需要提供更多信息。

标签: pythonattributeerror

解决方案


根据错误消息,包含一个布尔值,而不是您期望self.player的具有属性的对象。vel

问题不在您提供的代码中。我建议您查看周围的代码以确保正确处理此属性。


推荐阅读