Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

python - I got this error AttributeError: 'function' object has no attribute 'hlauncher' while trying to get a attribute from another file

So I have the error:

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

When I want to press spacebar to shoot my weapon, it worked before I added this in the code:

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

The self.kill() is just there to test if my If function works.

game.update.hlauncher() refers to my main.py file there it should look in the class 'game' for 'update' and within 'update' it should grab hlauncher hlauncher should change from False to True upon the player picking up the homing launcher. Here's update within game:

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)

I already tried putting () behind True and False in the main file which gave the same error and I already fiddled around with game.update.hlauncher() which should look at hlauncher each time I try to shoot to confirm that I'm holding a hominglauncher and vice versa.

Thanks for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If update was an instance of a class, and hlauncher was a member-function of that class, then your syntax would be correct.

But hlauncher is a local(?) variable in the function update(). It could also be a global variable, but it's not possible to know from the code. Assuming it's local, there is no way to access it from outside the update function, because it only exists while this function is executing - that's what it means to be locally-defined. If hlauncher is globally defined, at any time it can be set: hlauncher = True.

But it would probably be best to add this functionality to the player class:

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

Then the code could call:

self.player.addHLauncher()

If you're having difficulty with all this, perhaps have a quick read of how variable scope works in Python.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...