开发者

通过Python绘制冰墩墩的示例代码

开发者 https://www.devze.com 2022-12-10 10:52 出处:网络 作者: 玩转大数据
目录效果基础绘制圆基础彩色填充形状冰墩墩代码效果 基础绘制圆 基础知识:
目录
  • 效果
  • 基础绘制圆
  • 基础彩色填充形状
  • 冰墩墩代码

效果

通过Python绘制冰墩墩的示例代码

基础绘制圆

基础知识:

forward(x):将笔向前移动 x 个单位。

right(x):将笔顺时针旋转角度 x。

left(x):将笔逆时针旋转角度 x。

penup():停止绘制海龟笔。

pendown():开始绘制海龟笔。

backward(x):将笔向后移动 x 单位。

circle(radius):此函数以“海龟”位置为中心,绘制一个给定半径的圆。

画半径为50的圆:

importturtle

#初始化
t=turtle.Turtle()

r=50
t.circle(r)

如下:

通过Python绘制冰墩墩的示例代码

画螺旋圈:

importturtle

t=turtle.Turtle()

#初始半径10
r=10

#循环画
foriinrange(100):
t.circle(r+i,45)

如下:

通过Python绘制冰墩墩的示例代码

同心圆:

importturtle

t=turtle.Turtle()

#初始半径
r=10

#循环画圆
foriinrange(50):
t.circle(r*i)
t.up()
t.sety((r*i)*(-1))
t.down()

通过Python绘制冰墩墩的示例代码

基础彩色填充形状

fillcolor():这有助于选择填充形状的颜色。它将输入参数作为颜色名称或颜色的十六进制值,并用所选颜色填充即将到来的封闭地理对象。颜色名称是基本颜色名称,即红色、蓝色、绿色、橙色。

颜色的十六进制值是十六进制数字的字符串(以'#'开头),即#RRGGBB。R、G 和 B 是十六进制数(0、1、2、3、4、5、6、7、8、9、A、B、C、D、E、F)。

begin_fill():此函数告诉海龟所有即将关闭的图形对象都需要用所选颜色填充。

end_fill():此函数告诉海龟停止填充即将关闭的图形对象。

绘制彩色填充正方形:

importturtle

#创建画笔
t=turtle.Turtle()

t.speed(1)
#手动输入边长
s=int(input("Enterthelengthofthesideofthesquare:"))

#输入颜色
col=input("Enterthecolornameorhexvalueofcolor(#RRGGBB):")

#填充颜色
t.fillcolor(col)

#开始填充
t.begin_fill()

#画正方形
for_inrange(4):
t.forward(s)
t.right(90)

#endingthefillingofthecolor
t.end_fill()

如下:

通过Python绘制冰墩墩的示例代码

绘制颜色填充星

importturtle

#创建画笔
t=turtle.Turtle()
t.speed(1)
#输入五角星边长
s=int(input("Enterthelengthofthesideofthestar:"))

#输入颜色,必须为英文
col=input("Enterthecolornameorhexvalueofcolor(#RRGGBB):")

#创建画笔填充
t.fillcolor(col)

#开始填充
t.begin_fill()

#画图
for_inrange(5):
t.forward(s)
t.right(144)

#结束
t.end_fill()

如下:

通过Python绘制冰墩墩的示例代码

还有更多知识,需要大家自己探索,我就不挨个讲了。

冰墩墩代码

为啥前面要讲一点基础,因为讲一点基础你们就能看懂代码啥意思了。

基与其它开源作者修改了一点,运行即可成功:

#coding=gbk
"""
作者:川川
公众号:玩转大数据
@时间:2022/2/814:28</code><code>微信:hxgsrubxjogxeeag</code><code>群:428335755
"""
importturtleast

#设置速度
t.speed(50)#速度
t.delay(10)#延迟

t.title('冰墩墩')
#双耳
#左耳
t.penup()
t.goto(-150,200)
t.setheading(160)
t.begin_fill()
t.pendown()
t.circle(-30,230)
t.setheading(180)
t.circle(37,90)
t.end_fill()
#右耳
t.penup()
t.goto(60,200)
t.setheading(20)
t.begin_fill()
t.pendown()
t.circle(30,230)
t.setheading(0)
t.circle(-37,90)
t.end_fill()
#头
t.pensize(5)
t.penup()
t.goto(-113,237)
t.setheading(30)
t.pendown()
t.circle(-134,60)

t.penup()
t.goto(-150,200)
t.setheading(-120)
t.pendown()
t.circle(200,80)

t.penup()
t.goto(60,200)
t.setheading(-60)
t.pendown()
t.circle(-200,80)

t.penup()
t.setheading(210)
t.pendown()
t.circl编程客栈e(-120,60)
#双眼
#左眼
#眼圈
t.penup()
t.goto(-140,100)
t.setheading(-45)
t.begin_fill()
t.pendown()
a=0.2
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.1
t.lt(3)#向左转3度
t.fd(a)#向前走a的步长
else:
a=a-0.1
t.lt(3)
t.fd(a)
t.end_fill()
#眼白
t.fillcolor("white")
t.penup()
t.goto(-103,125)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(14,360)
t.end_fill()
#眼珠
#t.fillcolor("sienna")
#t.pencolor("sienna")
t.penup()
t.goto(-102,133)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(6,360)
t.end_fill()
#右眼
#眼圈
t.penup()
t.goto(50,100)
t.setheading(45)
t.fillcolor("black")
t.pencolor("black")
t.begin_fill()
t.pendown()
a=0.2
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.1
t.lt(3)#向左转3度
t.fd(a)#向前走a的步长
else:
a=a-0.1
t.lt(3)
t.fd(a)
t.end_fill()
#眼白
t.fillcolor("white")
t.penup()
t.goto(13,125)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(14,360)
t.end_fill()
#眼珠
#t.fillcolor("sienna")
#t.pencolor("sienna")
t.penup()
t.goto(12,133)
t.setheading(0)
t.begin_fill()
t.pendown()
t.circle(6,360)
t.http://www.cppcns.comend_fill()
#鼻子
t.pencolor("black")
t.fillcolor("black")
t.penup()
t.goto(-55,133)
t.begin_fill()
t.pendown()
t.fd(20)
t.seth(-120)
t.fd(20)
t.seth(120)
t.fd(20)
t.end_fill()
#嘴
t.penup()
t.goto(-70,110)
t.setheading(-30)
t.fillcolor("red")
t.begin_fill()
t.pendown()
t.circle(50,60)
t.setheading(-120)
t.circle(-100,15)
t.circle(-15,90)
t.circle(-100,15)
t.end_fill()
#四肢
#左臂
t.penup()
t.goto(-175,100)
t.fillcolor("black")
t.begin_fill()
t.setheading(-120)
t.pendown()
t.fd(100)
t.setheading(-110)
t.circle(20,180)
t.fd(30)
t.circle(-5,160)
t.end_fill()
#右臂
t.penup()
t.goto(85,100)
t.setheading(60)
t.begin_fill()
t.pendown()
t.fd(100)
t.setheading(70)
t.circle(20,180)
t.fd(30)
t.circle(-5,160)
t.end_fill()
#小红心
t.penup()
t.pencolor("red")
t.fillcolor('red')
t.goto(105,200)
t.begin_fill()
t.pendown()
t.circle(-5,180)
t.setheading(90)
t.circle(-5,180)
t.setheading(-120)
t.fd(17)
t.penup()
t.goto(105,200)
t.pendown()
t.setheading(-60)
t.fd(17)
t.end_fill()
t.pencolor("black")
t.fillcolor("black")
#左腿
t.penup()
t.goto(-120,-45)
t.begin_fill()
t.pendown()
t.setheading(-90)
t.circle(-140,20)
t.circle(5,109)
t.fd(30)
t.circle(10,120)
t.setheading(90)
t.circle(-140,10)
t.end_fill()
#右腿
t.penup()
t.goto(30,-45)
t.begin_fill()
t.pendown()
t.setheading(-90)
t.circle(140,20)
t.circle(-5,109)
t.fd(30)
t.circle(-10,120)
t.setheading(90)
t.circle(140,10)
t.end_fill()
#冰糖外壳
t.pensize(3)
t.penup()
t.goto(-160,195)
t.setheading(160)
t.pendown()
t.circle(-40,230)
t.setheading(30)
t.circle(-134,58)
t.setheading(60)
t.circle(-40,215)
t.setheading(-60)
t.fd(15)
t.circle(2,200)
t.setheading(65)
t.fd(30)
t.circle(-25,180)
t.fd(100)
t.circle(2,25)
t.circle(-200,47)
t.circle(2,60)
t.circle(140,23)
t.circle(-2,90)
t.setheading(180)
t.fd(70)
t.circle(-2,90)
t.fd(30)
t.setheading(-160)
t.circle(-100,35)
t.setheading(-90)
t.fd(30)
t.circle(-2,90)
t.fd(70)
t.circle(-2,90)
t.setheading(60)
t.circle(140,30)
t.circle(2,45)
t.circle(-200,19)
t.circle(2,130)
t.fd(30)
t.circle(-25,180)
t.fd(100)
t.setheading(90)
t.circle(-200,30)
#冰糖面罩
t.pensize(3)
t.penup()
t.goto(65,120)
t.setheading(90)
t.pendown(cvYRDGTflb)
t.pencolor("red")
a=1
foriinrange(120):
if0<=i<30or60<=i<90:#控制a的变化
a=a+0.25
t.lt(3)#向左转3度
t.fd(a)#向前走a的步长
else:
a=a-0.25
t.lt(3)
t.fd(a)
t.pencolor("orange")
t.penup()
t.goto(66,120)
t.pendown()
a=1
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.255
t.lt(3)
t.fd(a)
else:
a=a-0.255
t.lt(3)
t.fd(a)
t.pencolor("green")
t.penup()
t.goto(67,120)
t.pendown()
a=1
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.2555
t.lt(3)
t.fd(a)
else:
a=a-0.2555
t.lt(3)
t.fd(a)
t.pencolor("deepskyblue")
t.pe编程客栈nup()
t.goto(68,120)
t.pendown()
a=1
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.25955
t.lt(3)
t.fd(a)
else:
a=a-0.25955
t.lt(3)
t.fd(a)
t.pencolor("pink")
t.penup()
t.goto(71,120)
t.pendown()
a=1
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.26
t.lt(3)
t.fd(a)
else:
a=a-0.26
t.lt(3)
t.fd(a)
t.pencolor("purple")
t.penup()
t.goto(72,120)
t.pendown()
a=1
foriinrange(120):
if0<=i<30or60<=i<90:
a=a+0.269
t.lt(3)
t.fd(a)
else:
a=a-0.269
t.lt(3)
t.fd(a)

#五环
t.penup()
t.goto(-55,-10)
t.pendown()
t.pencolor("blue")
t.circle(10)
t.penup()
t.goto(-40,-10)
t.pendown()
t.pencolor("black")
t.circle(10)
t.penup()
t.goto(-25,-10)
t.pendown()
t.pencolor("red")
t.circle(10)
t.penup()
t.goto(-50,-20)
t.pendown()
t.pencolor("yellow")
t.circle(10)
t.penup()
t.goto(-30,-20)
t.pendown()
t.pencolor("green")
t.circle(10)

#输出文字
printer=t.Turtle()
printer.hideturtle()
printer.penup()
printer.goto(-350,-50)
printer.write("可\n\n",move=True,align="left",font=("楷体",31,"bold"))
printer.goto(-350,-100)
printer.write("爱\n\n",move=True,align="left",font=("楷体",31,"bold"))
printer.goto(-350,-150)
printer.write("冰\n\n",move=True,align="left",font=("楷体",31,"bold"))
printer.goto(-350,-200)
printer.write("墩\n\n",move=True,align="left",font=("楷体",31,"bold"))
printer.goto(-350,-250)
printer.write("墩\n\n",move=True,align="left",font=("楷体",31,"bold"))

t.hideturtle()
t.done()

如果想要打包成exe,cd到该文件目录下执行:

pyinstaller -F -w  bingdunndun.py

则会生成的dist文件下有:bingdundun.exe

生成好后你也可以直接改名为:冰墩墩.exe

通过Python绘制冰墩墩的示例代码

以上就是通过python绘制冰墩墩的示例代码的详细内容,更多关于Py编程客栈thon绘制冰墩墩的资料请关注我们其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号