import pygame

# 初始化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
line_width = 5
last_pos = None  # 记录上一个鼠标坐标

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

while running:
    clock.tick(60)
    for event in pygame.event.get():
        # 关闭窗口退出
        if event.type == pygame.QUIT:
            running = False

        # 键盘按键功能
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_c:   # C键清空画布
                screen.fill(WHITE)
            if event.key == pygame.K_1:   # 1黑色
                current_color = BLACK
            if event.key == pygame.K_2:   # 2红色
                current_color = RED
            if event.key == pygame.K_3:   # 3绿色
                current_color = GREEN
            if event.key == pygame.K_4:   # 4蓝色
                current_color = BLUE
            if event.key == pygame.K_UP:  # 上箭头加粗
                line_width += 2
            if event.key == pygame.K_DOWN:# 下箭头变细
                line_width = max(1, line_width - 2)

    # 鼠标按下拖拽画画
    mouse_press = pygame.mouse.get_pressed()
    if mouse_press[0]:  # 左键按住
        cur_pos = pygame.mouse.get_pos()
        if last_pos:
            pygame.draw.line(screen, current_color, last_pos, cur_pos, line_width)
        last_pos = cur_pos
    else:
        last_pos = None

    pygame.display.flip()

pygame.quit()