Why Doesn't Pygame Draw in the Window Before the Delay or Sleep

Why doesn't PyGame draw in the window before the delay or sleep?

The display is updated only if either pygame.display.update() or pygame.display.flip()
is called. See pygame.display.flip():

This will update the contents of the entire display.

Further you've to handles the events with pygame.event.pump(), before the update of the display becomes visible in the window.

See pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

If you want to display a text and delay the game, then you've to update the display and handle the events.

Write a function which delays the game and updates the display. I recommend to use the pygame.time module to implement the delay (e.g. pygame.time.delay())

def update_and_wait(delay):
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(delay * 1000) # 1 second == 1000 milliseconds

Or even implement a function which its own event loop to keep the application responding. Measure the time by pygame.time.get_ticks():

def update_and_wait(delay):
start_time = pygame.time.get_ticks()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("auit")
pygame.quit()
return False
if pygame.time.get_ticks() >= start_time + delay * 1000:
break
return True

Use the function in the application:

def main():
# [...]

while not done:
# [...]

for ball in ball_list:
# [...]

if right_score == 0:
message_wait("RIGHT PLAYER HAS WON!!", white, 300, 200, font, game_win)
update_and_wait(5)
quit()
elif left_score == 0:
message_wait("LEFT PLAYER HAS WON!!", white, 300, 200, font, game_win)
update_and_wait(5)
quit()

Why my program doesnt get delayed for 1 second?

Try inserting update and sleep inside your for loop, so your display would update after every iteration:

while running:
#update display
pygame.display.update()

for x in range(10000):
pygame.draw.circle(screen, (returnRandomInt(),returnRandomInt(),returnRandomInt()), (startX, startY), 2, 2)
if startX <= 0:
startX += 1
elif startX >= 800:
startX += -1
else:
startX += returnRandomNumber()

if startY <= 0:
startY += 1
elif startY >= 800:
startY += -1
else:
startY += returnRandomNumber()
pygame.display.update()
time.sleep(1)

How to wait some time in pygame?

For animation / cooldowns, etc: If you want to 'wait', but still have code running you use: pygame.time.get_ticks

class Unit():
def __init__(self):
self.last = pygame.time.get_ticks()
self.cooldown = 300

def fire(self):
# fire gun, only if cooldown has been 0.3 seconds since last
now = pygame.time.get_ticks()
if now - self.last >= self.cooldown:
self.last = now
spawn_bullet()

Why is Pygame waiting for sprites to load before creating the window?

Don't create the meteors before the application loop, create them with a time delay in the loop.

Use pygame.time.get_ticks() to get the current time in milliseconds and set the start time, before the main loop.
Define a time interval after which a new meteorite should appear. When a meteorite spawns, calculate the time when the next meteorite must spawn:

next_meteor_time = 0
meteor_interval = 2000 # 2000 milliseconds == 2 sceonds

while ready:
clock.tick(60) # FPS, Everything happens per frame
for event in pygame.event.get():
# [...]

# [...]

current_time = pygame.time.get_ticks()
if current_time >= next_meteor_time:
next_meteor_time += meteor_interval
new_x = random.randrange(0, display_width)
new_y = random.randrange(0, display_height)
all_meteors.add(Meteor(new_x, new_y))

# [...]

meteors.all_meteors.update()
meteors.all_meteors.draw(screen)
pygame.display.update()

How do you make Pygame stop for a few seconds?

The display is updated only if either pygame.display.update() or pygame.display.flip()
is called. Further you've to handles the events by pygame.event.pump(), before the update of the display becomes visible in the window.

See pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

If you want to display a text and delay the game, then you've to update the display and handle the events.
See Why doesn't PyGame draw in the window before the delay or sleep?.

Anyway I recommend to load the images beforehand and do the animation in a loop:

filenames = ['w00.png', 'e-2.png', 'l-2.png', 'c-2.png', 'o-2.png', 'm-2.png', 'e-2.png']
letters = [pygame.image.load(name) for name in filenames]
for letter in letters:
pygame.event.pump()
screen.blit(letter, (letter_x, 625))
letter_x += 30
pygame.display.flip()
pygame.time.delay(500) # 500 milliseconds = 0.5 seconds

Having trouble displaying a text in pygame

It is not sufficient to call pygame.display.update(), you've to handle the events, too (e.g. by pygame.event.pump()).

Further I recommend to use pygame.time.wait() rather than time.sleep(). Be aware that the time unit for pygame.time.wait() is milliseconds.

def display_gameover():
pygame.font.init()

font = pygame.font.SysFont(None, 100)
text = font.render("GAME OVER", True, red)
extRect = text.get_rect()

screen.blit(text,(screen_height//2, screen_width//2))

pygame.display.update()
pygame.event.pump()
pygame.time.wait(2000) # 2000 milliseconds == 2 seconds

Furthermore you've to ensure that the Surface which is associated to the display is initialized (pygame.display.set_mode()).
This means if pygame was terminated by pygame.quit(), then it has to be reinitialized by pygame.init() and screen has to be set by pygame.display.set_mode() before the call to display_gameover().

Alternatively don't terminate pygame.



Related Topics



Leave a reply



Submit