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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…