简体   繁体   中英

How do i quit and print the events in one loop here

import pygame

pygame.init()

x = height,width = (800,600)

Display = pygame.display.set_mode(x)


pygame.display.set_caption("Blocky")

red = (157, 139, 215)
black = (0,0,0)

Display.fill(red)

pygame.draw.rect(Display,black,(120,450,600,50))

#It updates every frame

pygame.display.update()

excape = False

while not excape:
    for dork in pygame.event.get():  
      print(dork)
    if pygame.event == pygame.QUIT:
        pygame.quit()
        quit()

这是结果

Here the print(dork) is working but when i click the exit button of the window it doesn't quit at all.. So how do i both print events and quit the application in 1 while loop?

First of all, you should update the screen in the while not excape loop. Secondly, set the excape to True if pygame.event is equal to pygame.QUIT . So your code will look like this:

import pygame, sys

pygame.init()

x = height,width = (800,600)

Display = pygame.display.set_mode(x)


pygame.display.set_caption("Blocky")

red = (157, 139, 215)
black = (0,0,0)

Display.fill(red)

pygame.draw.rect(Display,black,(120,450,600,50))

#It updates every frame


excape = False

while not excape:
    for event in pygame.event.get():  
      print(event)
      if event.type == pygame.QUIT:
          excape = True
          pygame.quit()
          sys.exit()
    pygame.display.update()

You need to cycle through EVERY pygame event and check if the event is a quit.

while not excape:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
            excape = True

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