Python AttributeError如何解决

更新一下:

问题已解决
原来 _init_应该是两个_ , 即__init__
苦笑


本人刚学python,在编写一个打飞船游戏出现以下异常:

 pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "alien_invasion.py", line 31, in <module>
    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 sys

import pygame

from settings import Settings

def 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)


Jabiiin
浏览 3186回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python