什么是 Pygame?
Pygame 是一个用于游戏开发的 Python 库。
构建 Pygame 项目模板
pygame_template.py
# Pygame 模板
import pygame
import random
WIDTH = 360
HEIGHT = 480
FPS = 30
# 定义RGB 颜色常量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 初始Pygame及窗口
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
# 主循环
running = True
while running:
    # 设置刷新频率
    clock.tick(FPS)
    # 输入侦测
    for event in pygame.event.get():
        # 检查窗口关闭操作
        if event.type == pygame.QUIT:
            running = False
    # 更新内容
    # 填充背景色
    screen.fill(BLACK)
    # 刷新
    pygame.display.flip()
pygame.quit()