python 属性错误:“设置”对象没有属性“屏幕宽度”

c2e8gylq  于 2023-06-04  发布在  Python
关注(0)|答案(2)|浏览(325)

我一直试图建立一个外星人入侵游戏,但当我运行它给我以下:“属性错误:'Settings'对象没有属性'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)
moiiocjp

moiiocjp1#

我不知道,但经过一些格式化它的工作由我罚款。

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()
oknrviil

oknrviil2#

我也犯了同样的错误。设置存储在settings.py文件中,并使用类Settings的init方法进行设置:

class Settings:
"""A class to store all settings for Alien Invasion."""

    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)

我忘记了这个方法中的缩进,因此self.* 行没有被考虑,并且在另一个文件的alien_invasion类中不可用。

相关问题