用Python画一只小兔子,祝您新年前途似锦,大展宏图
用Python画一只小兔子,祝您新年前途似锦,大展宏图
置顶
小斌哥ge
于2023-01-11 21:36:24发布
2637
收藏
36
分类专栏:
Python创造新事物
Python/PYPI
文章标签:
python
开发语言
兔年
turtle
python画图
Python创造新事物
同时被 2 个专栏收录
8 篇文章
3 订阅
订阅专栏
Python/PYPI
90 篇文章
39 订阅
订阅专栏
用Python画一只小兔子,祝您新年前途似锦,大展宏图
兔年到了,祝大家新年前途似锦!大展宏图!
2021牛年,我用Python画了一头金牛,参考:Python画金牛 2022虎年,我用Python画了一只小老虎,参考:Python画小老虎
今年是第三年,还是一样的方式,今年画一只小兔子,为大家送上祝福。
绘图过程录制成了如下视频,点击视频可以先看到效果。文末提供完整代码获取方式。
视频展示
用Python画一只小兔子
实现方法介绍
本文的绘图工具使用Python标准库turtle库,无需安装,导入即可使用。
部分函数用法介绍(对绘图函数了解可以跳过此部分):
画布设置
title(): 设置窗口的标题。bgpic():设置背景图片。setup(width, height, x, y): 设置窗口大小和窗口左上角在屏幕中的位置。shape():设置鼠标的形状。done(): 绘图结束后,不自动关闭窗口。画笔设置
pencolor(color): 设置画笔颜色。width(): 设置画笔宽度。speed(int): 设置画笔的速度,传入1~10的数字,1最慢,10最快。传入其他值会更快,但是没有鼠标移动的动画效果。penup(): 提起画笔,提起后移动画笔不会留下痕迹。pendown(): 落下画笔,开始绘图前先将画笔落下。setx(value): 设置画笔的x轴坐标。sety(value): 设置画笔的y轴坐标。towards(x, y): 设置画笔指向的点。setheading(): 设置画笔的方向。与towards()配合可以设置画笔指向某个点,如setheading(towards(0, 0))可以设置画笔指向原点。pos(): 返回画笔当前的坐标。鼠标移动一段时间后可以print()打印此函数获取鼠标位置。heading(): 返回画笔当前的方向。画笔操作
circle(radius[, extent=arc]): 画一个圆,传入圆的半径,根据画笔的方向控制圆心的位置,圆心与画笔的方向垂直。传入弧度值可以画出指定弧度的圆弧。left(angle): 画笔左转,传入任意一个目标角度。right(angle): 画笔右转。forward(distance): 画笔前进一段距离。backward(distance): 画笔后退一段距离。goto(x, y): 移动画笔到指定坐标。颜色填充
begin_fill(): 开始填充。fillcolor(color): 设置图形中填充的颜色。end_fill(): 结束填充。具体画图时,调整画笔的起点,设置不同的转角度数和不同的前进长度,可以得到不同形状的弧线,用弧线构成完整的图形。
部分代码
# coding=utf-8 """ 作者:小斌哥ge 公众号: 小斌哥ge 微信:XiaoBinnGe- 发表时间:2023年1月 """ from turtle import * import time def set_start(x, y, w=0.5, c='black'): penup() setx(x) sety(y) setheading(towards(0, 0)) width(w) pencolor(c) pendown() speed(0) def left_rotate(time, angle, length): for i in range(time): left(angle) forward(length) def right_rotate(time, angle, length): for i in range(time): right(angle) forward(length) def fill_color(color): def decorator_all(func): def wrapper(*args, **kwargs): begin_fill() func(*args, **kwargs) fillcolor(color) end_fill() return wrapper return decorator_all def fill_color_patch(x, y, c='white'): set_start(x, y, 1, c=c) forward(1) def draw_circle(radius, color, color2=''): if color2 == '': color2 = color penup() setheading(towards(0, 0)) right(90) pencolor(color) pendown() begin_fill() circle(radius) fillcolor(color2) end_fill() def draw_ear(): # 左侧耳朵 fill_color_patch(-50, 86, c='#9392B3') begin_fill() set_start(-50, 86, w=4, c='#9392B3') right_rotate(1, 174, 10) right_rotate(5, 5, 15) right_rotate(4, 6, 15) width(3) right_rotate(2, 4, 13) right_rotate(2, 5, 10) left_rotate(3, 9, 12) width(4) right_rotate(1, 128, 10) right_rotate(3, 9.5, 31) right_rotate(5, 7, 25) right_rotate(1, 80, 10) left_rotate(3, 7, 12) fillcolor('white') end_fill() width(3) right_rotate(1, 160, 5) left_rotate(1, 20, 10) left_rotate(3, 8, 20) width(4) left_rotate(1, 2, 15) left_rotate(5, 5, 15) left_rotate(4, 3, 15) fill_color_patch(-35, 105, c='#E6E6FA') begin_fill() set_start(-35, 105, w=1, c='#E6E6FA') right_rotate(1, 170, 10) right_rotate(3, 8, 25) right_rotate(3, 11, 23) right_rotate(1, 130, 20) right_rotate(5, 8, 25) goto(-35, 105) fillcolor('#E6E6FA') end_fill() fill_color_patch(-32, 110, c='#D6D2F6') begin_fill() set_start(-32, 110, w=1, c='#D6D2F6') right_rotate(1, 170, 10) right_rotate(3, 7, 23) right_rotate(3, 11, 20) right_rotate(1, 131, 20) right_rotate(5, 8, 23) goto(-32, 110) fillcolor('#D6D2F6') end_fill() set_start(-48.87, 85.22, w=3, c='#9392B3') setheading(195.67) right_rotate(1, 160, 5) left_rotate(1, 20, 10) left_rotate(3, 8, 20) width(4) left_rotate(1, 2, 15) left_rotate(5, 5, 15) left_rotate(4, 3, 15) # 右侧耳朵 fill_color_patch(90, 67, c='#9392B3') begin_fill() set_start(90, 67, w=4, c='#9392B3') left_rotate(1, 180, 10) left_rotate(3, 5, 13) left_rotate(2, 8, 15) left_rotate(3, 6, 15) left_rotate(4, 5, 15) right_rotate(4, 5.5, 8) left_rotate(1, 132, 15) left_rotate(2, 8, 18) left_rotate(5, 5, 18) left_rotate(4, 4, 17) left_rotate(1, 70, 5) right_rotate(3, 5, 8) goto(90, 67) fillcolor('white') end_fill() left_rotate(1, 180, 5) right_rotate(1, 63, 10) left_rotate(4, 2, 12) right_rotate(3, 3, 16) width(3) right_rotate(3, 6, 15) right_rotate(4, 5, 13.5) fill_color_patch(90, 80, c='#E6E6FA') begin_fill() set_start(90, 80, w=1, c='#E6E6FA') left_rotate(1, 175, 10) left_rotate(4, 10, 12) left_rotate(4, 7, 15) right_rotate(4, 5, 9) left_rotate(1, 150, 15) left_rotate(4, 7.5, 15) left_rotate(2, 3, 15) right_rotate(2, 2, 10) left_rotate(2, 7, 12) goto(90, 80) fillcolor('#E6E6FA') end_fill() fill_color_patch(90, 85, c='#D6D2F6') begin_fill() set_start(90, 85, w=1, c='#D6D2F6') left_rotate(1, 175, 10) left_rotate(4, 10, 10) left_rotate(4, 6, 15) right_rotate(4, 5, 7) left_rotate(1, 153, 15) left_rotate(3, 9.5, 15) left_rotate(2, 2, 15) right_rotate(3, 0, 10) goto(90, 85) fillcolor('#D6D2F6') end_fill() set_start(90.00, 67.00, w=4, c='#9392B3') setheading(327.67) left_rotate(1, 180, 5) right_rotate(1, 63, 10) left_rotate(4, 2, 12) right_rotate(3, 3, 16) width(3) right_rotate(3, 6, 15) right_rotate(4, 5, 13.5) def draw_cat(): set_start(34, 141, w=1, c='#C65B03') draw_circle(5, '#C65B03', color2='') set_start(33.5, 139, w=1, c='#ED9C22') draw_circle(3, '#ED9C22', color2='') fill_color_patch(-23, 90, c='#850009') begin_fill() set_start(-23, 90, w=2, c='#850009') left_rotate(1, 80, 10) right_rotate(6, 4, 14.2) left_rotate(1, 105, 9) left_rotate(2, 8, 8) width(3) left_rotate(11, 12, 10) width(2) left_rotate(2, 12, 12) goto(-23, 90) fillcolor('#B80705') end_fill() fill_color_patch(46, 110, c='#850009') begin_fill() set_start(46, 110, w=3, c='#850009') right_rotate(1, 145, 9) left_rotate(2, 16, 8) width(2) left_rotate(3, 22, 2) left_rotate(3, 12, 9) goto(46, 110) fillcolor('#E50703') end_fill() fill_color_patch(-23, 90, c='#BE7728') begin_fill() set_start(-23, 90, w=2, c='#BE7728') left_rotate(1, 80, 10) right_rotate(6, 4, 14.2) left_rotate(1, 105, 9) left_rotate(2, 8, 8) width(3) left_rotate(1, 55, 10) left_rotate(2, 5, 15) left_rotate(3, 6.5, 16) width(2) left_rotate(1, 55, 10) left_rotate(2, 10, 8) goto(-23, 90) fillcolor('#E4C127') end_fill() fill_color_patch(20, 98, c='#DC960D') begin_fill() set_start(20, 98, w=1, c='#DC960D') left_rotate(1, 75, 5) left_rotate(2, 12, 5) left_rotate(3, 33, 4) left_rotate(3, 35, 1) right_rotate(3, 28, 2) left_rotate(3, 35, 3) right_rotate(3, 30, 2) left_rotate(3, 45, 2) left_rotate(1, 30, 2) right_rotate(3, 25, 1) left_rotate(3, 45, 2) left_rotate(1, 30, 2) goto(20, 98) fillcolor('#DC960D') end_fill() fill_color_patch(22, 99, c='#839D76') begin_fill() set_start(22, 99, w=1, c='#839D76') left_rotate(1, 25, 0) left_rotate(3, 30, 4) left_rotate(3, 30, 2) left_rotate(3, 30, 4) left_rotate(3, 30, 2) fillcolor('#839D76') end_fill() begin_fill() set_start(24, 99, w=1, c='#398640') left_rotate(1, 25, 0) left_rotate(3, 30, 2.5) left_rotate(3, 30, 2) left_rotate(3, 30, 2.5) left_rotate(3, 30, 2) fillcolor('#398640') end_fill() begin_fill() set_start(25.5, 101, w=1, c='#0ADBC9') left_rotate(1, 25, 0) left_rotate(3, 30, 1.8) left_rotate(3, 30, 1) left_rotate(3, 30, 1.8) left_rotate(3, 30, 1) fillcolor('#0ADBC9') end_fill() def draw_head(): fill_color_patch(46, 83, c='#9392B3') begin_fill() set_start(46, 83, w=4, c='#9392B3') left_rotate(1, 105, 10) right_rotate(3, 8, 20) right_rotate(3, 10, 22) right_rotate(1, 13, 15) left_rotate(3, 3, 9) left_rotate(2, 12, 6) left_rotate(1, 30, 5) left_rotate(2, 15, 3) right_rotate(1, 135, 8) right_rotate(3, 8, 8) left_rotate(1, 140, 8) left_rotate(2, 10, 8) right_rotate(1, 130, 8) right_rotate(5, 7.5, 7.5) left_rotate(1, 140, 5) left_rotate(2, 8, 5) right_rotate(1, 125, 5) right_rotate(5, 8, 6) goto(125, -130) set_start(125, -130, w=4, c='#9392B3') left_rotate(1, 78, 10) right_rotate(3, 5, 18) right_rotate(4, 6, 21) right_rotate(5, 5, 18) right_rotate(3, 7, 20) left_rotate(1, 15, 10) right_rotate(2, 5, 6) right_rotate(4, 7, 6.5) right_rotate(1, 130, 5) left_rotate(3, 11, 4.5) left_rotate(1, 140, 7) right_rotate(3, 12, 6) right_rotate(5, 5, 5) right_rotate(1, 130, 5) left_rotate(3, 5, 4) left_rotate(3, 13, 3) left_rotate(1, 120, 8) right_rotate(5, 10, 6.5) right_rotate(1, 95, 5) left_rotate(3, 12, 5) left_rotate(3, 10, 10) right_rotate(3, 5, 12) right_rotate(3, 2, 5) right_rotate(3, 5, 12) right_rotate(4, 5, 13) left_rotate(3, 6, 6) left_rotate(5, 13.5, 7) right_rotate(1, 158, 8) right_rotate(3, 7, 7) right_rotate(5, 15, 5) fillcolor('white') end_fill() if __name__ == '__main__': title('前途似锦(公众号:小斌哥ge)') wide = 800 height = 600 screensize(wide, height, '#DC0058') setup(wide+30, height+30, 50, 10) shape(name='turtle') time.sleep(2) draw_ear() draw_cat() draw_head() # draw_face() # draw_mouth() # draw_eye() # draw_eyebrow() # draw_nose() # draw_cheek() # draw_forehead() # draw_foot() # draw_body() # draw_hand() # draw_clothes() # draw_jade() set_start(1000, 1000, 2.5) done()绘图结果
代码获取
代码有1800行,可以用WX关注我,在后台回复关键字“前途似锦”获取完整代码。
声明:转载、二创等使用本文代码,请注明出处。
相关阅读: 用Python标准库turtle画一只老虎,祝您新年虎虎生威,大吉大利 用Python标准库turtle画一头金牛,祝您新年牛气冲天!
📢欢迎 点赞👍 收藏⭐ 评论📝 关注❤ 如有错误敬请指正!
☟ 学Python,点击下方名片关注我。☟