【问题标题】:Python/Pygame : "pygame.error: display Surface quit"Python/Pygame:“pygame.error:显示表面退出”
【发布时间】:2021-10-07 17:22:06
【问题描述】:

所以,我正在制作一个游戏项目,我决定有一次为我的 pygame 窗口制作一个自定义类,如下所示:

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)

而且,在游戏循环中,我决定创建这个类的一个实例,并调用

._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))

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

所以我的问题是:为什么会这样?如果我可以制作自定义窗口类,如何?因为它似乎不起作用...

非常欢迎任何帮助!

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

【问题讨论】:

    标签: python class pygame


    【解决方案1】:

    尝试初始化父类。在你的 init 函数中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 2018-03-01
      • 2018-09-29
      相关资源
      最近更新 更多