compiler-errors - 即使我已初始化视频系统,也未初始化

标签 compiler-errors pygame

即使初始化了错误视频系统,我也未初始化它。在不创建类的情况下执行操作时,不会出现错误。有人可以向我解释一下。

请参见下面的代码:

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/

相关文章:

compiler-errors - Gulp-Sass undefined variable -下划线转换为破折号

python - PyInstaller .exe 文件不工作

python - 如何使用pygame使圆形物体跳跃?

python - 使用 Pygame, Python 3 Blitting 非矩形 Sprite

python - 如何修复 pygame 中的 Sprite 动画?

python - 使用 Pygame 播放 ADPCM 流

c# - 编译器错误 CS0542 : member names cannot be the same as their enclosing type 的解决方法

c# - 为什么在创建和返回新结构时出现此错误?

c++ - "not declared in this scope"模板和继承错误

c++ - Visual Studio 2015 : v120 vs v140?