每日一练:Python代码绘制航海王草帽路飞,打饭阿姨也能跟着学会的Turtle海龟绘图系列

简介: 每日一练:Python代码绘制航海王草帽路飞,打饭阿姨也能跟着学会的Turtle海龟绘图系列

第一章:程序运行

① 效果展示 - 轮廓描绘

看轮廓描绘效果:


de5cff3438174486b674d8d07e55e061.gif

② 效果展示 - 颜色填充

衣服和裤子颜色填充效果:

image.gif

第二章:实现过程

① 绘图数据下载

获取地址:小蓝枣的 csdn 资源仓库

d9d32ecd2c7142459bc3fc6bb24ebcf2.png

内容预览:


3bf9fac9451b46df916310c08a562072.png


② 海龟绘图配置项

降低刷新率可提升绘制速度,值越大刷新频率越低,速度越快

t.tracer(5000)

def set_trutle():
    '''
     作用:海龟绘图配置项
     参数:无
     返回:无
    '''
    # 默认颜色区间是[0,1],切换为[0,255]
    t.Screen().colormode(255)
    # 设置起始大小
    t.setup(width=x, height=y)
    # 调整坐标,
    t.setworldcoordinates(0,y,x,0)
    t.pen()
    # 设置绘制速度,0为最快
    t.speed(0)
    # 禁用延迟提升速度
    t.delay(0)
    # 提升速度,值越大越快
    t.tracer(5000)
    # 设置默认画笔颜色为白色
    t.pencolor((255,255,255))
    # 抬起画笔
    t.penup()

③ 轮廓绘制

通过下落画笔 t.pendown()

和抬起画笔 t.penup()

来避免连线问题。

def draw_lufei_outline():
    '''
     作用:绘制路飞轮廓
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = round(float(j[0]))
            y1 = round(float(j[1]))
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>50):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("轮廓绘制完成")

效果图演示:

335a4fe060aa4d64853bc9f106f555b6.gif

④ 颜色填充:衣服、裤子

绘制衣服、裤子的红色和蓝色。

def draw_lufei_tintage1():
    '''
     作用:路飞颜色填充:衣服、帽子
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = int(j[0])
            y1 = int(j[1])
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>150):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("上色完成")

效果图演示:

image.gif

⑤ 颜色填充:草帽、腰带

绘制草帽、腰带的黄色。

def draw_lufei_tintage2():
    '''
     作用:路飞颜色填充:草帽、腰带
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = int(j[0])
            y1 = int(j[1])
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>215):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("上色完成")

效果图演示:


网络异常,图片无法展示
|

⑥ 完整源码

# -*- coding:utf-8 -*-
# 2022-3-9
# 作者:小蓝枣
# 图像绘制:路飞
import turtle as t
import time
x = 224
y = 345
def set_trutle():
    '''
     作用:海龟绘图配置项
     参数:无
     返回:无
    '''
    # 默认颜色区间是[0,1],切换为[0,255]
    t.Screen().colormode(255)
    # 设置起始大小
    t.setup(width=x, height=y)
    # 调整坐标,
    t.setworldcoordinates(0,y,x,0)
    t.pen()
    # 设置绘制速度,0为最快
    t.speed(0)
    # 禁用延迟提升速度
    t.delay(0)
    # 提升速度,值越大越快
    t.tracer(5000)
    # 设置默认画笔颜色为白色
    t.pencolor((255,255,255))
    # 抬起画笔
    t.penup()
def draw_lufei_outline():
    '''
     作用:绘制路飞轮廓
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = round(float(j[0]))
            y1 = round(float(j[1]))
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>50):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("轮廓绘制完成")
def draw_lufei_tintage1():
    '''
     作用:路飞颜色填充:衣服、帽子
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = int(j[0])
            y1 = int(j[1])
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>150):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("上色完成")
def draw_lufei_tintage2():
    '''
     作用:路飞颜色填充:草帽、腰带
     参数:无
     返回:无
    '''
    # 数据文件读取
    f=open("lufei.txt","r")
    bigmom_date = f.read().split(" ")
    for i in bigmom_date:
        try:
            # 数据分离与转化
            j = i.split("_")
            x1 = int(j[0])
            y1 = int(j[1])
            color = j[2][1:-1].split(",")
            color[0]=int(color[0])
            color[1]=int(color[1])
            color[2]=int(color[2])
            if((color[0]*0.299 + color[1]*0.587 + color[2]*0.114)>215):
                color = (255,255,255);
            # 下落画笔
            t.pendown()
            # 解决图像只绘制一半的问题
            t.sety(y1)
            # 轨迹追踪与绘制
            t.goto(x1, y1)
            t.color(color)
            # 抬起画笔
            t.penup()
        except Exception as e:
            print()
    f.close()
    print("上色完成")
set_trutle()
draw_lufei_outline()
draw_lufei_tintage1()
draw_lufei_tintage2()
time.sleep(10000)

喜欢的点个赞❤吧!

目录
相关文章
|
2天前
|
自然语言处理 数据可视化 数据挖掘
数据代码分享|Python对全球Covid-19疫情失业数据相关性、可视化分析
数据代码分享|Python对全球Covid-19疫情失业数据相关性、可视化分析
|
3天前
|
安全 网络安全 Python
使用 Python 代码实现 ICMP Timestamp 请求和回应
使用 Python 代码实现 ICMP Timestamp 请求和回应
|
3天前
|
数据可视化 API Python
Python绘图工具seaborn,教会你如何绘制更加精美的图形(二)
Python绘图工具seaborn,教会你如何绘制更加精美的图形(二)
|
3天前
|
数据可视化 Linux API
Python绘图工具seaborn,教会你如何绘制更加精美的图形(一)
Python绘图工具seaborn,教会你如何绘制更加精美的图形(一)
|
3天前
|
Linux iOS开发 MacOS
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
|
3天前
|
机器学习/深度学习 数据可视化 数据挖掘
Python绘图工具Matplotlib安装与使用,快速上手
Python绘图工具Matplotlib安装与使用,快速上手
|
存储 JavaScript IDE
PyHubWeekly | 第十一期:一款开挂的Python绘图工具
PyHubWeekly每周定期更新,精选GitHub上优质的Python项目/小工具。 我把PyHubWeekly托管到了Github,感兴趣的可以搜索Github项目PyHubWeekly,如果喜欢,麻烦给个Star支持一下吧。此外,欢迎大家通过提交issue来投稿和推荐自己的项目~
PyHubWeekly | 第十一期:一款开挂的Python绘图工具
|
7天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
13 1
|
3天前
|
测试技术 调度 索引
python编程中常见的问题
【4月更文挑战第23天】
13 2
|
3天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver