"""A tiny paint program: left button draws, right button erases, wheel changes the brush.""" import pygame WIDTH, HEIGHT = 640, 480 COLORS = [(21, 35, 63), (232, 93, 82), (91, 147, 230), (110, 200, 120), (244, 197, 49)] pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() font = pygame.font.Font(None, 24) canvas = pygame.Surface((WIDTH, HEIGHT - 40)) canvas.fill("white") brush = 8 color_index = 0 last_pos = None palette = [pygame.Rect(10 + i * 34, HEIGHT - 32, 26, 26) for i in range(len(COLORS))] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: for i, swatch in enumerate(palette): if swatch.collidepoint(event.pos): # clicked a colour swatch color_index = i elif event.type == pygame.MOUSEWHEEL: brush = max(2, min(40, brush + event.y * 2)) # event.y is +1 or -1 elif event.type == pygame.MOUSEBUTTONUP: last_pos = None buttons = pygame.mouse.get_pressed() # (left, middle, right) mx, my = pygame.mouse.get_pos() if my < HEIGHT - 40 and (buttons[0] or buttons[2]): color = COLORS[color_index] if buttons[0] else (255, 255, 255) if last_pos: pygame.draw.line(canvas, color, last_pos, (mx, my), brush * 2) pygame.draw.circle(canvas, color, (mx, my), brush) last_pos = (mx, my) else: last_pos = None screen.blit(canvas, (0, 0)) pygame.draw.rect(screen, (230, 234, 240), (0, HEIGHT - 40, WIDTH, 40)) for i, swatch in enumerate(palette): pygame.draw.rect(screen, COLORS[i], swatch) if i == color_index: pygame.draw.rect(screen, (0, 0, 0), swatch.inflate(6, 6), 2) screen.blit(font.render(f"Brush {brush}px (mouse wheel) Right button erases", True, (21, 35, 63)), (200, HEIGHT - 28)) if my < HEIGHT - 40: pygame.draw.circle(screen, (120, 120, 120), (mx, my), brush, 1) # brush outline pygame.display.flip() clock.tick(120) pygame.quit()