import pygame

# 初始化
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简易画图板")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 初始参数
screen.fill(WHITE)  # 画布底色白色
current_color = BLACK
brush_size = 5
drawing = False
last_pos = None

clock = pygame.time.Clock()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标按下 开始画
        if event.type == pygame.MOUSEBUTTONDOWN:
            drawing = True
            last_pos = pygame.mouse.get_pos()
        # 鼠标松开 停止画
        if event.type == pygame.MOUSEBUTTONUP:
            drawing = False
            last_pos = None
        # 鼠标移动拖拽画线
        if event.type == pygame.MOUSEMOTION and drawing:
            x, y = pygame.mouse.get_pos()
            pygame.draw.line(screen, current_color, last_pos, (x, y), brush_size)
            last_pos = (x, y)

        # 键盘快捷键
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                current_color = RED
            elif event.key == pygame.K_g:
                current_color = GREEN
            elif event.key == pygame.K_b:
                current_color = BLUE
            elif event.key == pygame.K_k:
                current_color = BLACK
            elif event.key == pygame.K_c:
                # 清空画布
                screen.fill(WHITE)
            elif event.key == pygame.K_UP:
                brush_size += 2
            elif event.key == pygame.K_DOWN:
                brush_size -= 2
                if brush_size < 1:
                    brush_size = 1

    pygame.display.update()
    clock.tick(60)

pygame.quit()
