AttributeError: класс «имя класса» не имеет атрибута «переменная» - PullRequest
0 голосов
/ 21 января 2020

У меня такое ощущение, что ответ будет очень очевидным, но я получаю вышеуказанную ошибку. Я попытался поставить скобки, чтобы назвать класс, но это не исправило ошибку.

Модуль настроек:

class Settings:
    def __init__(self):
        self.title = "Historic City Builder"


class Screen:
    def __init__(self):
        self.width = 1000
        self.height = 650


class Colors:
    def __init__(self):
        self.red = (255, 0, 0)
        self.green = (0, 255, 0)
        self.blue = (0, 0, 255)

        self.black = (0, 0, 0)
        self.white = (255, 255, 255)

Основной:

import pygame as pg

from Settings import Settings
from Settings import Screen
from Settings import Colors

On = True

clock = pg.time.Clock()
pg.init()
MainDisplay = pg.display.set_mode((Screen.width, Screen.height))
pg.display.set_caption(Settings.title)

1 Ответ

0 голосов
/ 21 января 2020

Вы не инициализировали свой класс, поэтому __init__ не запустился и не установил его переменные так, как вы закодировали.

Попробуйте инициализировать свой класс заранее, чтобы он выполнял блок кода __init__.

import pygame as pg

from Settings import Settings
from Settings import Screen
from Settings import Colors

On = True

clock = pg.time.Clock()
pg.init()
s = Screen() #Initialise the class Screen() as s
settings = Settings() #Init the class Settings() as settings
MainDisplay = pg.display.set_mode((s.width, s.height))
pg.display.set_caption(settings.title)

Вот небольшое чтение, чтобы помочь;

self и __init__

...