所以我有错误:
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正在拿着寻人发射器,反之亦然。
感谢您的帮助!
答案 0 :(得分:0)
如果update
是一个类的实例,而hlauncher
是该类的成员函数,那么您的语法将是正确的。
但是hlauncher
是函数update()
中的local(?)变量。它也可以是全局变量,但是无法从代码中得知。假定它是本地的,则无法从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()
如果您在处理所有这些方面都遇到困难,也许可以快速了解variable scope在Python中的工作方式。