50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > Python-turtle绘画旅程第一站:哆啦a梦

Python-turtle绘画旅程第一站:哆啦a梦

时间:2024-02-01 23:42:16

相关推荐

Python-turtle绘画旅程第一站:哆啦a梦

本次绘画主要用到的库是turtle(海龟绘图),主要用到的函数是circle(画圆)、fillcolor(填充颜色)、forward(画线)等。具体用到的函数在代码中都有注释。

铛铛铛铛!!!先给大家揭晓无敌可爱之哆啦a梦:

接下来是神奇而繁琐的代码时刻(用到的函数其实很基础,主要是要理清坐标轴位置以及颜色覆盖顺序,有兴趣的伙伴们可以试图运行一下代码,绘画过程大约耗时1分钟吧,过程中还有些丑,请保持耐心!):

import turtle as timport math# 计算长度、角度 t1:画笔对象 r:半径 angle:扇形(圆形)的角度def myarc(t1, r, angle):arc_length = 2 * math.pi * r * angle / 360 # angle角度的扇形的弧长n = int(arc_length / 3) + 1 # 线段条数step_length = arc_length / n # 每条线段的长度step_angle = angle / n # 每条线段的角度mypolyline(t1, n, step_length, step_angle)# 画弧线 t1:画笔对象 n:线段条数 length:每条线段长度 angle:每条线段的角度def mypolyline(t1, n, length, angle):for index in range(n):t1.fd(length)t1.lt(angle)# 设置画布大小,背景色t.screensize(500, 500, "white")# 画笔宽度t.pensize(2)# 画笔颜色t.pencolor("black")# 画笔移动速度 [1-10]t.speed(10)# 头部t.fillcolor("#57a3c7")t.begin_fill()t.circle(100)t.end_fill()# 白芯t.fillcolor("#fff")t.begin_fill()t.circle(85)t.end_fill()# 左眼睛t.penup()t.goto(-22, 130)t.pendown()step = 1t.setheading(0)t.fillcolor("#fff")t.begin_fill()for i in range(2):for j in range(60):if j < 30:step += 0.02else:step -= 0.02t.forward(step)t.left(3)t.end_fill()# 左眼仁t.penup()t.goto(-17, 150)t.pendown()step = 1t.setheading(0)t.fillcolor("#000")t.begin_fill()for i in range(2):for j in range(30):if j < 15:step += 0.0001else:step -= 0.0001t.forward(step)t.left(8)t.end_fill()# 左眼仁-白板t.penup()t.goto(-15, 160)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(4)t.end_fill()# 右眼睛t.penup()t.goto(24, 130)t.pendown()step = 1t.setheading(0)t.fillcolor("#fff")t.begin_fill()for i in range(2):for j in range(60):if j < 30:step += 0.02else:step -= 0.02t.forward(step)t.left(3)t.end_fill()# 右眼仁t.penup()t.goto(17, 150)t.pendown()step = 1t.setheading(0)t.fillcolor("#000")t.begin_fill()for i in range(2):for j in range(30):if j < 15:step += 0.0001else:step -= 0.0001t.forward(step)t.left(8)t.end_fill()# 右眼仁-白板t.penup()t.goto(19, 160)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(4)t.end_fill()# 鼻子t.setheading(0)t.penup()t.goto(2, 115)t.pendown()t.fillcolor("#b74255")t.begin_fill()t.circle(13)t.end_fill()t.pensize(0)t.penup()t.goto(2, 115)t.pendown()t.fillcolor("#7e3048")t.begin_fill()t.circle(8)t.end_fill()t.penup()t.goto(-1, 125)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(5)t.end_fill()# 嘴巴t.penup()t.goto(-60, 80)t.pendown()step = 1t.setheading(270)t.fillcolor("#7e3048")t.begin_fill()myarc(t, 60, 190)# 画弧线t.setheading(180)t.forward(120)t.end_fill()# 舌头t.penup()t.goto(2, 60)t.setheading(120)t.fillcolor("#ce665b")t.pendown()t.begin_fill()myarc(t, 25, 170)t.penup()t.goto(45, 40)t.setheading(70)t.pendown()myarc(t, 25, 170)t.penup()t.pencolor("#ce665b")t.goto(-44, 40)t.setheading(305)t.pendown()myarc(t, 53, 125)t.end_fill()t.pencolor("black")# 左边胡子t.pensize(3)t.penup()t.goto(-35, 120)t.setheading(165)t.pendown()t.forward(50)t.penup()t.goto(-35, 110)t.setheading(175)t.pendown()t.forward(50)t.penup()t.goto(-35, 100)t.setheading(185)t.pendown()t.forward(50)# 右边胡子t.penup()t.goto(35, 120)t.setheading(7)t.pendown()t.forward(50)t.penup()t.goto(35, 110)t.setheading(0)t.pendown()t.forward(50)t.penup()t.goto(35, 100)t.setheading(353)t.pendown()t.forward(50)# 身子-左腿t.pensize(2)t.pencolor("#57a3c5")t.penup()t.goto(-70, 35)t.setheading(265)t.fillcolor("#57a3c5")t.begin_fill()t.pendown()for y in range(50):if y > 10:t.pencolor("black")if y < 35:t.left(0.3)else:t.right(0.3)t.forward(3)t.setheading(0)t.forward(60)t.setheading(70)t.forward(25)t.setheading(90)t.forward(90)t.setheading(160)t.forward(78)t.end_fill()# 左胳膊t.fillcolor("#57a3c5")t.begin_fill()t.penup()t.goto(-72, 32)t.setheading(200)t.pendown()for y1 in range(90):t.forward(1)t.left(0.3)t.setheading(300)t.forward(35)t.setheading(30)t.forward(65)t.setheading(88)for y2 in range(2):if y2 == 1:t.pencolor("#57a3c5")t.forward(20)t.end_fill()# 左手t.pencolor("black")t.penup()t.goto(-100, -25)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(25)t.end_fill()# 身子-右腿t.penup()t.goto(70, 33)t.setheading(275)t.fillcolor("#57a3c5")t.begin_fill()t.pendown()for y in range(50):if y < 35:t.right(0.3)else:t.left(0.3)t.forward(3)t.setheading(180)t.forward(60)t.setheading(110)t.forward(25)t.setheading(90)t.forward(90)t.setheading(20)t.forward(76)t.end_fill()# # 右胳膊t.fillcolor("#57a3c5")t.begin_fill()t.penup()t.goto(70, 28)t.setheading(35)t.pendown()for y1 in range(30):t.forward(1.5)t.left(0.3)t.setheading(330)t.forward(35)t.setheading(240)for y2 in range(91):t.forward(1)t.right(0.3)t.end_fill()# # 右手t.penup()t.goto(110, 70)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(25)t.end_fill()# 左脚t.penup()t.goto(-66, -100)t.pendown()t.fillcolor("#fff")t.begin_fill()t.setheading(190)for x in range(50):t.forward(1)t.left(3)for x1 in range(70):t.forward(1)t.left(0.8)for x2 in range(22):t.forward(1)t.left(5)t.setheading(190)for x3 in range(20):t.forward(1)t.left(0.02)for x4 in range(50):t.forward(1)t.right(1)t.end_fill()# 右脚t.penup()t.goto(66, -100)t.pendown()t.fillcolor("#fff")t.begin_fill()t.setheading(350)for x in range(50):t.forward(1)t.right(3)for x1 in range(70):t.forward(1)t.right(0.8)for x2 in range(22):t.forward(1)t.right(5)t.setheading(350)for x3 in range(20):t.forward(1)t.right(0.02)for x4 in range(50):t.forward(1)t.left(1)t.end_fill()# 肚子t.penup()t.setheading(0)t.goto(0, -95)t.pendown()t.fillcolor("#fff")t.begin_fill()t.circle(50)t.end_fill()t.penup()t.goto(-40, -45)t.setheading(270)t.pendown()t.circle(40, 180)t.setheading(180)t.forward(80)# 铃铛绳t.fillcolor("#b13954")t.begin_fill()t.penup()t.goto(-82, 27)t.setheading(327)t.pendown()for z in range(170):t.forward(1)if z < 80:t.left(0.3)else:t.left(0.6)for z1 in range(12):t.forward(1)t.left(10)t.setheading(220)for z2 in range(162):t.forward(1)if z2 < 70:t.right(0.5)elif z2 < 100:t.right(0.8)else:t.right(0.2)for z3 in range(20):t.forward(1)t.left(9.5)t.end_fill()# 铃铛t.penup()t.goto(0, 10)t.setheading(0)t.pendown()t.fillcolor("#e1b959")t.begin_fill()t.circle(-17)t.end_fill()t.penup()t.goto(2, -3)t.pendown()t.fillcolor("#4c442f")t.begin_fill()t.circle(-5)t.end_fill()t.penup()t.goto(2, -8)t.pendown()t.setheading(275)for k in range(15):t.forward(1)t.right(0.2)t.penup()t.goto(-18, -10)t.pendown()t.setheading(30)for p in range(35):t.forward(1)t.right(0.5)t.penup()t.goto(-17, -6)t.pendown()t.setheading(30)for p in range(30):t.forward(1)t.right(0.5)# 隐藏画笔t.hideturtle()# 窗口不自动关闭t.mainloop()

此篇文章是本人第一篇原创,也想了好久要写什么题材的,最后还是选择了自己喜爱的绘画,主要是为了让大家了解编程绘画,也为积攒学习经验,如需转载请标明出处,谢谢!!!

ps:剧透一下,下篇小可爱和哆啦a梦同属猫系哦!

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