Pygame.display.update() disappearing [duplicate] - python

This question already has answers here:
Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.
(2 answers)
Closed 7 years ago.
We have recently moved our school lab to Mac, and when I run some code to display an image from a file, the display appears then disappears immediately. I am using the 3.4 interpreter in Pycharm and Pygame version 1.9.2. Can someone please help?
Here is my code:
# displays a hard-coded filename in a window
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0, 0))
pygame.display.update()

If the code you posted is the complete code, then that is the reason why its exiting immediately, because immediately after calling pygame.display.update() , the program exits. I do not think this should have been working before.
One thing you can do for this , add a loop to run till the user presses close, after your code -
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

Put a
time.sleep(10)
after pygame.display.update() see if it stays up longer.

Related

Pygame Window opens then immediately cloeses [duplicate]

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed last year.
I am coming back to pygame after around one and a half months of taking a break.
I tried to open and create a window I tried to run it and the window immediately closed.
This is all of the code:
import pygame
pygame.init()
screen_x = 230
screen_y = 230
display = pygame.display.set_mode ((screen_x, screen_y))
I am using VSCode if that does anything, this is the first time I have used VSCode so if I do not do something right I may not know.
You need an event loop, or else the application will close immediately.
Here is some starter code to do this:
import pygame
# screen object(width,height)
screen_x = 230
screen_y = 230
screen = pygame.display.set_mode((screen_x, screen_y))
# Set the caption of the screen
pygame.display.set_caption('Title')
# Variable to keep our game loop running
running = True
# game loop
while running:
# for loop through the event queue
for event in pygame.event.get():
# Check for QUIT event
if event.type == pygame.QUIT:
running = False

Annoying error: pygame.error: video system not initialized [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
I've recently started using Pygame but I came across this error and can't seem to find a reason as to why this has happened.
My code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400,500))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.quit():
done = True
pygame.display.flip()
The error I get:
pygame.error: video system not initialized
You have made a small error write pygame.QUIT instead of pygame.quit(). This causes pygame to quit, so the error makes sense.

Pygame window is not responding after just making it

I wanted to make a game using pygame and I started my code making the pygame window
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
But every time I run the program, my pygame window does not respond.
Does anyone know what is happening, this did happen to me on previous codes too but it wasn't all the time like it is now
you need a game loop to start the game and you need events to work on.
for python code i suggest:
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
start_game = True
while start_game:
print("Game Started!")
and for the events you can use this code in the while loop:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print("mouse button up pressed")
the source of this code is : RipTutorial
I assume that by 'my pygame window does not respond' you mean that you believe the window is not displaying. I think that you are just missing it because it is closing too quickly.
The window does display, but because your program ends right after you create the window, the window is immediately closed again. This is likely just happening so quickly that you are not noticing it. If you want to see this, then just put a call to sleep after opening the window and you will see the window sit there until the sleep expires and then it will close.
If you do not mean that and you are seeing the window but expecting it to respond, then I do not understand what you expect it to respond to. You do not have any code that tries to make it respond to anything.
EDIT:
I just noticed that #AnassABEA suggests that in a comment under his answer, though not in his answer. #AnassABEA you should update your answer to include this since I believe that is the actual answer to his question, not the missing event loop.
Add this below to your code:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Lines 4 to 5 of the code are for closing pygame when the window is closed.

Pygame : The game stops temporarily when I am moving the window

I'm a new in PyGame and I was making a game. Until I discovered that when you were moving the window to another place of your desktop for example, the game temporarily stops. But ticks of the game still running when I used pygame.time.get_ticks().
So I made a completely new program with only the necessary code and it does the same thing.
Can someone explain me why it does it and if we can resolve this problem?
import pygame
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('The Test Program')
running = True
update_counter = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
print(str(update_counter) + " updates")
update_counter += 1
pygame.quit()
quit()
# So try to move the window across your screen and you will see that, prints will stop and they will resume when you will release the click
So someone tell me that was normal, so I think it's only on Windows but there are no solutions. I put a script that show a pause symbol on screen when the cursor leave the window to make it normal.

Python pygame window keeps crashing

Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)
1 - Import library
import pygame
from pygame.locals import *
2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
3 - Load Images
player = pygame.images.load("resources/images/dude.png")
4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
screen.blit(player, (100,100))
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
Don't import pygame.locals. It is actually unnecessary, since you are already importing pygame.
Also, as #furas said, it should be:
player = pygame.image.load("resources/images/dude.png")
Not:
player = pygame.images.load("resources/images/dude.png")
This will clear up some of the problems in your code.
From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me.
And as furas said use
player = pygame.image.load("resources/images/dude.png")
instead of
player = pygame.images.load("resources/images/dude.png")
Replace that for loop with this
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

Categories

Resources