所以,我正在做一个游戏项目,我决定,第一次,为我的游戏窗口定制一个类,如下所示:
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)并且,在gameloop中,我决定创建这个类的一个实例,并调用
._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))我真的不知道为什么会这样。也许是因为不可能做一个定制的屏幕类?
所以我的问题是:为什么会发生这种事?如果我能做一个自定义窗口类,怎么做?因为它似乎不起作用.
任何帮助都是非常欢迎的!
编辑1:如果您需要所有文件的代码,我将使它们可见:D
发布于 2021-10-07 17:36:35
尝试初始化父类。在你的init功能里面。
def __init__(self, width: int, height: int):
"""Screen Class Constructor"""
super().__init__((width, height))
self = pygame.display.set_mode((width, height))https://stackoverflow.com/questions/69485368
复制相似问题