猫史档案馆


【Python作品分享】画图

用户:温和的阿米巴49Da温和的阿米巴49Da查看:0 回复:0 评论:0 创建时间:2023-07-07T09:31:44


【作品展示】

center_image

 

【作品介绍】

就画一下就行

 

【作品源代码】

import pygame

# 初始化pygame
pygame.init()

# 设置画布大小
width = 800
height = 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("画图程序")

# 设置画笔颜色和大小
colors = [(255, 255, 255), (255, 0, 0), (255, 155, 0), (255, 255, 0), (0, 255, 0),
          (0, 255, 255), (0, 0, 255), (255, 0, 255)]  # 颜色列表
erase_color = (0, 0, 0)
radius = 5

# 设置绘图状态
drawing = False
erasing = False
current_color = 0

# 设置颜色显示区域
color_display_rect = pygame.Rect(10, 10, 100, 30)

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 监听鼠标事件
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:  # 左键按下开始绘制
                drawing = True
            elif event.button == 3:  # 右键按下开始擦除
                erasing = True
        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:  # 左键释放停止绘制
                drawing = False
            elif event.button == 3:  # 右键释放停止擦除
                erasing = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:  # 按下空格键切换颜色
                current_color = (current_color + 1) % len(colors)
            elif event.key == pygame.K_c:  # 按下C键清除画布
                window.fill(erase_color)

    # 绘制图形
    if drawing and not erasing:
        pos = pygame.mouse.get_pos()  # 获取鼠标位置
        pygame.draw.circle(window, colors[current_color], pos, radius)
    elif erasing and not drawing:
        pos = pygame.mouse.get_pos()  # 获取鼠标位置
        pygame.draw.circle(window, erase_color, pos, radius)

    # 绘制颜色显示区域
    pygame.draw.rect(window, colors[current_color], color_display_rect)

    # 刷新窗口
    pygame.display.update()

pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页