python-使用从模块导入的类的问题

python-使用从模块导入的类的问题,python,Python,我的任务是制作一个游戏,我们应该有多个模块,以避免在一个脚本中出现混乱。我在从其中一个模块导入变量时遇到问题。到目前为止,我有一个设置和一个主设置。设置非常简单,如下所示: class Settings(): def __init__(self): self.screen_width = 1920 self.screen_height = 1080 self.bg_color = (230, 230, 230) 非常简单,但当我尝试引用这些变量时,它会为类“设置”显示“

我的任务是制作一个游戏,我们应该有多个模块,以避免在一个脚本中出现混乱。我在从其中一个模块导入变量时遇到问题。到目前为止,我有一个设置和一个主设置。设置非常简单,如下所示:

class Settings():
def __init__(self):
    self.screen_width = 1920
    self.screen_height = 1080
    self.bg_color = (230, 230, 230)
非常简单,但当我尝试引用这些变量时,它会为类“设置”显示“未解析属性引用“屏幕宽度”

主要内容如下:

import sys, pygame
from game_settings import Settings

def run_game():
    #Initialize the game and create the window
    pygame.init()
    screen = pygame.display.set_mode((Settings.screen_width,Settings.screen_height), pygame.FULLSCREEN)
    pygame.display.set_caption("Alien Invasion")

while True:

    #Listening for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             sys.exit()
    screen.fill(Settings.bg_color)
    pygame.display.flip()

run_game()
我原以为这可能是PyCharm的问题,但发现它在空闲时做同样的事情,那么导入变量的正确方法是什么呢


感谢您花时间阅读本文!

您需要创建设置对象的实例:
s=Settings()

用法:
s.bg\u颜色

像这样更改设置类,可以静态访问属性:

class Settings():
    screen_width = 1920
    screen_height = 1080
    bg_color = (230, 230, 230)

您需要创建设置对象的实例:
s=Settings()

用法:
s.bg\u颜色

像这样更改设置类,可以静态访问属性:

class Settings():
    screen_width = 1920
    screen_height = 1080
    bg_color = (230, 230, 230)

您需要创建
设置
类的实例,因为您在其
\uuuu init\uuuu
方法中设置的属性是实例属性

试着这样做:

def run_game():
    my_settings = Settings() # create Settings instance
    pygame.init()
    screen = pygame.display.set_mode((my_settings.screen_width, # lookup attributes on it
                                      my_settings.screen_height),
                                     pygame.FULLSCREEN)
    # ...

您需要创建
设置
类的实例,因为您在其
\uuuu init\uuuu
方法中设置的属性是实例属性

试着这样做:

def run_game():
    my_settings = Settings() # create Settings instance
    pygame.init()
    screen = pygame.display.set_mode((my_settings.screen_width, # lookup attributes on it
                                      my_settings.screen_height),
                                     pygame.FULLSCREEN)
    # ...

两个文件必须位于同一文件夹中,并且您必须创建
设置
类的实例。然后您可以访问实例的属性

main.py:

from game_settings import Settings
s = Settings()
print(s.bg_color)
class Settings():
    def __init__(self):
        self.screen_width = 1920
        self.screen_height = 1080
        self.bg_color = (230, 230, 230)
game\u settings.py:

from game_settings import Settings
s = Settings()
print(s.bg_color)
class Settings():
    def __init__(self):
        self.screen_width = 1920
        self.screen_height = 1080
        self.bg_color = (230, 230, 230)
运行
main.py
时,输出将为:

(230, 230, 230)

两个文件必须位于同一文件夹中,并且您必须创建
设置
类的实例。然后您可以访问实例的属性

main.py:

from game_settings import Settings
s = Settings()
print(s.bg_color)
class Settings():
    def __init__(self):
        self.screen_width = 1920
        self.screen_height = 1080
        self.bg_color = (230, 230, 230)
game\u settings.py:

from game_settings import Settings
s = Settings()
print(s.bg_color)
class Settings():
    def __init__(self):
        self.screen_width = 1920
        self.screen_height = 1080
        self.bg_color = (230, 230, 230)
运行
main.py
时,输出将为:

(230, 230, 230)

我绝对不是专家,但不是实例的一部分,而不是类本身?换句话说,我认为你不能只访问设置。bg_color,我认为你需要创建一个像
s=Settings()这样的设置对象
然后你可以做
s.bg_color
@danidee不,它不起作用。顺便说一句,如果你想了解更多关于这个问题的信息,请看这篇文章@AndrésAG谢谢,我错过了这么一个简单的问题。除了需要创建一个实例之外,我基本上都知道如何获得它。我肯定不是专家,但你是t
bg\u color
screen\u width
screen\u height
是实例的一部分,而不是类本身?换句话说,我认为您不能只访问设置。bg\u color,我认为您需要创建一个设置对象,如
s=Settings()
然后你可以做
s.bg_color
@danidee不,它不起作用。顺便说一句,如果你想了解更多关于这个问题的信息,请看这篇文章@AndrésAG谢谢,我遗漏了这么一个简单的问题。除了需要创建一个实例之外,我基本上都知道如何获得它。