7

谁能告诉我为什么我的应用程序退出:

pygame 错误:显示 Surface 退出。

4

9 回答 9

11

我遇到了类似的问题,发现 Surface 对象不喜欢被深度复制。当我在此类对象上使用 copy.deepcopy() 然后访问副本时,我收到了奇怪的错误消息(没有调用 pygame.quit())。也许您会遇到类似的行为?

于 2010-11-30T19:37:39.303 回答
5

我在一段非常简单的代码中遇到了类似的问题:

    import sys, pygame
    pygame.init()

    size = width, height = 640, 480
    speed = [2, 2]
    black = 0, 0, 0

    screen = pygame.display.set_mode(size)
    ball = pygame.image.load("Golfball.png")
    ballrect = ball.get_rect()
    while 1:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
                pygame.quit()

        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]
        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()
        pygame.time.delay(5)

错误信息是:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "bounce.py", line 22, in <module>
        screen.fill(black)
    pygame.error: display Surface quit

所以我把

    import pdb

在顶部之后

    pygame.init()

并使用

    pdb.set_trace()

    pygame.quit()

现在我运行程序,点击关闭窗口,看到自己掉进了调试器,其实有点惊讶(毕竟,我希望退出会立即将我完全带出去)。所以我得出的结论是,退出实际上并没有在那时停止一切。看起来该计划在退出之后仍在继续,正在达到

    screen.fill(black)

这导致了问题。所以我加了

    break

之后

    pygame.quit()

现在一切都很愉快。

[稍后添加:我现在想到

    pygame.quit()

正在退出模块,而不是正在运行的程序,因此您需要中断才能退出这个简单的程序。]

仅作记录,这意味着好的版本是

    import sys, pygame
    pygame.init()

    size = width, height = 640, 480
    speed = [2, 2]
    black = 0, 0, 0

    screen = pygame.display.set_mode(size)
    ball = pygame.image.load("Golfball.png")
    ballrect = ball.get_rect()
    while 1:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
                pygame.quit()
                break

        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]
        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()
        pygame.time.delay(5)
于 2012-03-01T18:39:04.557 回答
3

替换if event.type == pygame.quit():if event.type == pygame.QUIT:

于 2018-09-19T10:22:36.843 回答
2

确保你写pygame.QUIT:的不是pygame.quit(): 我知道这听起来很奇怪,但我遇到了同样的问题。

于 2020-01-10T19:13:16.200 回答
2
import pygame, sys

running = True
while running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit() # quit the screen
        running = False
        sys.exit()

在 pygame.quit() 之后调用 sys.exit() 以停止程序,这样您在退出 pygame 后就无法更改表面并且不会出现错误

于 2016-02-17T14:47:08.863 回答
1

来自http://archives.seul.org/pygame/users/Nov-2006/msg00236.html

2006 年 11 月 30 日星期四 21:27 -0300,Nicolas Bischof 写道:

pygame.error: display Surface quit 这是什么意思?,我无法修复

这意味着调用了 pygame.display.quit() 或 pygame.quit()。如果您在退出后尝试对显示表面进行任何操作,您将收到此错误。

于 2010-01-04T14:43:37.277 回答
1

我也有这个问题,但从另一个来源得到它。

我有一个这样定义的类:

class PauseMenu(pygame.Surface)

我忘记初始化 pygame.Surface 并尝试对其进行 blit 时出现错误,导致 pygame.screen 崩溃并给了我这个错误。

所以我很明显地添加了这个

pygame.Surface.__init__(self, (width, height))
于 2013-04-27T18:40:21.320 回答
1

我刚才遇到了类似的问题,我已经找到了解决方案。

因为看起来pygame.quit()只会退出 pygame 模块而不是整个程序,所以在下一行使用sys.exit()方法。

在这之后:

pygame.quit()

放置这个:

sys.exit()

完整片段:

pygame.quit()
sys.exit()
于 2021-03-18T14:05:46.917 回答
0

我也遇到了这个问题,与 Maciej Miąsik 的回答类似,我的答案与 copy.deepcopy()-ing 图像有关。

我有:

import copy,pygame,sys
from pygame.locals import *

EMPTY_IMG= pygame.image.load('C:super/fancy/file/path/transparent.png')
held_image=copy.deepcopy(EMPTY_IMG)
my_rect=held_image.get_rect()
my_rect.center = (50,50)
screen.blit(held_image,my_rect)

我得到了同样的错误。

我只是将 copy.deepcopy(EMPTY_IMG) 更改为 EMPTY_IMG。

import copy,pygame,sys
from pygame.locals import *

EMPTY_IMG= pygame.image.load('C:super/fancy/file/path/transparent.png')
held_image=EMPTY_IMG
my_rect=held_image.get_rect()
my_rect.center = (50,50)
screen.blit(held_image,my_rect)

然后一切正常。

于 2012-05-14T14:12:53.880 回答