1

I am trying to create a 5 second long display message for every time the user presses "Return". But the program just pauses for 5 seconds and displays the message for 1 millisecond. This is just a rough representation of the type of code i tried to use.

while running:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys=pygame.key.get_pressed()
    win.fill((255,255,255))
    if keys[pygame.K_RETURN]:
        text = font.render("You pressed Return", True,(0,0,0))
        win.blit(text, (350,350))
    pygame.display.update()
    if keys[pygame.K_RETURN]:    
        pygame.time.delay(5000)
fealty025
  • 13
  • 3

2 Answers2

0

You to handle the events by either pygame.event.pump() or pygame.event.get() after pygame.display.update(). On some systems the update of the display is finished during the internal event handling. e.g:

while running:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    keys=pygame.key.get_pressed()
    win.fill((255,255,255))
    if keys[pygame.K_RETURN]:
        text = font.render("You pressed Return", True,(0,0,0))
        win.blit(text, (350,350))
    pygame.display.update()
    if keys[pygame.K_RETURN]:
        pygame.event.pump() # <----
        pygame.time.delay(5000)

Anyway I won't do it like that, because the system is not responding, when the application is delayed.
Use pygame.time.get_ticks() to draw the text for a time span.

Get the current time in milliseconds:

current_time = pygame.time.get_ticks()

When RETURN is pressed, then compute the time when the text has to disapear:

if keys[pygame.K_RETURN]:
    pause_time = current_time + 5000 

Draw the text as long text_time is grater than current_time:

e.g.:

running = True
text_time = 0
while running:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    current_time = pygame.time.get_ticks()
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RETURN]:
        text_time = current_time + 5000

    win.fill((255,255,255))
    if text_time > current_time:
        text = font.render("You pressed Return", True,(0,0,0))
        win.blit(text, (350,350))
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

You could try using time and waiting for 5 seconds. Also i wouldn't recommend using pygame.time.delay(100) every frame. You can use clock

import time

clock = pygame.time.Clock()
Show_Text = False
Start_show_text = 0

while running:
    clock.tick(60) #this limits the fps to 60
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys=pygame.key.get_pressed()
    win.fill((255,255,255))

    if keys[pygame.K_RETURN]:
        Show_Text = True
        Start_show_text = time.time()

    if Show_Text:
        if time.time() - Start_show_text < 5: #if its been less than 5 seconds
            text = font.render("You pressed Return", True,(0,0,0))
            win.blit(text, (350,350))
        else:
            Show_Text = False

    pygame.display.update()
The Big Kahuna
  • 2,097
  • 1
  • 6
  • 19