600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 【Python】用Python画小猪佩奇 落叶树和动漫人物

【Python】用Python画小猪佩奇 落叶树和动漫人物

时间:2023-05-17 23:55:07

相关推荐

【Python】用Python画小猪佩奇 落叶树和动漫人物

本文目录:小猪佩奇-落叶树-魔法少女

本文作者:小赖同学

最近在浏览Python文章时,无意中发现了一个好玩的画图工具turtle,看到各路大神竟然用Python画了一个图片出来,无法用言语来形容心中感概,只能重复默念“卧槽卧槽厉害”。为了丰富大家的娱乐时间,我进行了汇总,整理出三个比较好看的给大家消磨时间。

小猪佩奇

效果图:

代码:

# !/usr/bin/env python3# -*- coding: utf-8 -*-# @Time : /3/13 20:00# @Author : Lucas Lai# @IDE: PyCharmimport turtle as tt​def nose():# 鼻子tt.pu()tt.goto(-100, 100)tt.pd()tt.seth(-30)tt.begin_fill()a = 0.4for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a = a + 0.08tt.lt(3) # 向左转3度tt.fd(a) # 向前走a的步长else:a = a - 0.08tt.lt(3)tt.fd(a)tt.end_fill()tt.pu()tt.seth(90)tt.fd(25)tt.seth(0)tt.fd(10)tt.pd()tt.pencolor(255, 155, 192)tt.seth(10)tt.begin_fill()tt.circle(5)tt.color(160, 82, 45)tt.end_fill()​tt.pu()tt.seth(0)tt.fd(20)tt.pd()tt.pencolor(255, 155, 192)tt.seth(10)tt.begin_fill()tt.circle(5)tt.color(160, 82, 45)tt.end_fill()​def head():# 头tt.color((255, 155, 192), "pink")tt.pu()tt.seth(90)tt.fd(41)tt.seth(0)tt.fd(0)tt.pd()tt.begin_fill()tt.seth(180)tt.circle(300, -30)tt.circle(100, -60)tt.circle(80, -100)tt.circle(150, -20)tt.circle(60, -95)tt.seth(161)tt.circle(-300, 15)tt.pu()tt.goto(-100, 100)tt.pd()tt.seth(-30)a = 0.4for i in range(60):if 0 <= i < 30 or 60 <= i < 90:a = a + 0.08tt.lt(3) # 向左转3度tt.fd(a) # 向前走a的步长else:a = a - 0.08tt.lt(3)tt.fd(a)tt.end_fill()​def ear():# 耳朵tt.color((255, 155, 192), "pink")tt.pu()tt.seth(90)tt.fd(-7)tt.seth(0)tt.fd(70)tt.pd()tt.begin_fill()tt.seth(100)tt.circle(-50, 50)tt.circle(-10, 120)tt.circle(-50, 54)tt.end_fill()​tt.pu()tt.seth(90)tt.fd(-12)tt.seth(0)tt.fd(30)tt.pd()tt.begin_fill()tt.seth(100)tt.circle(-50, 50)tt.circle(-10, 120)tt.circle(-50, 56)tt.end_fill()​def eyes():# 眼睛tt.color((255, 155, 192), "white")tt.pu()tt.seth(90)tt.fd(-20)tt.seth(0)tt.fd(-95)tt.pd()tt.begin_fill()tt.circle(15)tt.end_fill()​tt.color("black")tt.pu()tt.seth(90)tt.fd(12)tt.seth(0)tt.fd(-3)tt.pd()tt.begin_fill()tt.circle(3)tt.end_fill()​tt.color((255, 155, 192), "white")tt.pu()tt.seth(90)tt.fd(-25)tt.seth(0)tt.fd(40)tt.pd()tt.begin_fill()tt.circle(15)tt.end_fill()​tt.color("black")tt.pu()tt.seth(90)tt.fd(12)tt.seth(0)tt.fd(-3)tt.pd()tt.begin_fill()tt.circle(3)tt.end_fill()def gill():# 腮tt.color((255, 155, 192))tt.pu()tt.seth(90)tt.fd(-95)tt.seth(0)tt.fd(65)tt.pd()tt.begin_fill()tt.circle(30)tt.end_fill()​def mouth():# 嘴tt.color(239, 69, 19)tt.pu()tt.seth(90)tt.fd(15)tt.seth(0)tt.fd(-100)tt.pd()tt.seth(-80)tt.circle(30, 40)tt.circle(40, 80)​def body():# 身体tt.color("red", (255, 99, 71))tt.pu()tt.seth(90)tt.fd(-20)tt.seth(0)tt.fd(-78)tt.pd()tt.begin_fill()tt.seth(-130)tt.circle(100, 10)tt.circle(300, 30)tt.seth(0)tt.fd(230)tt.seth(90)tt.circle(300, 30)tt.circle(100, 3)tt.color((255, 155, 192), (255, 100, 100))tt.seth(-135)tt.circle(-80, 63)tt.circle(-150, 24)tt.end_fill()​def hand():# 手tt.color((255, 155, 192))tt.pu()tt.seth(90)tt.fd(-40)tt.seth(0)tt.fd(-27)tt.pd()tt.seth(-160)tt.circle(300, 15)tt.pu()tt.seth(90)tt.fd(15)tt.seth(0)tt.fd(0)tt.pd()tt.seth(-10)tt.circle(-20, 90)​tt.pu()tt.seth(90)tt.fd(30)tt.seth(0)tt.fd(237)tt.pd()tt.seth(-20)tt.circle(-300, 15)tt.pu()tt.seth(90)tt.fd(20)tt.seth(0)tt.fd(0)tt.pd()tt.seth(-170)tt.circle(20, 90)​def foot():# 脚tt.pensize(10)tt.color((240, 128, 128))tt.pu()tt.seth(90)tt.fd(-75)tt.seth(0)tt.fd(-180)tt.pd()tt.seth(-90)tt.fd(40)tt.seth(-180)tt.color("black")tt.pensize(15)tt.fd(20)​tt.pensize(10)tt.color((240, 128, 128))tt.pu()tt.seth(90)tt.fd(40)tt.seth(0)tt.fd(90)tt.pd()tt.seth(-90)tt.fd(40)tt.seth(-180)tt.color("black")tt.pensize(15)tt.fd(20)​def tail():# 尾巴tt.pensize(4)tt.color((255, 155, 192))tt.pu()tt.seth(90)tt.fd(70)tt.seth(0)tt.fd(95)tt.pd()tt.seth(0)tt.circle(70, 20)tt.circle(10, 330)tt.circle(70, 30)tt.done()​def init():tt.pensize(4)tt.hideturtle()tt.colormode(255)tt.color((255, 155, 192), "pink")tt.setup(840, 500)tt.speed(10)​def main():init()nose()head()ear()eyes()gill()mouth()body()hand()foot()tail()​if __name__ == '__main__':main()

落叶树

效果图:

代码:

# !/usr/bin/env python3# -*- coding: utf-8 -*-# @Time : /3/13 20:31# @Author : Lucas Lai# @IDE: PyCharmimport turtle as ttfrom random import *from math import *​def tree(n, l):tt.pd() # 下笔# 阴影效果t = cos(radians(tt.heading() + 45)) / 8 + 0.25tt.pencolor(t, t, t)tt.pensize(n / 3)tt.forward(l) # 画树枝​if n > 0:b = random() * 15 + 10 # 右分支偏转角度c = random() * 15 + 10 # 左分支偏转角度d = l * (random() * 0.25 + 0.7) # 下一个分支的长度# 右转一定角度,画右分支tt.right(b)tree(n - 1, d)# 左转一定角度,画左分支tt.left(b + c)tree(n - 1, d)# 转回来tt.right(c)else:# 画叶子tt.right(90)n = cos(radians(tt.heading() - 45)) / 4 + 0.5tt.pencolor(n, n * 0.8, n * 0.8)tt.circle(3)tt.left(90)# 添加0.3倍的飘落叶子if random() > 0.7:tt.pu()# 飘落t = tt.heading()an = -40 + random() * 40tt.setheading(an)dis = int(800 * random() * 0.5 + 400 * random() * 0.3 + 200 * random() * 0.2)tt.forward(dis)tt.setheading(t)# 画叶子tt.pd()tt.right(90)n = cos(radians(tt.heading() - 45)) / 4 + 0.5tt.pencolor(n * 0.5 + 0.5, 0.4 + n * 0.4, 0.4 + n * 0.4)tt.circle(2)tt.left(90)tt.pu()# 返回t = tt.heading()tt.setheading(an)tt.backward(dis)tt.setheading(t)tt.pu()tt.backward(l) # 退回​def init():tt.bgcolor(0.5, 0.5, 0.5) # 背景色tt.hideturtle() # 隐藏turtlett.speed(10) # 速度 1-10渐进,0 最快tt.tracer(0, 0)tt.pu() # 抬笔tt.backward(100)tt.left(90) # 左转90度tt.pu() # 抬笔tt.backward(300) # 后退300tree(12, 100) # 递归7层tt.done()​def main():init()​if __name__ == '__main__':main()

魔法少女

因篇幅有限,代码太多了,这个就不放代码了,可在微信公众号“学渣同学”后台回复“魔法少女”发你源码链接,直接上最终效果图:

推荐阅读

【Python】我用python爬取一月份微博热搜数据来分析人们对新型肺炎的关注程度变化

无聊的周末,我写了个证件照换底色神器,一秒就“变色”

如有问题,可联系我

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。