【发布时间】:2010-01-04 05:49:03
【问题描述】:
谁能告诉我为什么我的应用退出:
pygame 错误:显示 Surface 退出。
【问题讨论】:
-
发布整个回溯,至少是引发异常的函数的代码。
-
真的,没有minimal reproducible example,这个问题应该被关闭。但是,它有一些有价值的答案值得保留。
谁能告诉我为什么我的应用退出:
pygame 错误:显示 Surface 退出。
【问题讨论】:
将if event.type == pygame.quit(): 替换为if event.type == pygame.QUIT:
【讨论】:
我遇到了类似的问题,发现 Surface 对象不喜欢被深度复制。当我在此类对象上使用 copy.deepcopy() 然后访问副本时,我收到了奇怪的错误消息(没有调用 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()
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()
现在我运行程序,点击关闭窗口,看到自己掉进了调试器,其实有点意外(毕竟我本以为退出会立刻把我彻底带出去)。所以我得出的结论是,quit 实际上并没有在那时停止一切。看起来该计划在退出之后仍在继续,正在达到
screen.fill(black)
这是导致问题的原因。 所以我加了
break
之后
pygame.quit()
现在一切正常。
[稍后添加:我现在想到
pygame.quit()
退出的是模块,而不是正在运行的程序,所以你需要break来退出这个简单的程序。 ]
只是为了记录,这意味着好的版本是
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)
【讨论】:
确保你写的是pygame.QUIT:而不是pygame.quit():
我知道这听起来很奇怪,但我遇到了同样的问题。
【讨论】:
我刚刚遇到了类似的问题,我已经找到了解决办法。
因为看起来 pygame.quit() 只会退出 pygame 模块而不是整个程序,所以在下一行使用 sys.exit() 方法。
之后:
pygame.quit()
放置这个:
sys.exit()
完成sn-p:
pygame.quit()
sys.exit()
【讨论】:
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 后就不能改变表面并且不会得到错误
【讨论】:
我也有这个问题,但是从另一个来源得到的。
我有一个这样定义的类:
class PauseMenu(pygame.Surface)
我在忘记初始化 pygame.Surface 并尝试对其进行 blit 时遇到错误,导致 pygame.screen 崩溃并给了我这个错误。
所以我很明显地添加了这个
pygame.Surface.__init__(self, (width, height))
【讨论】:
来自http://archives.seul.org/pygame/users/Nov-2006/msg00236.html:
2006 年 11 月 30 日星期四 21:27 -0300,Nicolas Bischof 写道:
pygame.error: 显示 Surface 退出 这是什么意思?,我无法解决它
这意味着调用了 pygame.display.quit() 或 pygame.quit()。如果 你尝试在退出后对显示表面做任何事情你会得到 这个错误。
【讨论】:
我也遇到了这个问题,与 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)
然后一切正常。
【讨论】: