简体   繁体   English

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

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

Can anyone tell me why my app quits with:谁能告诉我为什么我的应用程序退出:

pygame error: display Surface quit. pygame 错误:显示 Surface 退出。

I had similar problem and discovered that Surface objects don't like to be deepcopied.我有类似的问题,发现 Surface 对象不喜欢被深度复制。 When I used copy.deepcopy() on such object and then accessed the copy, I got that strange error message (without calling pygame.quit()).当我在这样的对象上使用 copy.deepcopy() 然后访问副本时,我收到了那个奇怪的错误消息(没有调用 pygame.quit())。 Maybe you experience similar behavior?也许你遇到过类似的行为?

I had a similar problem in a very simple piece of code:我在一段非常简单的代码中遇到了类似的问题:

    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)

Error message was:错误消息是:

    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

So I put所以我把

    import pdb

at the top after在顶部之后

    pygame.init()

and used并使用

    pdb.set_trace()

after the line with在行之后

    pygame.quit()

Now I ran the program, clicked to close the window and was actually a bit surprised to see that I fell into the debugger (after all, I expected the quit to completely take me out immediately).现在我运行程序,点击关闭窗口,看到我掉进调试器实际上有点惊讶(毕竟,我预计退出会立即将我完全带出)。 So I concluded that the quit doesn't actually stop everything at that point.所以我得出结论,退出实际上并没有在那时停止一切。 Looked like the program was continuing beyond the quit, was reaching看起来程序在退出之后还在继续,正在到达

    screen.fill(black)

and this was causing the problem.这导致了问题。 So I added所以我加了

    break

after the之后

    pygame.quit()

and all works happily now.现在一切都很愉快。

[ Added later: It occurs to me now that [后来补充:我现在想到的是

    pygame.quit()

is quitting the module , and not the program that is running , so you need the break to get out of this simple program.正在退出模块,而不是正在运行程序,因此您需要休息一下才能退出这个简单的程序。 ] ]

Just for the record, this means the good version is只是为了记录,这意味着好的版本是

    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()

call sys.exit() after pygame.quit() to stop the program so you can not change the surface after you have quit pygame and not get the error在 pygame.quit() 之后调用 sys.exit() 来停止程序,这样你就不能在退出 pygame 后改变表面并且不会得到错误

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

I had this problem too, but got it from another origin.我也有这个问题,但它是从另一个来源得到的。

I had a class defined like this:我有一个这样定义的类:

class PauseMenu(pygame.Surface)

i got the error when forgetting to initialize the pygame.Surface and trying to blit it, making pygame.screen crash and gave me this error.我在忘记初始化 pygame.Surface 并尝试对其进行 blit 时出现错误,使 pygame.screen 崩溃并给了我这个错误。

so i just added this obviously所以我只是明显地添加了这个

pygame.Surface.__init__(self, (width, height))

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

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

pygame.error: display Surface quit what does that mean?, i can't fix it pygame.error: display Surface quit 这意味着什么?,我无法修复

This means a call was made to pygame.display.quit() or pygame.quit().这意味着调用了 pygame.display.quit() 或 pygame.quit()。 If you try to do anything to the display surface after quit you will get this error.如果您在退出后尝试对显示表面执行任何操作,您将收到此错误。

I had the similar problem just right now and I have found a solution to it.我现在遇到了类似的问题,我已经找到了解决方案。

Because it seems like pygame.quit() will just quit the pygame module and not the entire program, use sys.exit() method on the following line.因为看起来pygame.quit()只会退出 pygame 模块而不是整个程序,所以在下一行使用sys.exit()方法。

After this:在这之后:

pygame.quit()

Place this:放置这个:

sys.exit()

Complete snippet:完整片段:

pygame.quit()
sys.exit()

I too had this problem, and similar to Maciej Miąsik's answer mine had to do with copy.deepcopy()-ing an image.我也有这个问题,类似于 Maciej Miąsik 的回答,我的答案与 copy.deepcopy() 图像有关。

I had:我有:

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)

And I got the same error.我得到了同样的错误。

I simply changed the copy.deepcopy(EMPTY_IMG) to just EMPTY_IMG.我只是将 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)

Then it all worked fine.然后一切正常。

Just update the display in the infinite loop.只需在无限循环中更新显示。

Type this:输入这个:

pygame.display.update() pygame.display.update()

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

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