【问题标题】:AttributeError: 'Settings' object has no attribute 'screen_width'AttributeError:“设置”对象没有属性“屏幕宽度”
【发布时间】:2020-12-07 17:08:34
【问题描述】:

我一直在尝试构建一个外星入侵游戏,但是当我运行它时,它给了我以下信息:“AttributeError: 'Settings' object has no attribute 'screen_width'”

我理解错误的含义,但我不明白为什么会出现错误。我仔细检查了我的拼写,甚至尝试将我的设置类和外星人入侵类中的名称更改为没有成功。有人知道可能出了什么问题吗?谢谢

class AlienInvasion:
    
    # Initialize the game and create game resources
    def __init__(self):
        pygame.init()
        self.ai_settings = Settings()

        #this code below creates the pygame screen. The set_caption method just changes th title of the window
        #remmeber that if we do not pass the attibutes (screen, etc), it is considered the default value and will not change until we change it via another method in the class
        self.screen = pygame.display.set_mode((self.ai_settings.screen_width, self.ai_settings.screen_height))
        pygame.display.set_caption("Alien Invasion")


    def run_game(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            #create the background color
            self.screen.fill(self.ai_settings.bg_color)
            pygame.display.flip()

    `if __name__ == '__main__':
        ai = AlienInvasion()
        ai.run_game()

class Settings:
    """A class to store all settings for the Alien Invasions game."""

    def __init__(self):
        #Screen Settings
        self.screen_width= 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)

【问题讨论】:

  • 两个类都在单独的文件中,我只是不知道如何在问题框中说明这一点,并且除了 settings.py 之外,我的 Alien_Invasion 目录中的任何地方都没有使用设置
  • 我没有,我什至尝试将类名从“设置”更改为“设置”,我得到了同样的错误

标签: python class pygame


【解决方案1】:

我不知道,但经过一些格式化后,我可以正常工作。

import pygame
import sys


class Settings:
"""A class to store all settings for the Alien Invasions game."""

def __init__(self):
    # Screen Settings
    self.screen_width = 200
    self.screen_height = 200
    self.bg_color = (0, 230, 230)


class AlienInvasion:

# Initialize the game and create game resources
def __init__(self):
    pygame.init()
    self.ai_settings = Settings()

    # this code below creates the pygame screen. The set_caption method just changes th title of the window
    # remmeber that if we do not pass the attibutes (screen, etc), it is considered the default value and will not change until we change it via another method in the class
    self.screen = pygame.display.set_mode((self.ai_settings.screen_width, self.ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")

def run_game(self):
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # create the background color
        self.screen.fill(self.ai_settings.bg_color)
        pygame.display.flip()


if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()

【讨论】:

  • 有效!就像你说的,它正在格式化。我没有缩进 init 方法。谢谢!
猜你喜欢
  • 2019-03-15
  • 2020-05-19
  • 2017-10-28
  • 2013-05-03
  • 2012-10-02
  • 2020-11-06
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多