python - 我收到此错误 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归位发射器,反之亦然。
谢谢你的帮助!
解决方案
如果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 中变量作用域的工作原理。
推荐阅读
- python - 从 Python 调用 CustomVision 预测 API - 第二次不是魅力
- acumatica - 为什么我无法在我的交叉引用 (INItemXRef) 表中获得“当前”记录?
- discord - 是否有可能让 awaitReactions 给每个做出反应的人一个角色?
- laravel - 带有“LIKE”语句的雄辩查询在 Laravel 6 中不起作用
- laravel - Laravel 419 页面过期,同时通过刀片中的表单发布请求
- python - 有没有办法在保留章节元数据的同时下载视频?
- three.js - 如何相对于向量旋转笛卡尔坐标?
- android - 谷歌地图填充底部不适用于颤振Android
- regex - GSheets - 删除*后*一个单词的所有内容(但保留单词)
- javascript - 预期声明或声明 (Javascript)