繁体   English   中英

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

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

所以,我正在做一个游戏项目,我决定,这一次,为我的 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)

而且,在游戏循环中,我决定制作这个 class 的实例,并调用

._fill()

方法,像这样:

# 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()

但是当我尝试运行主循环时,它给了我这个错误信息(我以前从未见过):

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

而且,也许除了那条特定的线:

self = pygame.display.set_mode((width, height))

我真的不知道为什么会这样。 也许是因为无法制作自定义屏幕 class?

所以我的问题是:为什么会这样? 如果我可以自定义 window class,怎么样? 因为它似乎不起作用...

非常欢迎任何帮助!

编辑 1:如果您需要所有文件的代码,我会让它们可见:D

尝试初始化父 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