簡體   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