Python Turtle画奥运标志
Python Turtle画奥运标志
最近了解了Python Turtle,非常简单有趣。为了培养小朋友兴趣,写个turtle画奥运标志程序。
1. 画圆
turtle属于内置包,无需安装。只要导入即可以画图,下面先写几行代码画圆。
import turtlet = turtle.Pen()
t.circle(50)
t.getscreen()._root.mainloop()
导入turtle之后,创建Pen画笔t,避免后续代码。circle方法是画圆,最后是消息循环,让程序等待可以看到画图结果。
2. 画奥运标识
除了画圆方法,还有几个方法需要了解。
setposition # 设置位置,画布的中心位置是坐标0位置
penup() # 抬起笔,移动时不画。
pendown() # 落笔,开始画
请看下面代码,很容易理解:
import turtlet = turtle.Pen()
t.circle(50)t.penup()
t.setposition(-120, 0)
t.pendown()
t.circle(50)t.penup()
t.setposition(60, 60)
t.pendown()
t.circle(50)t.penup()
t.setposition(-60, 60)
t.pendown()
t.circle(50)t.penup()
t.setposition(-180, 60)
t.pendown()
t.circle(50)t.getscreen()._root.mainloop()
仅通过移动坐标就能达到目的,但这个代码不够精简,需要重构。
3. 重构代码
每次画圆,变化部分只有坐标,这里把坐标和半径抽取为参数,定义drawCircle方法,然后定义每个圆起始坐标并针对每个坐标调用drawCircle方法即可。请看代码:
import turtleclass DrawAoYun(turtle.Turtle):"""Draw Olympics logo"""def __init__(self):"""DrawAoYun Constructor"""turtle.Turtle.__init__(self, shape="turtle")def drawCircle(self, x, y, radius=50):"""Moves the turtle to the correct position and draws a circle"""self.penup()self.setposition(x, y)self.pendown()self.circle(radius)def drawOlympicSymbol(self):"""Iterates over a set of positions to draw the Olympics logo"""positions = [(0, 0), (-120, 0), (60, 60), (-60, 60), (-180, 60)]for pos in positions:self.drawCircle(pos[0], pos[1])if __name__ == "__main__":t = DrawAoYun()t.drawOlympicSymbol()turtle.getscreen()._root.mainloop()
这里定义类,继承turtle.Turtle,构造函数中调用父类__init__进行初始化,并设置画笔为乌龟样式。drawCircle方法定义画圆过程,位置和半径为参数,半径默认为50。drawOlympicSymbol方法先定义5个坐标列表,然后迭代调用drawCircle画圆,即完成了画奥运标识。
4. 美化标识
你可能觉得标识有点单调,没有颜色。我需要加上蓝色、黑色、红色和下面黄色和绿色,也要把画笔加粗点,最后在画上北京2008的文字。
import turtleclass DrawAoYun(turtle.Turtle):"""Draw Olympics logo"""def __init__(self):"""DrawAoYun Constructor"""turtle.Turtle.__init__(self, shape="turtle")self.width(5)def drawCircle(self, x, y, color,radius=50):"""Moves the turtle to the correct position and draws a circle"""self.penup()self.setposition(x, y)self.pendown()self.color(color)self.circle(radius)def drawOlympicSymbol(self):"""Iterates over a set of positions to draw the Olympics logo"""positions = [(0, 0, "green"), (-120, 0, "yellow"), (60, 60, "red"), (-60, 60, "black"), (-180, 60, "blue")]for x, y, color in positions:self.drawCircle(x, y, color)def drawText(self):"""Draw text to the screen"""self.penup()self.setposition(-120, 180)self.pendown()self.color("black")self.width(1)self.write("Beijing 2008", font=("Arial", 16, "bold"))if __name__ == "__main__":t = DrawAoYun()t.drawOlympicSymbol()t.drawText()turtle.getscreen()._root.mainloop()
构造函数中通过width方法设置为5。drawCircle方法增加颜色参数,每次画之前使用self.color(color)设置颜色。drawOlympicSymbol方法中给每个坐标增加颜色元素。
drawText方法通过write方法画文字,其他代码基本一样。
5. 总结
turtle非常简单吧,如果需要更深入了解或想画一些更漂亮、复杂的图形,参考官方文档。
Python Turtle画奥运标志相关推荐
- python turtle循环,python实现画循环圆 python turtle画4个同心圆方法
在python中如何使用循环结构画四个相切的圆 利用循环绘制复杂图形 python题? 循环画圆,圆的圆心在同一个圆上,同时用不同的渐进颜色 要用到三角函数算圆心 python怎么实现画圆功能 pyt ...
- python turtle画熊-Python turtle画图库画姓名实例
*****看一下我定义的change()和run()函数****** 绘图坐标体系: 作用:设置主窗体的大小和位置 ? 1 turtle.setup(width, height, startx, st ...
- python turtle画椭圆-【python turtle如何画椭圆】
python turtle画4个同心圆方法 from turtle import * def Circle(radius,angle,length): for i in range(length): ...
- 如何用python画一个心形图_求问怎样用python/python turtle画“心”
展开全部 python turtle画52614个同心圆方法 import turtle #draw first circle turtle.penup() turtle.goto(0,-200) t ...
- python turtle画房子详细解释_[宜配屋]听图阁
我就废话不多说了,直接上代码吧! import turtle t=turtle.Turtle() turtle.Turtle().screen.delay(0) tleft=turtle.Turtle ...
- python创意turtle作品大白-Python turtle 画个大白
今天用Python Turtle画个大白吧.效果图如下: Python画大白def bai(): #tracer(False) speed(0) pensize(5) color('black','w ...
- 利用python的turtle库绘制玫瑰的步骤_用python turtle画玫瑰
1.turtle 库 2.画玫瑰的代码: import turtle turtle.penup() turtle.left() turtle.fd() turtle.pendown() turtle. ...
- qpython3手机版turtle_使用Python turtle画一片树林
原标题:使用Python turtle画一片树林 使用Python Turtle绘制一片树林代码 这段代码结合了随机函数来使树林更多样化,同时设置了颜色的渐变.几乎每句都有注释,还有不懂的函数可以到官 ...
- Python turtle 画正多边形和多角形
Python turtle 画正多边形和多角形 作正多边形 作正多角形 计算内角 画图代码 作棱角分明的多角形 观察棱角分明的多角形 简洁的结论 代码 代码存在的问题 高斯与正十七边形 作出正十七角形 ...
- python用turtle画月亮_使用python turtle画高达
今天小编就为大家分享一篇使用python turtle画高达,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 我就废话不多说了,直接上代码吧! import turtle t=turt ...
最新文章
- Qt Creator检查代码语法
- Skype通讯协议分析
- P4619 [SDOI2018]旧试题
- 百度OCR文字识别API使用心得 com.baidu.ocr.sdk.exception.SDKError[283604]
- ubuntu下IP、DNS配置
- 牛客 奇怪的排序问题(单调栈/遍历)
- 答简书安卓公测群里朋友的提问
- 聚焦2017博鳌亚洲论坛:数据安全仍是互联网金融的重中之重
- Operations Manager 2007 R2系列之单台服务器拓扑图监控
- uniapp动态修改元素节点样式
- 前端特效——复杂下雪,雪花纷飞(纯css)
- 流量映射端口反弹(lcx,ew)
- 英文论文评审意见_艾德思:英文论文审稿意见模板
- AIDL中should be declared in a file错误
- 设计模式----装饰模式(JAVA)
- JS如何在高德地图多边形覆盖物填充平行折线的算法
- 熊猫直播:成也资本,败也资本
- 系统分析与设计--BCE详细设计和框架映射
- 基于springboot与layui的防疫物资管理管理系统
- 图像压缩 动态规划C语言实现