Answer the question
In order to leave comments, you need to log in
How to fix the error that arose due to the incorrect construction of the program?
I decided to write a program in Python.
I declared a variable in the init function called self.screen
, it was saved in the App class
, and when I used the App.screen variable in the run function, an
error came out (
the error itself:
source code:
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' # Mute
import pygame as app # Main module
from pygame.locals import *
import os as os, sys as sys # System module
import time as time, random as random # Other module
# Settings
app.init()
clock = app.time.Clock()
font_basic = app.font.SysFont(None, 36)
font_addon = None
white = (255, 255, 255)
black = (0, 0, 0)
class App():
def Terminate():
app.quit()
sys.exit()
def Text(text, font, color,surface ,x, y):
text = font.render(str(text), True, color)
text_rect = text.get_rect()
text_rect.topleft = (x, y)
surface.blit(text, text_rect)
return text, text_rect
def init(self):
self.resolution = self.width, self.hight = (800, 450)
self.screen = app.display.set_mode(self.resolution, app.SCALED) # Переменная
app.display.set_caption("NRen")
app.display.set_icon(app.image.load('icon.png'))
def run(self):
while True:
App.Text("Ку, это тестовое сообщение)", font_basic, white, App.screen, 800, 450) # Тут ошибка
for i in app.event.get():
if i.type == QUIT:
App.Terminate()
if i.type == KEYDOWN:
if i.key == K_ESCAPE:
App.Terminate()
app.display.update()
application = App()
application.init()
application.run()
Answer the question
In order to leave comments, you need to log in
The screen attribute is created for you only at the time of creating an instance of the class ( application = App() ) and calling the init () method. If you try to call it like you - App.screen - your class knows nothing about it.
You need to replace App.screen with self.screen.
PS and it's better to rename init() to __init()__, then you won't need to call this method separately.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question