我的任务是制作一个游戏,应该有多个模块以避免在一个脚本中混乱。我遇到了从其中一个模块导入变量的问题。到目前为止,我有一个设置和一个主要设置。这些设置是相当简单的,去:python - 使用从模块导入的类的问题
class Settings():
def __init__(self):
self.screen_width = 1920
self.screen_height = 1080
self.bg_color = (230, 230, 230)
很简单,但是当我尝试引用这些变量,它说类“未解决属性引用‘SCREEN_WIDTH’‘设置’
主,就如同:
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问题,但发现它在空闲所以这将是导入变量的正确方法是一回事吗?
感谢您花时间阅读本文!
但它运行的权利? – danidee
我绝对不是专家,但不是'bg_color','screen_width'和'scree_height'部分的实例而不是类本身?换句话说,我不认为你可以访问Settings.bg_color,我认为你需要创建一个Settings对象,像's = Settings()',然后你可以执行'.bg_color'。 –
@danidee没有它不起作用 –