1

I am a nubie in pygame and python overall. I started a simple helicopter game project yesterday but i cant figure out why i cant display a message.

I tried using different code formats and also tried moving a few lines here and there but still could not get it to work.

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()

    time.sleep(2)

if  x > screen_width - heli_width or x < 0 or y > screen_height - heli_height or y < 0:
    display_gameover()
    game_loop()

I defined display_gameover and called it as shown above. However when i try to run the code, everything works fine, apart from the fact that during the 2 second wait time, nothing is displayed.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I updated my code with the block of code you provided but it still dosent work. –  Nov 09 '19 at 22:12
  • Some other part of my code might be causing this(which i doubt it is) but i dont think its possible to post my entire code here. –  Nov 09 '19 at 22:16
  • Yes i am. But here's that line of code anyways:screen = pygame.display.set_mode((screen_width,screen_height)) –  Nov 09 '19 at 22:17
  • They look like this : screen_height = 600 screen_width = 1200 –  Nov 09 '19 at 22:19
  • Do you call `pygame.quit()` somewhere before `display_gameover`? – Rabbid76 Nov 09 '19 at 22:20
  • Just tried using smaller font size(30), still dosent work –  Nov 09 '19 at 22:21
  • YES . HERES THE CODE : for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() –  Nov 09 '19 at 22:22
  • Just tried removing pygame.quit() and quit() , same result , didnt work –  Nov 09 '19 at 22:25
  • Call `pygyme.init()` respectively `screen = pygame.display.set_mode(...)` in `display_gameover` – Rabbid76 Nov 09 '19 at 22:27
  • Screen goes black for 2 seconds (instead of text appearing), then game_loop() continues. –  Nov 09 '19 at 22:32