Python AttributeError如何解决
更新一下:
问题已解决原来 _init_应该是两个_ , 即__init__苦笑
本人刚学python,在编写一个打飞船游戏出现以下异常:
pygame 1.9.4Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (most recent call last): File "alien_invasion.py", line 31, in run_game() File "alien_invasion.py", line 12, in run_game (ai_settings.screen_width, ai_settings.screen_height))AttributeError: 'Settings' object has no attribute 'screen_width'------------------(program exited with code: 1)请按任意键继续. . .本人通过网上重新命名和删除.pyc文件的方法均不能解决, 不知道问题出现在哪,还请大神指教
主程序:
import sysimport pygamefrom settings import Settingsdef run_game(): # 初始化pygame,设置和屏幕对象 pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invation") #设置背景颜色 bg_color = (ai_settings.bg_color) # 开始游戏主循环 while True: # 监视键盘和鼠标事件 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 每次循环都重绘屏幕 screen.fill(bg_color) # 让最近绘制的屏幕可见 pygame.display.flip()run_game()settings.py模组
#-*-coding:gbk-*-class Settings(): """存储《外星人入侵》的所有设置的类""" def _init_(self): """初始化游戏的设置""" #屏幕设置 self.screen_width = 1200 self.screen_height = 667 self.bg_color = (230,230,230) |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|