You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame中Player类出现AttributeError:vel属性未定义求助

问题:Pygame中Player类出现AttributeError:找不到vel属性

在使用Pygame编写代码时遇到怪异的AttributeError:明明类中定义了vel属性,却提示找不到。特殊的是只有第二个if语句会触发报错,注释该语句后无错误。使用的vector()是pygame.math.Vector2对象。

相关代码

class Player:
    def __init__(self, pos) -> None:
        self.surf = self.player_surf()
        self.pos = vector(pos[0],pos[1]-self.surf.get_height())
        self.vel = vector(0,0)
        self.rect = self.surf.get_rect(midbottom=self.pos)
    
    def player_surf(self):
        self.orient = 'Right'
        surf = pygame.image.load('assets\player\player.png').convert_alpha()

        if self.orient == 'Left' and self.vel.x > 0:
            self.orient = 'Right'
            surf = pygame.transform.flip(surf, True, False).convert_alpha()
        elif self.orient == 'Right' and self.vel.x < 0:
            self.orient = 'Left'
            surf = pygame.transform.flip(surf, True, False).convert_alpha()
        return surf

报错信息

Traceback (most recent call last):
  File "d:\Python_projects\Gyakorlások\platformer\main.py", line 84, in <module>
    game = main()
  File "d:\Python_projects\Gyakorlások\platformer\main.py", line 26, in __init__
    self.player = Player(vector(WIN_WIDTH//2, self.ground))
  File "d:\Python_projects\Gyakorlások\platformer\main.py", line 54, in __init__
    self.surf = self.player_surf()
  File "d:\Python_projects\Gyakorlások\platformer\main.py", line 66, in player_surf
    elif self.orient == 'Right' and self.vel.x < 0:
AttributeError: type object 'Player' has no attribute 'vel'

问题原因

核心是初始化顺序错误:

  1. 在__init__方法中,先调用self.player_surf()赋值self.surf,但self.vel是在这之后才定义的。
  2. 第一个if条件self.orient == 'Left'不成立(player_surf里刚把self.orient设为Right),所以不会执行到访问self.vel的代码;而第二个elif条件self.orient == 'Right'成立,会尝试访问self.vel.x,但此时self.vel还未被创建,因此触发AttributeError。

解决方案

调整__init__中的属性定义顺序,先创建self.vel,再调用player_surf():

class Player:
    def __init__(self, pos) -> None:
        self.vel = vector(0,0)  # 先初始化vel
        self.orient = 'Right'   # 把orient的初始化移到这里,避免重复定义
        self.surf = self.player_surf()
        self.pos = vector(pos[0],pos[1]-self.surf.get_height())
        self.rect = self.surf.get_rect(midbottom=self.pos)
    
    def player_surf(self):
        surf = pygame.image.load('assets/player/player.png').convert_alpha()  # 用正斜杠避免转义问题

        if self.orient == 'Left' and self.vel.x > 0:
            self.orient = 'Right'
            surf = pygame.transform.flip(surf, True, False).convert_alpha()
        elif self.orient == 'Right' and self.vel.x < 0:
            self.orient = 'Left'
            surf = pygame.transform.flip(surf, True, False).convert_alpha()
        return surf

额外优化点:

  • 将self.orient的初始化移到__init__中,避免每次调用player_surf都重复赋值。
  • 图片路径改用正斜杠/,避免Windows下的转义字符问题(也可以用原始字符串r'assets\player\player.png')。

内容的提问来源于stack exchange,提问作者MonadoMaster0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:55:19