1

I was trying to use pygame to create a script that upon clicking run. The window changes the colours of the screen to blue, grey, red with one second delays between them, and then exit out of that loop and then run the game as per normal being the print("cycle done") code. Unfortunately what happens is that the window opens, hangs for around 3 seconds and then shows a red screen, rather than going through each of the colours.

import pygame as pg

running = True
calibration = False
pg.init()
screen = pg.display.set_mode((600, 400))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
timer = 0

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False

    if not calibration:
        pg.time.wait(1000)
        screen.fill(blue)
        pg.display.flip()

        pg.time.wait(1000)
        screen.fill(green)
        pg.display.flip()

        pg.time.wait(1000)
        screen.fill(red)
        pg.display.flip()

        calibration = True
        print(calibration)

    print("cycle done")
    clock.tick(60)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Hmm, works fine on my machine. The expected output is blue for 1 second, green for 1 second and then remain red? – Peter May 08 '20 at 17:12
  • Yes that exactly what I want to happen, Im using a Mac, may I know what you're running it on? – Sarthak Das May 08 '20 at 17:18
  • I'm running it on windows 10 and using pygame 1.9.6 – Peter May 08 '20 at 17:20
  • Im using the same version of pygame. Know what could be causing the issue then? – Sarthak Das May 08 '20 at 17:22
  • Python 3.7.7, pygame 2.0.0.dev6, win10 works fine as well. Which program are you using to run the code? – kaktus_car May 08 '20 at 17:58
  • I’m using pycharm, on the masters release on MacOS – Sarthak Das May 08 '20 at 18:00
  • Try it using IDLE or sublime. Also try with `import time` -> `time.sleep(1)`. I'm afraid I can't help any more, I have no experience with Mac – kaktus_car May 08 '20 at 18:04
  • Yeah I’ve tried that that didn’t work either – Sarthak Das May 08 '20 at 18:11
  • Python 3.7.5, works fine on my Kubuntu distro. – TechPerson May 08 '20 at 18:23
  • It's possible that Mac expects the event loop to be checked every time the window updates. In that case, try adjusting the program to run your event loop between color cycles. – TechPerson May 08 '20 at 18:24
  • 1
    Something like this might work: [...] `if not calibration:` `pg.time.wait(1000)` `screen.fill(blue)` `pg.display.flip()` `pg.event.pump()` `pg.time.wait(1000)` `screen.fill(green)` `pg.display.flip()` `pg.event.pump()` `pg.time.wait(1000)` `screen.fill(red)` `pg.display.flip()` `pg.event.pump()` `calibration = True` `print(calibration)` – TechPerson May 08 '20 at 18:35
  • Hi yes that worked thank you so much you might want to post that as an answer since it works. Thank you so much – Sarthak Das May 08 '20 at 18:55

1 Answers1

1

If you just wait for some time, you can use pygame.time.wait or pygame.time.delay. However, if you want to display a message and then wait some time, you need to update the display beforehand. 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.

This all means that you have to call pygame.display.flip() and pygame.event.pump() before pygame.time.wait():

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False

    if not calibration:
        
        pygame.event.pump()
        pg.time.wait(1000)
        
        screen.fill(blue)

        pg.display.flip()
        pygame.event.pump()
        pg.time.wait(1000)

        screen.fill(green)
        
        pg.display.flip()
        pygame.event.pump()
        pg.time.wait(1000)

        screen.fill(red)
        
        pg.display.flip()
        pygame.event.pump()
        pg.time.wait(1000)

        calibration = True
        print(calibration)

    print("cycle done")
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174