视频系统未初始化,即使我已经初始化了

问题描述 投票:0回答:1

即使我初始化了视频系统,我也收到错误消息:视频系统未初始化。当我在不创建类的情况下执行此操作时,我不会收到错误。有人可以向我解释一下吗?

参见下面的代码:

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)
compiler-errors pygame
1个回答
2
投票

首先,screen没有正确存储,将其设为一个属性,稍后会对您有所帮助。

其次,在 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()
© www.soinside.com 2019 - 2024. All rights reserved.