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

这是结果

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

首先,您应该在while not exape循环中更新屏幕。 其次,设置excape至True ,如果pygame.event等于pygame.QUIT 因此,您的代码将如下所示:

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

您需要循环浏览每个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