首页 > 解决方案 > 我收到此错误 AttributeError: 'function' object has no attribute 'hlauncher' 同时尝试从另一个文件获取属性

问题描述

所以我有错误:

launchercheck = game.update.hlauncher()
AttributeError: 'function' object has no attribute 'hlauncher'

当我想按空格键射击我的武器时,在我在代码中添加它之前它就起作用了:

launchercheck = game.update.hlauncher()
if launchercheck is True:
    self.kill()

self.kill()只是为了测试我的功能是否If有效。

game.update.hlauncher()指的是我的main.py文件,它应该在“游戏”类中查找“更新”,并且在“更新”中它应该抓取 hlauncher hlauncher 应该在玩家拿起归位发射器时从 False 更改为 True。以下是游戏内更新:

def update(self):
    # update portion of the game loop
    self.all_sprites.update()
    self.camera.update(self.player)
    # game over
    if len(self.mobs) == 0:
        self.playing = False
    # player hits items
    hlauncher = False
    hits = pg.sprite.spritecollide(self.player, self.items, False)
    for hit in hits:
        if hit.type == 'health' and self.player.health < PLAYER_HEALTH:
            hit.kill()
            self.effects_sounds['health_up'].play()
            self.player.add_health(HEALTH_PACK_AMOUNT)
        if hit.type == 'pistol':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'pistol'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
        if hit.type == 'shotgun':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'shotgun'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
        if hit.type == 'rifle':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'rifle'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
        if hit.type == 'sniperrifle':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'sniperrifle'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
        if hit.type == 'launcher':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'launcher'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
        if hit.type == 'hominglauncher':
            hit.kill()
            self.effects_sounds['gun_pickup'].play()
            self.player.weapon = 'hominglauncher'
            game_folder = path.dirname(__file__)
            img_folder = path.join(game_folder, 'img')
            hlauncher = True
    # mobs hit player
    hits = pg.sprite.spritecollide(self.player, self.mobs, False, collide_hit_rect)
    for hit in hits:
        if random() < 0.7:
            choice(self.player_hit_sounds).play()
        self.player.health -= MOB_DAMAGE
        hit.vel = vec(0, 0)
        if self.player.health <= 0:
            self.playing = False
    if hits:
        self.player.hit()
        self.player.pos += vec(MOB_KNOCKBACK, 0).rotate(-hits[0].rot)
    # bullets hit mobs
    hits = pg.sprite.groupcollide(self.mobs, self.bullets, False, True)
    for mob in hits:
        for bullet in hits[mob]:
            mob.health -= bullet.damage
        mob.vel = vec(0, 0)

我已经尝试在主文件中将 () 放在 True 和 False 后面,这给出了相同的错误,并且我已经摆弄了 game.update.hlauncher() 每次我尝试拍摄以确认我持有时都应该查看 hlauncher归位发射器,反之亦然。

谢谢你的帮助!

标签: pythonpygame

解决方案


如果update是一个类的实例,并且hlauncher是该类的成员函数,那么您的语法将是正确的。

但是hlauncher是函数中的局部(?)变量update()。它也可能是一个全局变量,但不可能从代码中知道。假设它是本地的,则无法从更新函数外部访问它,因为它仅在此函数执行时存在 - 这就是本地定义的含义。如果hlauncher是全局定义的,则可以随时设置:hlauncher = True.

但最好将此功能添加到播放器类:

class Player:
    def __init__( self ):
        pygame.sprite.Sprite.__init__(self)
        ... # other stuff
        self.has_hlauncher  = False
        self.hlauncher_ammo = 0

    def addHLauncher( self ):
        self.has_hlauncher = True
        self.hlauncher_ammo += 10

然后代码可以调用:

self.player.addHLauncher()

如果你对这一切有困难,或许可以快速阅读一下 Python 中变量作用域的工作原理。


推荐阅读