Pygame screen freezes when I close it

前端 未结 4 1633
Happy的楠姐
Happy的楠姐 2021-01-21 01:19

The code loads up a pygame screen window, but when I click the X to close it, it becomes unresponsive. I\'m running on a 64-bit system, using a 32-bit python and 32-bit pygame.<

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 01:40

    I'd recommend the following code. First, it includes Clock so your program doesn't eat the CPU doing nothing but polling for events. Second, it calls pygame.quit() which prevents the program from freezing when running under IDLE on windows.

    # Sample Python/Pygame Programs
    # Simpson College Computer Science
    # http://cs.simpson.edu/?q=python_pygame_examples
    
    import pygame
    
    # Define some colors
    black    = (   0,   0,   0)
    white    = ( 255, 255, 255)
    green    = (   0, 255,   0)
    red      = ( 255,   0,   0)
    
    pygame.init()
    
    # Set the height and width of the screen
    size=[700,500]
    screen=pygame.display.set_mode(size)
    
    pygame.display.set_caption("My Game")
    
    #Loop until the user clicks the close button.
    done=False
    
    # Used to manage how fast the screen updates
    clock=pygame.time.Clock()
    
    # -------- Main Program Loop -----------
    while done==False:
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                done=True # Flag that we are done so we exit this loop
    
        # Set the screen background
        screen.fill(black)
    
        # Limit to 20 frames per second
        clock.tick(20)
    
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
    
    # Be IDLE friendly. If you forget this line, the program will 'hang'
    # on exit.
    pygame.quit ()
    

提交回复
热议问题