[英]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.