简体   繁体   中英

Pygame screen freezes when I close it

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.

from livewires import games, color

games.init(screen_width = 640, screen_height = 480, fps = 50)

games.screen.mainloop()

Mach1723's answer is correct, but I would like to suggest another variant of a main loop:

while 1:
    for event in pygame.event.get():
        if event.type == QUIT: ## defined in pygame.locals
            pygame.quit()
            sys.exit()

        if event.type == ## Handle other event types here...

    ## Do other important game loop stuff here.

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

This is a pretty simple issue, you need to handle the "QUIT" event, see the event documentation at: http://www.pygame.org/docs/ref/event.html

EDIT: It occurs to me now that you might be handling the "QUIT" event and its not working but without more details to your code I dunno.

A quick example of a simple way to handle the "QUIT" event:

import sys
import pygame

# Initialize pygame
pygame.init()
pygame.display.set_mode(resolution=(640, 480))

# Simple(ugly) main loop
curEvent = pygame.event.poll()

while curEvent.type != pygame.QUIT:
      # do something
      curEvent = pygame.event.poll()

In using pygame, you have to handle all events including QUIT so if you don't handle the quit event, your program will not quit. Here's a code.

import sys
import pygame
from pygame.locals import *

def main():
    running = True
    while running:
        for event in pygame.event.get():
            if event.type==QUIT: #QUIT is defined at pygame.locals 
                runnning = False
    #other game stuff to be done

if __name__=='__main__':
    pygame.init()
    pygame.display.set_mode((640,480))
    main()

To make a pygame window closable is simple, make a game loop with the function while True: then in this use the function for event in pygame.event.get(): to create a for loop, next add on the code if event.type == QUIT: if you have already created a 'while True' loop you only need to add on the last piece of code. Now add on the pygame.quit() and sys.exit() , here is the finished code:

while True:
for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()
        sys.exit()

Note that you need to have imported pygame and sys first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM