即使初始化了错误视频系统,我也未初始化它。在不创建类的情况下执行操作时,不会出现错误。有人可以向我解释一下。
请参见下面的代码:
from pygame import *
import pygame
class App:
def __init__(self):
self.exitgame = False
self.surface = (640, 500)
pygame.init()
def setup(self):
display.set_mode(self.surface)
display.update()
def exitapp(self):
if self.exitgame:
quit()
if __name__ == "__main__":
game = App()
game.setup()
switch = {QUIT: quit()}
while not game.exitgame:
for event in event.get():
switch.get(event.type)
最佳答案
提示:使用CTRL + K正确格式化代码
首先,屏幕存储不正确,将其设置为属性,将在以后为您提供帮助。
其次,在switch字典中,始终运行quit函数,并且在事件循环中不起作用。
第三,您要两次导入pygame。
第四,不要使用quit,请使用pygame.quit()。
第五,可以删除几个无用的函数,而不会影响应用程序类。
这是正确的代码:
import pygame
class App:
def __init__(self):
self.exitgame = False
self.surface = (640, 500)
self.screen = pygame.display.set_mode(self.surface)
def setup(self):
pass
if __name__ == "__main__":
game = App()
game.setup()
while not game.exitgame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.exitgame = True
pygame.quit()
关于compiler-errors - 即使我已初始化视频系统,也未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50118613/