简体   繁体   English

Python/Pygame:“pygame.error:显示 Surface 退出”

[英]Python/Pygame : "pygame.error: display Surface quit"

So, I'm making a game project, and I decided, for once, to make a custom class for my pygame window, like so:所以,我正在做一个游戏项目,我决定,这一次,为我的 pygame window 定制一个 class,如下所示:

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而且,在游戏循环中,我决定制作这个 class 的实例,并调用

._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:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\mainloop.py", line 17, in <module>
    main()
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\mainloop.py", line 13, in main
    scr._fill(StaticsClass.Color.BLACK0)
  File "C:\Users\PC\Desktop\PYTHON\Jeux\A While Ago\ScreenClass.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?也许是因为无法制作自定义屏幕 class?

So my question is: Why is this happening?所以我的问题是:为什么会这样? If I can make a custom window class, how?如果我可以自定义 window class,怎么样? 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编辑 1:如果您需要所有文件的代码,我会让它们可见:D

Try initializing the parent class. Inside your init function.尝试初始化父 class。在你的 init function 中。

def __init__(self, width: int, height: int):
    """Screen Class Constructor"""
    super().__init__((width, height))
    self = pygame.display.set_mode((width, height))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM