Pygame, screen only updates when exiting pygame window? - python

I'm fairly new to pygame and i needed some help because my code is not working properly.
Okay so here's the problem: I want the screen to turn white when i run it but it remains black, however when i press the exit, it turns white for about a second and then closes.
This also happens when i put a picture (like the player.png) it appears for about a second before exiting. I don't know what i'm doing wrong,
please help fix the code and explain why it is happening?
here is the code:
import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
image = pygame.image.load('player.png')
gameExit = False
gameLoop = True
pygame.display.update()
white = (255,255,255)
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
pygame.quit()
quit()
PS. I don't get any errors

Python is indentation sensitive. In your code, you call pygame.display.update() only once your main loop ends.
Also, you only paint the background white in the case there's an event in the event queue between the two for loops, and then you fill the background for every event in the queue.
Note that this could also lead to a situation in which your QUIT event is "swallowed" by the second loop.
So this
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
should be
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
screen.fill(white)
pygame.display.update()

Ok, so I agree with Sloth, but instead of doing all the screen updating and filling and blitting at the end, making the script less readable, you should make like an animate function that runs at the end of the while loop each time, and put the blitting and screen updating stuff in that function. Sorry for the run-on sentence.
Oh and you don't need your GameLoop variable.

Related

Pygame window does not close properly when clock.tick in the loop

I'm working on a project and when I add the clock.tick to my main game loop, my pygame window doesnt close.
def game_loop():
"""The main game loop that runs as the game runs. Returns when the pygame window is closed."""
global running
global timer
while running:
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
return
screen.clock.tick(screen.fps)
timer += delta_time()
pygame.quit()
return
When I click the X, the screen freezes until I let go, but unless I click the X in a very specific time frame( I usually need to click it 20 times to close) it doesnt work.
This is likely because of the way you are handling the QUIT event in your event loop. The pygame.event.get() function returns all of the events that have occurred since the last time it was called, so if there is a delay in your loop, the QUIT event may not be handled until multiple events have accumulated. To fix this, you should move the QUIT event handling code to the top of your event loop so that it is handled as soon as the event occurs. Also you could try to add pygame.display.update() after the event handling to make sure it is updated.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
return
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
screen.clock.tick(screen.fps)
timer += delta_time()

Incorrect background in pygame

I am trying to make a game in pygame. I have Python 3.6 installed in Ubuntu, and this is how my code looks:
import pygame
pygame.init()
# on_screen
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Little UFO')
# actors
ufo = pygame.image.load('ufo.png')
def player():
screen.blit(ufo, (375, 591))
run = True
while run:
screen.fill([255,255,255])
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
run = False
player()
When I run it I see a black background even though I have selected white, and I can't see the ufo.
You need to call pygame.display.flip() after drawing to the screen for changes to become visible.
By default, whenever drawing to your surface it actually draws to a hidden surface. Whenever you are done drawing everything on a frame you flip the front buffers with the back buffer. This way a user can never see a half-drawn screen.
Add pygame.display.update() after the player function is called:
while run:
screen.fill([255,255,255])
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
run = False
player()
pygame.display.update()
Using pygame.display.update() can be more efficient than flip() if you specify which section of the screen to update, more on that here: https://www.pygame.org/docs/ref/display.html#pygame.display.update
If you don't pass any parameters it does the same job as pygame.display.flip()

pygame.quit(), quit(), or run = False?

Lets says I just have a normal game loop using pygame.
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Where run = False is, should I use run = False and pygame.quit() at the end? Should I just put pygame.quit() where run = False is? Should I use quit() without pygame in front of it at all? Thanks.
I recommend to do it exactly as you do it in the question.
pygame.quit() uninitialize all pygame modules. Any further call to a pygame module (except pygame.init()) will cause an exception. To terminate a pygame application correctly, pygame.quit() has to be the called at the end. If you do pygame.quit() in the main application loop, then the application will crash if you do something after it (e.g. pygame.disaply.update()).

pygame window keeps not responding

After a long time trying to install pygame for 2.7 it finally installs and I now have it downloaded, but there is now the problem that it keeps not responding after a couple seconds of being open. Any answer will be appreciated, the code that I have so far is just.
import pygame
pygame.init()
pygame.display.set_mode((640,480))
so I need some help please.
So what you want to do, like what skrx said, is a while loop to continuously keep the code inside the while loop and the pygame window running, and as well as a for event loop, in order to be able to shut down the window. Here's how you can do it:
import pygame
pygame.init()
pygame.display.set_mode((640, 480)) # opens the display
while True: # the while loop that will keep your display up and running!
for event in pygame.event.get(): # the for event loop, keeping track of events,
if event.type == pygame.QUIT: # and in this case, it will be keeping track of pygame.QUIT, which is the X or the top right
pygame.quit() # stops pygame
There are other ways of stopping the while loop, and you could do this:
running = True
while running: # the while loop that will keep your display up and running!
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Hope this helps!

pygame and gnome-shell using TOO much CPU [duplicate]

i am new to pygame and i was wondering what an event loop is and what clock does in this situation, like what is clock.tick(60)? I don't understand any explanations online
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
That meas that the loop:
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
runs 60 times per second.
for event in pygame.event.get() handles the internal events an retrieves a list of external events (the events are removed from the internal event queue).
If you press the close button of the window, than the causes the QUIT event and you'll get the event by for event in pygame.event.get(). See pygame.event for the different event types. e.g. KEYDOWN occurs once when a key is pressed.
e.g. The following loop prints the names of the a key once it it pressed:
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))

Categories

Resources