Can anyone tell me why my app quits with:
pygame error: display Surface quit.
I had similar problem and discovered that Surface objects don't like to be deepcopied. When I used copy.deepcopy() on such object and then accessed the copy, I got that strange error message (without calling 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)
Replace if event.type == pygame.quit(): by if event.type == pygame.QUIT:
Make sure if you write pygame.QUIT: and not pygame.quit():
I know it sounds weird, but I had the same problem.
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
From http://archives.seul.org/pygame/users/Nov-2006/msg00236.html :
On Thu, 2006-11-30 at 21:27 -0300, Nicolas Bischof wrote:
pygame.error: display Surface quit
what does that mean?, i can't fix it
This means a call was made to pygame.display.quit() or pygame.quit(). If
you try to do anything to the display surface after quit you will get
this error.
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.
so i just added this obviously
pygame.Surface.__init__(self, (width, height))
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.
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.
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.
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()
Related
I am trying to move my sprite across the screen when the window starts.... It moves for a second than stops for a while than it starts back up again. its not giving me an error.... So im really not sure whats going on here.... anyway here is the code.... any info is needed!
thanks,
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
sky_surface = pygame.image.load("bg_desert.png")
snail_surface = pygame.image.load("snailWalk1.png")
snail_x_pos = 600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
snail_x_pos -=1
screen.blit(sky_surface,(0,0))
screen.blit(snail_surface,(snail_x_pos,350))
pygame.display.update()
Your loop will only execute when there are events. When there are no events, you don't move. If you wiggle your mouse in front of the screen, for example, you'll see pretty continuous movement.
You need to use pygame.time.set_timer to force an event for you to update.
It looks like the final 4 lines you have written are designed to run on each cycle of the for loop, however they will only run when an event occurs, because they are indented a block too far.
Your new code should look like this:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
sky_surface = pygame.image.load("bg_desert.png")
snail_surface = pygame.image.load("snailWalk1.png")
snail_x_pos = 600
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
snail_x_pos -=1
screen.blit(sky_surface,(0,0))
screen.blit(snail_surface,(snail_x_pos,350))
pygame.display.update()
I have tried putting the fill color before blit but it won't work.
im using python 3.8.2, windows 10, and pygame 1.9.6.
please help
btw im following this tutorial (im new to pygame)
and here's my code
import pygame
#initialize
pygame.init()
#screen
screen = pygame.display.set_mode((800, 600))
#things
pygame.display.set_caption("space invaders 1.0")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
#player settings
playerimg = pygame.image.load('space-invaders.png')
playerx = 370
playery = 430
#define player
def player():
screen.blit(playerimg, (playerx, playery))
#loop
running = True
while running:
#rgb
screen.fill((10, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT():
running = False
player()
pygame.display.update()
#end process
pygame.quit()
exit()
ill answer any questions
(side note it crashes when I press "x" and exits when I press '-' idk why)
pygame.QUIT is not a function - it's a constant, simply an integer representing a QUIT event. You can't do if event.type == pygame.QUIT(), you need to do if event.type == pygame.QUIT, without parenthesis.
You also don't really need to call pygame.quit() and exit() at the end of the program - both pygame and the program itself will be terminated when the program reaches its end.
Apart from that your program works for me, images are showing. Make sure your image is visible over a black background and if it is and you are still having problems I'll try to help you with them.
I've just started programming in Python. I've been reading the book called "Making Games with Python & Pygame" and it's been very helpful so far, but I cannot find the reason why this simple program freezes. It should display an image and then move it by pressing left or right. It's strange, since it was working properly --I didn't change anything. I have tried everything so I'd really appreciate your help! Thanks in advance!
import pygame, sys
from pygame import *
# VARIABLES
x_res = 400
y_res = 300
DISPLAYSURF = 0
event = 0
person = 0
posx = 50
posy = 50
pygame.init()
DISPLAYSURF = pygame.display.set_mode((x_res, y_res))
pygame.display.set_caption("Test")
person = pygame.image.load("Person.png")
while True: # main game loop
DISPLAYSURF.blit(person, (posx, posy))
if event in pygame.event.get():
if event.type == KEYUP and event.key == K_RIGHT:
posx = posx + 5
DISPLAYSURF.fill((0,0,0))
if event.type == KEYUP and event.key == K_LEFT:
posx = posx - 5
DISPLAYSURF.fill((0,0,0))
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
pygame.display.update()
I'm not sure what you mean by "freezing" in this case, but I guess the problem is because you don't have an image called "Person.png", since when I tried out your code without an image on my file system called "Person.png" it also froze and gave me the error on the IDLE:
Traceback (most recent call last):
File "/Users/me/Desktop/your_program.py", line 17, in <module>
person = pygame.image.load("Person.png")
pygame.error: Couldn't open Person.png
Instead, if I have an image called "Person.png", it works properly.
Try writing "(wilst) true" instead of while? :)
Using Python (and Pygame), I have been creating a short one-screen game and I code each part in a different window. In my Home Screen, when I blit the play button onto the screen, it doesn't appear. I am new to Python and Pygame. This is my code:
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1352,638))
pygame.display.set_caption("Termination: Part 1")
bg = True
playButton = pygame.image.load("Play Button.png")
mouse = pygame.mouse.get_pos()
def playButtonFunction():
if background == pygame.image.load("Home Screen.png"):
background.blit(playButton(533.5,278))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
bg = False
screen.blit(background,(0,0))
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
playButtonFunction()
pygame.display.update()
As Frédéric Hamidi already said in a comment, the line
if background == pygame.image.load("Home Screen.png")
will not work as you probably expect.
You should probabkly pass a flag to that method or don't call that function at all when you don't want to show that playButton image.
Also, the line
background.blit(playButton(533.5,278))
will throw an exception, it should look like
background.blit(playButton, (533, 278))
So, change your code to
...
if bg:
background = pygame.image.load("Intro Screen.png")
else:
background = pygame.image.load("Home Screen.png")
screen.blit(background,(0,0))
if !bg:
background.blit(playButton, (533, 278))
...
Another issue that you load the images from disk every iteration of your game loop (with pygame.image.load). It's enough to load each image once.
I'm trying to run code from a tutorial I found online.
But when I run this code, my background image does not appear, even if it loaded correctly.
Why?
bif = "castle.jpg"
import pygame, sys, pygame.mixer
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1280,960),0, 32)
background = pygame.image.load(bif).convert()
pygame.display.set_caption("castlevania ultimate")
hit_sound = pygame.mixer.Sound("02.wav")
hit = False
if hit is True:
hit_sound.play()
sound = pygame.mixer.Sound("castlevania_1.wav")
sound.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
You never call pygame.display.update() in your while loop.
Python is indentation sensitive, so the loop should look like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
pygame.display.update()
Note that pygame.display.update() is now inside the while loop.