search

Home  >  Q&A  >  body text

python - pygame退出不了

这断代码按下esc键后退出不了
import pygame
from pygame.locals import *
screen = pygame.display.set_mode((600,500))
pygame.init()
while True:

keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
    print 1
    exit()
    
    

这段代码按下后又可以退出...:
import pygame
from pygame.locals import *
screen = pygame.display.set_mode((600,500))
pygame.init()
while True:

pygame.event.get()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
    print 1
    exit()

就是多了一句 pygame.event.get()....
真心没弄懂

PHP中文网PHP中文网2804 days ago873

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-17 16:11:59

    To exit pygame, call pygame.quit()

    pygame generally uses the following code to handle keyboard events:

    import pygame as py
    py.init()
    for event in py.event.get():
        if event.type== py.QUIT:
            sys.exit()
        elif event.type==py.KEYDOWN:
            if event.key==py.K_ESCAPE:
                print "exit"
                sys.exit()
                py.quit()
            elif event.key==py.K_UP:
                print "up"
            elif event.key==py.K_LEFT:
                print "left"
            elif event.key==py.K_DOWN:
                print "down"
            elif event.key==py.K_RIGHT:
                print "right"

    reply
    0
  • Cancelreply