1

I started learning pygame, wrote a simple program to display some text on the screen.

import pygame, time

pygame.init() 
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
window.blit(label, (100, 100))
pygame.display.update()
time.sleep(15)
pygame.quit()

But it keeps crashing. I am using python2.7

pygame crashing without display on mac

nitte93
  • 1,820
  • 3
  • 26
  • 42
  • Are you getting any errors? Please provide more details as it is hard to diagnose your problem with the details that you have given. – Micheal O'Dwyer Jan 20 '18 at 10:01
  • Thanks for responding. No, I am not getting any errors, I've updated the screenshot with the run window. I am sorry if I'm not clear with the question. The problem is when I run the above program the pygame window gets stuck in the forever loading state untill the sleep time as stated in the program. With no errors. Let me know if you need anything else to understand the question. – nitte93 Jan 20 '18 at 10:09

1 Answers1

4

The issue is that you are running the code only once and not repeating the lines of code that need to be repeated for every frame.

Then you are calling pygame.quit() without exiting the Python thread with quit() which results in the windows just "crashing" or not responding.

To fix this problem:

  • Include some code inside a while loop that will run on every frame and thus keep the program running and responding.

  • Make sure that initialization code is only ran once.

  • Add in some event handling to let the user exit the program when the "X" button is clicked.

Some useful additions:

  • Included a Clock which allows for an FPS-cap.

  • Filled the screen with black every frame

  • Exited the game properly with pygame.quit() to exit the pygame window and sys.exit() to exit the Python thread.

A Clock in pygame game allows you to specify an FPS. At the end of every main game loop iteration (frame) you call clock.tick(FPS) to wait the amount of time that will ensure the game is running at the specified framerate.

Here is the revised code example:

import pygame
import sys

# this code only needs to be ran once
pygame.init() 
window = pygame.display.set_mode((600,300))
myfont = pygame.font.SysFont("Arial", 60)
label = myfont.render("Hello Pygame!", 1, (255, 255, 0))
clock = pygame.time.Clock()
FPS = 30

while True:
    #allows user to exit the screen
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
           pygame.quit()
           sys.exit()

    # this code should be ran every frame
    window.fill((0, 0, 0))
    window.blit(label, (100, 100))
    pygame.display.update()
    clock.tick(FPS)

I hope that this answer has helped you and if you have any further questions please feel free to post a comment below!

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
  • Probably you are right. It still become unresponsive as soon as I click on it. – nitte93 Jan 20 '18 at 11:01
  • 2
    It's the call to one of the functions in the event modules you need to call regularly (https://stackoverflow.com/a/42719689/6486738), in order for the program to not crash or become unresponsive (https://stackoverflow.com/questions/44254458/pygame-needs-for-event-in-pygame-event-get-in-order-not-to-crash/44256668#44256668). – Ted Klein Bergman Jan 20 '18 at 11:11
  • 2
    The problem is `time.sleep`. You should never use that function in pygame! It halts the program and doesn't let pygame handle internal logic needed to communicate with the OS that the game is running correctly. Instead, use pygame's Clock object and its tick method ([docs](http://www.pygame.org/docs/ref/time.html#pygame.time.Clock)) to limit the framerate. – Ted Klein Bergman Jan 20 '18 at 11:21
  • Yes, you are right! I forgot to take that troublesome line out – Micheal O'Dwyer Jan 20 '18 at 11:44
  • 1
    I'd also add a clock to show the questioner how to cap the frame rate, and replace [`quit()` by `sys.exit()`](https://stackoverflow.com/a/19747562/6220679) or just a flag to leave the while loop. The `time` import can be removed as well. – skrx Jan 20 '18 at 12:25
  • And fill the `window` each frame, otherwise the text will look rather ugly. – skrx Jan 20 '18 at 12:31
  • 1
    Thanks for the suggestions @skrx! I just edited my post to add them in. – Micheal O'Dwyer Jan 21 '18 at 15:19