turtle画哆啦A梦(不止一个哦 多个有趣的画)
有趣的turtle库
- 前言
- 彩色螺旋线
- 效果
- 太阳花
- 效果
- 国旗
- 效果
- 玫瑰花
- 效果
- 彩色树
- 效果
- 随机樱花树
- 效果
- 表白树
- 效果
- 圆舞曲
- 效果
- 哆啦A梦
- 效果
- 时钟
- 效果
- 我的Python版本
前言
听说有人找画哆啦A梦的源码。分享python turtle库 几个有趣的源码
彩色螺旋线
from turtle import *
speed(9) # 画笔速度
pensize(2) # 画笔的宽度
bgcolor("black") # 画布背景色
colors = ["red","yellow","purple","blue"] # 定义画笔线色
for x in range(400): # 循环一次 画一条线 forward(2*x) # 向当前方向前进n像素color(colors[x % 4]) # 根据求余 调整画笔线色left(91) # 向左旋转91度mainloop()
效果
太阳花
import turtle
turtle=turtle.Turtle()
screen=turtle.getscreen()
turtle.color('red', 'yellow')
turtle.begin_fill()
for i in range(50):turtle.forward(200)turtle.left(170)
turtle.end_fill()
turtle.done()
效果
国旗
from turtle import *screensize(2000, 2000, 'white') # 设置画布大小
speed(9)
# 绘制旗面
pencolor('red')
# pu()
goto(-300, -200)
pd()
fillcolor('red')
begin_fill()
for i in range(0, 2):fd(600)lt(90)fd(400)lt(90)
end_fill()# 绘制大五角星
pu()
pencolor('yellow')
goto(-260, 120)
pd()
fillcolor('yellow')
begin_fill()
for i in range(0, 5):fd(113.137) # 大星一划的边长rt(144)
end_fill()# 绘制四个小五角星
list1 = [(-100, 160), (-60, 120), (-60, 60), (-100, 20)] # 四个五角星的中心坐标
list2 = [31.98, 8.13, -15.59, -38.66] # 相对角度0的后退1.111需要左转的角度for j in range(0, 4):seth(0) # 这是龟头角度为0pu()goto(list1[j]) # 定位到五角星中心lt(list2[j]) # 旋转角度,以背向指向大五角星的角尖bk(20) # 从五角星中心到指向大五角星的角尖(龟倒着爬)退一个小圆半径lt(18) # 五角星的半角角度pd()begin_fill()for i in range(0, 5):fd(113.137 / 3) # 小星一划的边长rt(144)end_fill()
pu()
ht()
done()
效果
玫瑰花
import turtle
import time
turtle.speed(5)
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill() # 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40) # 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83) # 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80) # 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,60)
turtle.done()
效果
彩色树
# 这个比较复杂,画的时间较长from turtle import *
# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)
penup()
bk(l)
pendown()
fd(l)
def draw_tree(l, level):global r, g, b# save the current pen widthw = width()# narrow the pen widthwidth(w * 3.0 / 4.0)# set color:r = r + 1g = g + 2b = b + 3pencolor(r % 200, g % 200, b % 200)l = 3.0 / 4.0 * llt(s)fd(l)if level < lv:draw_tree(l, level + 1)bk(l)rt(2 * s)fd(l)if level < lv:draw_tree(l, level + 1)bk(l)lt(s)# restore the previous pen widthwidth(w)
speed("fastest")
draw_tree(l, 4)
done()
效果
随机樱花树
# 每次运行 树的形状是随机的
import turtle as T
import random
import time# 画樱花的躯干(60,t)
def Tree(branch, t):time.sleep(0.0005)if branch > 3:if 8 <= branch <= 12:if random.randint(0, 2) == 0:t.color('snow') # 白else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 3)elif branch < 8:if random.randint(0, 1) == 0:t.color('snow')else:t.color('lightcoral') # 淡珊瑚色t.pensize(branch / 2)else:t.color('sienna') # 赭(zhě)色t.pensize(branch / 10) # 6t.forward(branch)a = 1.5 * random.random()t.right(20 * a)b = 1.5 * random.random()Tree(branch - 10 * b, t)t.left(40 * a)Tree(branch - 10 * b, t)t.right(20 * a)t.up()t.backward(branch)t.down()# 掉落的花瓣
def Petal(m, t):for i in range(m):a = 200 - 400 * random.random()b = 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color('lightcoral') # 淡珊瑚色t.circle(1)t.up()t.backward(a)t.right(90)t.backward(b)# 绘图区域
t = T.Turtle()
# 画布大小
w = T.Screen()
t.hideturtle() # 隐藏画笔
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat') # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()
T.done()
效果
表白树
import random
import turtle
def love(x, y): # 在(x,y)处画爱心lalalalv = turtle.Turtle()lv.hideturtle()lv.up()lv.goto(x, y) # 定位到(x,y)def curvemove(): # 画圆弧for i in range(20):lv.right(10)lv.forward(2)lv.color('red', 'pink')lv.speed(0)lv.pensize(1)# 开始画爱心lalalalv.down()lv.begin_fill()lv.left(140)lv.forward(22)curvemove()lv.left(120)curvemove()lv.forward(22)lv.write("{}".format("他-她"), font=("Arial", 10, "normal"), align="center")lv.left(140) # 画完复位lv.end_fill()def tree(branchLen, t):if branchLen > 5: # 剩余树枝太少要结束递归if branchLen < 20: # 如果树枝剩余长度较短则变绿t.color("green")t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)love(t.xcor(), t.ycor()) # 传输现在turtle的坐标t.up()t.backward(branchLen)t.color("brown")returnt.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))t.down()t.forward(branchLen)# 以下递归ang = random.uniform(15, 45)t.right(ang)tree(branchLen - random.uniform(12, 16), t) # 随机决定减小长度t.left(2 * ang)tree(branchLen - random.uniform(12, 16), t) # 随机决定减小长度t.right(ang)t.up()t.backward(branchLen)def Fonts():t.penup()t.goto(-500, -300)t.pencolor('black')t.write("余生的快乐希望与你一起!^_^", font=('方正行黑简体', 30, 'normal'))
myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
Fonts()
myWin.exitonclick()
效果
来自 王小王-123
圆舞曲
from turtle import *def stop():global runningrunning = Falsedef main():global runningclearscreen()bgcolor("gray10")tracer(False)shape("triangle")f = 0.793402phi = 9.064678s = 5c = 1# create compound shapesh = Shape("compound")for i in range(10):shapesize(s)p =get_shapepoly()s *= fc *= ftilt(-phi)sh.addcomponent(p, (c, 0.25, 1-c), "black")register_shape("multitri", sh)# create dancersshapesize(1)shape("multitri")pu()setpos(0, -200)dancers = []for i in range(180):fd(7)tilt(-4)lt(2)update()if i % 12 == 0:dancers.append(clone())home()# dancerunning = Trueonkeypress(stop)listen()cs = 1while running:ta = -4for dancer in dancers:dancer.fd(7)dancer.lt(2)dancer.tilt(ta)ta = -4 if ta > 0 else 2if cs < 180:right(4)shapesize(cs)cs *= 1.005update()return "DONE!"if __name__=='__main__':print(main())mainloop()
效果
哆啦A梦
import turtledef flyTo(x, y):turtle.penup()turtle.goto(x, y)turtle.pendown()
def drawEye():turtle.tracer(False)a = 2.5for i in range(120):if 0 <= i < 30 or 60 <= i < 90:a -= 0.05else:a += 0.05turtle.left(3)turtle.fd(a)turtle.tracer(True)
def beard():""" 画胡子, 一共六根"""# 左边第一根胡子flyTo(-37, 135)turtle.seth(165)turtle.fd(60)# 左边第二根胡子flyTo(-37, 125)turtle.seth(180)turtle.fd(60)# 左边第三根胡子flyTo(-37, 115)turtle.seth(193)turtle.fd(60)# 右边第一根胡子flyTo(37, 135)turtle.seth(15)turtle.fd(60)# 右边第二根胡子flyTo(37, 125)turtle.seth(0)turtle.fd(60)# 右边第三根胡子flyTo(37, 115)turtle.seth(-13)turtle.fd(60)
def drawRedScarf():""" 画围巾"""turtle.fillcolor("red") # 填充颜色turtle.begin_fill()turtle.seth(0) # 朝向右turtle.fd(200) # 前进10个单位turtle.circle(-5, 90)turtle.fd(10)turtle.circle(-5, 90)turtle.fd(207)turtle.circle(-5, 90)turtle.fd(10)turtle.circle(-5, 90)turtle.end_fill()
def drawMouse():flyTo(5, 148)turtle.seth(270)turtle.fd(100)turtle.seth(0)turtle.circle(120, 50)turtle.seth(230)turtle.circle(-120, 100)
def drawRedNose():flyTo(-10, 158)turtle.fillcolor("red") # 填充颜色turtle.begin_fill()turtle.circle(20)turtle.end_fill()
def drawBlackdrawEye():turtle.seth(0)flyTo(-20, 195)turtle.fillcolor("#000000") # 填充颜色turtle.begin_fill()turtle.circle(13)turtle.end_fill()turtle.pensize(6)flyTo(20, 205)turtle.seth(75)turtle.circle(-10, 150)turtle.pensize(3)flyTo(-17, 200)turtle.seth(0)turtle.fillcolor("#ffffff")turtle.begin_fill()turtle.circle(5)turtle.end_fill()flyTo(0, 0)
def drawFace():turtle.forward(183) # 前行183个单位turtle.fillcolor("white") # 填充颜色为白色turtle.begin_fill() # 开始填充turtle.left(45) # 左转45度turtle.circle(120, 100) # 右边那半边脸turtle.seth(90) # 朝向向上drawEye() # 画右眼睛turtle.seth(180) # 朝向左turtle.penup() # 抬笔turtle.fd(60) # 前行60turtle.pendown() # 落笔turtle.seth(90) # 朝向上drawEye() # 画左眼睛turtle.penup() # 抬笔turtle.seth(180) # 朝向左turtle.fd(64) # 前进64turtle.pendown() # 落笔turtle.seth(215) # 修改朝向turtle.circle(120, 100) # 左边那半边脸turtle.end_fill() #
def drawHead():""" 画了一个被切掉下半部分的圆"""turtle.penup() # 抬笔turtle.circle(150, 40) # 画圆, 半径150,圆周角40turtle.pendown() # 落笔turtle.fillcolor("#00a0de") # 填充色turtle.begin_fill() # 开始填充turtle.circle(150, 280) # 画圆,半径150, 圆周角280turtle.end_fill()
def drawAll():drawHead()drawRedScarf()drawFace()drawRedNose()drawMouse()beard()flyTo(0, 0)turtle.seth(0)turtle.penup()turtle.circle(150, 50)turtle.pendown()turtle.seth(30)turtle.fd(40)turtle.seth(70)turtle.circle(-30, 270)turtle.fillcolor("#00a0de")turtle.begin_fill()turtle.seth(230)turtle.fd(80)turtle.seth(90)turtle.circle(1000, 1)turtle.seth(-89)turtle.circle(-1000, 10)turtle.seth(180)turtle.fd(70)turtle.seth(90)turtle.circle(30, 180)turtle.seth(180)turtle.fd(70)turtle.seth(100)turtle.circle(-1000, 9)turtle.seth(-86)turtle.circle(1000, 2)turtle.seth(230)turtle.fd(40)turtle.circle(-30, 230)turtle.seth(45)turtle.fd(81)turtle.seth(0)turtle.fd(203)turtle.circle(5, 90)turtle.fd(10)turtle.circle(5, 90)turtle.fd(7)turtle.seth(40)turtle.circle(150, 10)turtle.seth(30)turtle.fd(40)turtle.end_fill()# 左手turtle.seth(70)turtle.fillcolor("#FFFFFF")turtle.begin_fill()turtle.circle(-30)turtle.end_fill()# 脚flyTo(103.74, -182.59)turtle.seth(0)turtle.fillcolor("#FFFFFF")turtle.begin_fill()turtle.fd(15)turtle.circle(-15, 180)turtle.fd(90)turtle.circle(-15, 180)turtle.fd(10)turtle.end_fill()flyTo(-96.26, -182.59)turtle.seth(180)turtle.fillcolor("#FFFFFF")turtle.begin_fill()turtle.fd(15)turtle.circle(15, 180)turtle.fd(90)turtle.circle(15, 180)turtle.fd(10)turtle.end_fill()# 右手flyTo(-133.97, -91.81)turtle.seth(50)turtle.fillcolor("#FFFFFF")turtle.begin_fill()turtle.circle(30)turtle.end_fill()# 口袋flyTo(-103.42, 15.09)turtle.seth(0)turtle.fd(38)turtle.seth(230)turtle.begin_fill()turtle.circle(90, 260)turtle.end_fill()flyTo(5, -40)turtle.seth(0)turtle.fd(70)turtle.seth(-90)turtle.circle(-70, 180)turtle.seth(0)turtle.fd(70)# 铃铛flyTo(-103.42, 15.09)turtle.fd(90)turtle.seth(70)turtle.fillcolor("#ffd200")turtle.begin_fill()turtle.circle(-20)turtle.end_fill()turtle.seth(170)turtle.fillcolor("#ffd200")turtle.begin_fill()turtle.circle(-2, 180)turtle.seth(10)turtle.circle(-100, 22)turtle.circle(-2, 180)turtle.seth(180 - 10)turtle.circle(100, 22)turtle.end_fill()flyTo(-13.42, 15.09)turtle.seth(250)turtle.circle(20, 110)turtle.seth(90)turtle.fd(15)turtle.dot(10)flyTo(0, -150)drawBlackdrawEye()
def main():turtle.screensize(800, 6000, "#F0F0F0")turtle.pensize(3)turtle.speed(9)drawAll()
if __name__ == "__main__":main()turtle.mainloop()
效果
时钟
import turtle
from datetime import *# 抬起画笔,向前运动一段距离放下
def Skip(step):turtle.penup()turtle.forward(step)turtle.pendown()def mkHand(name, length):# 注册Turtle形状,建立表针Turtleturtle.reset()Skip(-length * 0.1)# 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。turtle.begin_poly()turtle.forward(length * 1.1)# 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。turtle.end_poly()# 返回最后记录的多边形。handForm = turtle.get_poly()turtle.register_shape(name, handForm)def Init():global secHand, minHand, hurHand, printer# 重置Turtle指向北turtle.mode("logo")# 建立三个表针Turtle并初始化mkHand("secHand", 135)mkHand("minHand", 125)mkHand("hurHand", 90)secHand = turtle.Turtle()secHand.shape("secHand")minHand = turtle.Turtle()minHand.shape("minHand")hurHand = turtle.Turtle()hurHand.shape("hurHand")for hand in secHand, minHand, hurHand:hand.shapesize(1, 1, 3)hand.speed(0)# 建立输出文字Turtleprinter = turtle.Turtle()# 隐藏画笔的turtle形状printer.hideturtle()printer.penup()def SetupClock(radius):# 建立表的外框turtle.reset()turtle.pensize(7)for i in range(60):Skip(radius)if i % 5 == 0:turtle.forward(20)Skip(-radius - 20)Skip(radius + 20)if i == 0:turtle.write(int(12), align="center", font=("Courier", 14, "bold"))elif i == 30:Skip(25)turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))Skip(-25)elif (i == 25 or i == 35):Skip(20)turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))Skip(-20)else:turtle.write(int(i / 5), align="center", font=("Courier", 14, "bold"))Skip(-radius - 20)else:turtle.dot(5)Skip(-radius)turtle.right(6)def Week(t):week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]return week[t.weekday()]def Date(t):y = t.yearm = t.monthd = t.dayreturn "%s-%d-%d" % (y, m, d)def Tick():# 绘制表针的动态显示t = datetime.today()second = t.second + t.microsecond * 0.000001minute = t.minute + second / 60.0hour = t.hour + minute / 60.0secHand.setheading(6 * second)minHand.setheading(6 * minute)hurHand.setheading(30 * hour)turtle.tracer(False)printer.forward(65)printer.write(Week(t), align="center", font=("Courier", 14, "bold"))printer.back(130)printer.write(Date(t), align="center", font=("Courier", 14, "bold"))printer.home()turtle.tracer(True)# 100ms后继续调用tickturtle.ontimer(Tick, 100)def main():# 打开/关闭龟动画,并为更新图纸设置延迟。turtle.tracer(False)Init()SetupClock(160)turtle.tracer(True)Tick()turtle.done()if __name__ == "__main__":main()
效果
我的Python版本
以上代码均在 Python 3.5.2 运行
来一波 评论 点赞 收藏
turtle画哆啦A梦(不止一个哦 多个有趣的画)相关推荐
- 用python画多来a梦-使用Python的Turtle绘制哆啦A梦实例
这是我几年前为了练习python的turtle库而画的,今天翻出了代码,分享给大家. 这是我初学python时画的,当时还没有面向对象的概念,也没有采取类方法之类,纯原始手工,供大家参考. 若有兴趣可 ...
- 用python画哆啦a梦的身体_每天一个Python小技巧,用Python 画个多啦A梦,小猪佩奇,文末还有Python入门学习视频...
见网络上有人用Python 画出来个多啦A梦,很是新奇,来来来,我们看一下他们主要用到的库. 其实主要用的库就一个 turtle 库 先说明一下turtle绘图的基础知识: 1. 画布(canvas) ...
- python画图哆啦a梦-使用Python的Turtle绘制哆啦A梦实例
这是我几年前为了练习python的turtle库而画的,今天翻出了代码,分享给大家. 这是我初学python时画的,当时还没有面向对象的概念,也没有采取类方法之类,纯原始手工,供大家参考. 若有兴趣可 ...
- python编程100例海绵宝宝-用python画哆啦a梦
广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 也收到了读者想用 python 画各种图的各种需求. 和一些读者沟通后才 ...
- 哆啦a梦简单图画python编程_[python]画哆啦A梦,Python,绘制
from turtle import * #无轨迹跳跃 defmy_goto(x, y): penup() goto(x, y) pendown()#眼睛 defeyes(): fillcolor(& ...
- 多啦a梦的python代码_分享一篇文本文档画哆啦A梦代码!
#encoding=utf-8 #Drawcat 画多啦A梦 from turtle import * # 无轨迹跳跃 def my_goto(x, y): penup() goto(x, y) pe ...
- 用java画哆啦a梦,来画哆啦A梦吧!
import turtle 创建哆啦A梦 doraemon = turtle.Turtle() doraemon.speed(10) def draw_eye_white_circle(x): dor ...
- 用Python画哆啦A梦
from turtle import *# 无轨迹跳跃 def my_goto(x, y):penup()goto(x, y)pendown()# 眼睛 def eyes():fillcolor(&q ...
- 用python画哆啦a梦的头,用python画多来a梦-【Python】绘制哆啦A梦
from turtle import * #无轨迹跳跃 defmy_goto(x, y): penup() goto(x, y) pendown()#眼睛 defeyes(): fillcolor(& ...
- Css3之画哆啦A梦
最近学习完css3画图,然后就百度找了看有没有好的练手机会,看到别人博客上面有画哆啦A梦,还有小黄人,就自己看了哆啦A梦的来巩固下Css3的知识,先看看效果图 我们把这个图拆分开来,分为上下两部分,上 ...
最新文章
- SQLServer 扫盲
- python数据分析从入门到精通电子工业出版社_荐书丨Python数据分析从入门到精通...
- keras 导出onnx并使用
- Swift的控制转移语句-- fallthrough语句
- java:BufferedReader接受输入进来的2个数字,并将它们相加
- 全连接层 时间复杂度_神经网络全连接层(3)
- 真香定律!Android动态换肤实现原理解析,吐血整理
- ie浏览器模拟器_航空飞机模拟器安卓版下载-航空飞机模拟器游戏下载
- java 堆_JAVA学习笔记 07——堆、栈和垃圾回收机制
- 服务器在线看视频无法播放,上传到服务器的视频不能在线播放怎么办?
- 网络协议分析(Network Protocol Analysis)之IPCP、认证协议PAP与CHAP
- Spark Row对象入门到熟悉
- mysql+cls()_MySQL性能优化之show processlist(一)
- R_ggplot2基础(三)
- OmegaXYZ知识图谱应用Github仓库(长期更新)
- 从应用层修改系统日期和时间
- 用PHP实现手机对jar,jad文件的下载(转)
- 遥感基础——红外波段分类
- 程序员学数学读哪本书?
- 我的世界中国版服务器家园系统,《我的世界》中国版“暑期更新”上线 家园系统休闲玩法亮点...