So, I’m making a game project, and I decided, for once, to make a custom class for my pygame window, like so :
JavaScript
x
19
19
1
class Screen(pygame.Surface):
2
"""Class for making a screen"""
3
4
def __init__(self, width: int, height: int):
5
"""Screen Class Constructor"""
6
self = pygame.display.set_mode((width, height))
7
8
9
def _events(self):
10
"""Events Handler"""
11
12
for event in pygame.event.get():
13
if event.type == pygame.QUIT:
14
pygame.quit()
15
quit()
16
17
def _fill(self, color):
18
self.fill(color)
19
And, in the gameloop, I decided to make an instance of this class, and to call the
JavaScript
1
2
1
._fill()
2
method, like so :
JavaScript
1
18
18
1
# Importing
2
import pygame
3
import ScreenClass
4
import StaticsClass # Class with multiple colors
5
6
# Functions
7
def main():
8
"""Main function, where all the game happens.."""
9
scr = ScreenClass.Screen(800, 600)
10
print(type(scr))
11
12
while True:
13
scr._fill(StaticsClass.Color.BLACK0)
14
scr._events()
15
16
if __name__ == '__main__':
17
main()
18
But when I try to run the main loop, it gives me this error message (that I never saw before) :
JavaScript
1
9
1
Traceback (most recent call last):
2
File "C:UsersPCDesktopPYTHONJeuxA While Agomainloop.py", line 17, in <module>
3
main()
4
File "C:UsersPCDesktopPYTHONJeuxA While Agomainloop.py", line 13, in main
5
scr._fill(StaticsClass.Color.BLACK0)
6
File "C:UsersPCDesktopPYTHONJeuxA While AgoScreenClass.py", line 22, in _fill
7
self.fill(color)
8
pygame.error: display Surface quit
9
And, except maybe for THAT particular line :
JavaScript
1
2
1
self = pygame.display.set_mode((width, height))
2
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.
JavaScript
1
5
1
def __init__(self, width: int, height: int):
2
"""Screen Class Constructor"""
3
super().__init__((width, height))
4
self = pygame.display.set_mode((width, height))
5