简体   繁体   English

我如何在这里退出并在一个循环中打印事件

[英]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? 这里的print(dork)工作正常,但是当我单击窗口的退出按钮时,它根本不会退出。那么,我如何同时在1 while循环中同时打印事件和退出应用程序呢?

First of all, you should update the screen in the while not excape loop. 首先,您应该在while not exape循环中更新屏幕。 Secondly, set the excape to True if pygame.event is equal to pygame.QUIT . 其次,设置excape至True ,如果pygame.event等于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. 您需要循环浏览每个pygame事件,并检查该事件是否已退出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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