简体   繁体   中英

Pygame not exiting on keypress

I've been following along with a bouncing ball example just to get my chops warmed up with pygame: every time I test my code I have to kill the game window by causing it to freeze, though in my code (taken directly from the pygame website) I state that the game should exit if the Escape key is pressed or the X button on the screen. I get an error

    running == False
NameError: name 'running' is not defined

my code is

   while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running == False
        if event.type ==pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running == False

can I define "running?" such that the game doesn't simply freeze when I try to quit.

First off, you should define running to True ( running = True ) above your while loop. Secondly, you should be checking that value somewhere; easiest is to change while 1 to while running Third, == is checking for equality, = is setting a value. You want to check event.type to be pygame.QUIT or KEYDOWN, so those == are correct, but then you want to set running to False, which is running = False . Doing running == False as a statement is not effective.

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