Pygame - 常量模組



該模組包含在 Pygame 應用程式中頻繁使用的各種常量的定義。儘管這些常量在各自的模組中定義,但透過常量模組使用它們會變得更容易。

例如,按鍵或滑鼠事件(如 KEYDOWN 或 MOUSEBUTTONDOWN)被定義為 pygame.key.KEYDOWN 或 pygame.mouse.MOUSEBUTTON,透過匯入常量模組,可以在不限定模組名稱的情況下使用這些常量。

此處,我們正在使用常量模組中的 QUIT 事件。

import pygame,sys
from pygame.locals import *
pygame.init()
canvas=pygame.display.set_mode((400,300))
pygame.display.set_caption("Hello")
canvas.fill((0,0,0))
while True:
   for event in pygame.event.get():
      if(event.type == QUIT):
         pygame.quit()
            sys.exit(1)
廣告