我遇到了类似的问题,发现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()
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)
将if event.type == pygame.quit():
替换为if event.type == pygame.QUIT:
确保你写的是pygame.QUIT:
而不是pygame.quit():
我知道这听起来很奇怪,但我遇到了同样的问题。
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() 只会退出 pygame 模块而不是整个程序,因此请在下一行使用 sys.exit() 方法。
完成后:
pygame.quit()
Place this:
sys.exit()
完整代码片段:
pygame.quit()
sys.exit()
我也遇到了这个问题,但是我从另一个来源得到了它。
我定义了一个类,像这样:
class PauseMenu(pygame.Surface)
pygame.Surface.__init__(self, (width, height))
来自http://archives.seul.org/pygame/users/Nov-2006/msg00236.html :
在2006年11月30日星期四,Nicolas Bischof写道:
pygame.error: 显示界面已退出 这是什么意思?我无法解决
这意味着调用了pygame.display.quit()或pygame.quit()。 如果你在退出后尝试对显示屏面板做任何事情,就会得到这个错误。
我也遇到了这个问题,和Maciej Miąsik的答案类似,我的问题与copy.deepcopy()复制图像有关。
我有:
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)
然后一切都正常工作了。
只需在无限循环中更新显示。
输入以下内容:
pygame.display.update()