
import pygame

pygame.init()

# 窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简易Pygame画图板")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 220, 0)
BLUE = (0, 80, 255)

# 画布（底层画面）
canvas = pygame.Surface((WIDTH, HEIGHT - 80))
canvas.fill(WHITE)

# 字体
font = pygame.font.SysFont("simhei", 24)

# 变量
drawing = False
last_pos = None
current_color = BLACK
brush_size = 5

# 按钮区域
btn_clear = pygame.Rect(10, HEIGHT - 70, 100, 60)
btn_black = pygame.Rect(130, HEIGHT - 70, 50, 60)
btn_red = pygame.Rect(190, HEIGHT - 70, 50, 60)
btn_green = pygame.Rect(250, HEIGHT - 70, 50, 60)
btn_blue = pygame.Rect(310, HEIGHT - 70, 50, 60)

# 粗细加减按钮
btn_plus = pygame.Rect(400, HEIGHT - 70, 50, 60)
btn_minus = pygame.Rect(460, HEIGHT - 70, 50, 60)

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

while running:
    screen.fill((40, 40, 40))
    # 贴画布
    screen.blit(canvas, (0, 0))

    # 事件循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 鼠标按下
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = event.pos
            if event.button == 1:
                # 判断按钮点击
                if btn_clear.collidepoint(pos):
                    canvas.fill(WHITE)
                elif btn_black.collidepoint(pos):
                    current_color = BLACK
                elif btn_red.collidepoint(pos):
                    current_color = RED
                elif btn_green.collidepoint(pos):
                    current_color = GREEN
                elif btn_blue.collidepoint(pos):
                    current_color = BLUE
                elif btn_plus.collidepoint(pos):
                    brush_size += 2
                elif btn_minus.collidepoint(pos):
                    if brush_size > 1:
                        brush_size -= 2
                else:
                    # 在画布区域开始画画
                    drawing = True
                    last_pos = pos

        # 鼠标松开
        if event.type == pygame.MOUSEBUTTONUP:
            drawing = False
            last_pos = None

        # 鼠标拖动
        if event.type == pygame.MOUSEMOTION:
            if drawing and last_pos:
                # 在画布上画线
                pygame.draw.line(canvas, current_color, last_pos, event.pos, brush_size)
                last_pos = event.pos

    # ========== 绘制底部控制栏按钮 ==========
    # 清空按钮
    pygame.draw.rect(screen, (80, 80, 80), btn_clear)
    text_clear = font.render("清空", True, WHITE)
    screen.blit(text_clear, text_clear.get_rect(center=btn_clear.center))

    # 颜色方块
    pygame.draw.rect(screen, BLACK, btn_black)
    pygame.draw.rect(screen, RED, btn_red)
    pygame.draw.rect(screen, GREEN, btn_green)
    pygame.draw.rect(screen, BLUE, btn_blue)

    # 粗细按钮
    pygame.draw.rect(screen, (60,60,60), btn_plus)
    pygame.draw.rect(screen, (60,60,60), btn_minus)
    text_plus = font.render("+", True, WHITE)
    text_minus = font.render("-", True, WHITE)
    screen.blit(text_plus, text_plus.get_rect(center=btn_plus.center))
    screen.blit(text_minus, text_minus.get_rect(center=btn_minus.center))

    # 显示当前画笔大小
    size_text = font.render(f"粗细:{brush_size}", True, WHITE)
    screen.blit(size_text, (530, HEIGHT - 50))

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

pygame.quit()
