So, I’m making a game project, and I decided, for once, to make a custom class for my pygame window, like so :
class Screen(pygame.Surface): """Class for making a screen""" def __init__(self, width: int, height: int): """Screen Class Constructor""" self = pygame.display.set_mode((width, height)) def _events(self): """Events Handler""" for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() def _fill(self, color): self.fill(color)
And, in the gameloop, I decided to make an instance of this class, and to call the
._fill()
method, like so :
# Importing import pygame import ScreenClass import StaticsClass # Class with multiple colors # Functions def main(): """Main function, where all the game happens..""" scr = ScreenClass.Screen(800, 600) print(type(scr)) while True: scr._fill(StaticsClass.Color.BLACK0) scr._events() if __name__ == '__main__': main()
But when I try to run the main loop, it gives me this error message (that I never saw before) :
Traceback (most recent call last): File "C:UsersPCDesktopPYTHONJeuxA While Agomainloop.py", line 17, in <module> main() File "C:UsersPCDesktopPYTHONJeuxA While Agomainloop.py", line 13, in main scr._fill(StaticsClass.Color.BLACK0) File "C:UsersPCDesktopPYTHONJeuxA While AgoScreenClass.py", line 22, in _fill self.fill(color) pygame.error: display Surface quit
And, except maybe for THAT particular line :
self = pygame.display.set_mode((width, height))
I really don’t know why it happens. Maybe it’s because it’s not possible to make a custom Screen class ?
So my question is : Why is this happening ? If I can make a custom window class, how ? Because it doesn’t seem to work…
Any help would be greatly welcome!
EDIT 1: If you need the code of all the files, I will make them visible :D
Advertisement
Answer
Try initializing the parent class. Inside your init function.
def __init__(self, width: int, height: int): """Screen Class Constructor""" super().__init__((width, height)) self = pygame.display.set_mode((width, height))