python - pygame.错误: display Surface quit after filling the surface black

标签 python pygame

当我在 pygame 中将表面涂成黑色后。我收到错误

pygame.error: display Surface quit.

完整错误:

>
D:\Programme\Anaconda3\envs\gameDev\python.exe "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 81, in <module>
    thegame.execute()
  File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 74, in execute
    self.render(self._display_surf)
  File "C:/Users/xxx/PycharmProjects/Workspace/pygame snake/main.py", line 41, in render
    _display_surf.fill((0,0,0))
pygame.error: display Surface quit
<Surface(Dead Display)>

我尝试代替:

_display_surf.fill((0,0,0))

使用:

_display_surf.fill(pygame.color("black"))

但这也不起作用。

这是我的完整源代码:

import pygame
from pygame.locals import *


class Player(object):
    x = 10
    y = 10
    speed = 1

    def moveRight(self):
        self.x = self.x + self.speed

    def moveLeft(self):
        self.x = self.x - self.speed

    def moveUp(self):
        self.y = self.y - self.speed

    def moveDown(self):
        self.y = self.y + self.speed


class game:
    def __init__(self):
        self.resolution = (800, 500)
        self._running = True
        self._display_surf = None
        self.player = Player()

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(self.resolution)
        pygame.display.set_caption("Snake!")
        self._running = True

    def on_cleanup(self):
        pygame.quit()

    def render(self, _display_surf):
        print(_display_surf)
        _display_surf.fill((0,0,0))
        pygame.draw.rect(self._display_surf, (0, 128, 255), pygame.Rect(self.player.x, self.player.y, 30, 30))
        pygame.display.flip()

    def loop(self):
        pass

    def on_event(self, event):
        if event.type == pygame.quit():
            print("quiet")
            self._running = False

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                self.player.moveRight()

            elif event.key == pygame.K_LEFT:
                self.player.moveLeft()

            elif event.key == pygame.K_UP:
                self.player.moveUp()

            elif event.key == pygame.K_DOWN:
                self.player.moveDown()

    def execute(self):
        if self.on_init() == False:
            self._running = False

        while self._running:
            for event in pygame.event.get():
                self.on_event(event)

            self.render(self._display_surf)
            self.loop()
        self.on_cleanup()


if __name__ == "__main__":
    thegame = game()
    thegame.execute()

我希望我的表面是黑色的,并且什么也没有发生。但相反,当我尝试将其漆成黑色时,它崩溃了。希望有人能帮忙

解决方案: 对于任何对此感兴趣的人

而不是

        if event.type == pygame.quit():
            print("quiet")
            self._running = False

我需要

        if event.type == pygame.QUIT:
            print("quiet")
            self._running = False

最佳答案

代码

 if event.type == pygame.quit():
     print("quiet")

没有做你期望的事情。 pygame.quit()是一个函数调用并取消初始化所有 pygame 模块。该函数返回 None,因此条件失败。代码运行完并在下一条尝试访问 pygame 模块的指令处崩溃。

您必须将 event.type 与事件枚举器常量 pygame.QUIT 进行比较,后者标识 quit 事件:

if event.type == pygame.QUIT:
    print("quiet")
    self._running = False

请参阅 pygame.event 的文档。

关于python - pygame.错误: display Surface quit after filling the surface black,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56303548/

相关文章:

python - 仅在 mask 区域计算梯度

python - pivot_table 索引中的 NaN 值导致数据丢失

python - 如何更改 pygame 窗口顶部栏的颜色?

python - 如何从 Python 向游戏发送方向盘/操纵杆输入?

python - Pygame 窗口在几秒钟后没有响应

python - 尝试使用 Google Translate API 翻译 Discord Message 的结果只是组成输入的字母列表

python - "from a import b"不起作用,但我可以导入 a 然后使用 a.b

python - InterfaceError :(sqlte3. InterfaceError)绑定(bind)参数0时出错

Python:在 Windows 上的 Ubuntu 上通过 Bash 运行 pygame

python - 我如何使用二进制列表在 PyGame 中绘制正方形?