繁体   English   中英

Pygame 错误:显示表面退出:为什么?

[英]Pygame error: display surface quit: Why?

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

pygame 错误:显示 Surface 退出。

我有类似的问题,发现 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()

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

只是为了记录,这意味着好的版本是

    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(): by if event.type == 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() 之后调用 sys.exit() 来停止程序,这样你就不能在退出 pygame 后改变表面并且不会得到错误

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

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

我有一个这样定义的类:

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: display Surface quit 这意味着什么?,我无法修复

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

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

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

在这之后:

pygame.quit()

放置这个:

sys.exit()

完整片段:

pygame.quit()
sys.exit()

我也有这个问题,类似于 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM