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

2024-06-16 11:00:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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's the result

这里的打印(呆子)是工作,但当我点击退出按钮的窗口,它没有退出在所有。。 那么如何在1 while循环中打印事件并退出应用程序呢?你知道吗


Tags: importeventinitdisplayredwidthpygamequit
2条回答

您需要循环检查每个pygame事件,并检查该事件是否为退出。你知道吗

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

首先,应该在while not excape循环中更新屏幕。 其次,将excape设置为Trueifpygame.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()

相关问题 更多 >