Python Turtle画奥运标志
责任编辑:哆啦俊俊2     时间:2021-07-25     来源:转载于CSDN
责任编辑:哆啦俊俊2
时间:2021-07-25  来源:转载于CSDN
分类: 观点评述
浏览量: 328

Python Turtle画奥运标志

最近了解了Python Turtle,非常简单有趣。为了培养小朋友兴趣,写个turtle画奥运标志程序。

1. 画圆

turtle属于内置包,无需安装。只要导入即可以画图,下面先写几行代码画圆。

import turtle 

t = turtle.Pen()

 t.circle(50) 

t.getscreen()._root.mainloop()

导入turtle之后,创建Pen画笔t,避免后续代码。circle方法是画圆,最后是消息循环,让程序等待可以看到画图结果。

2. 画奥运标识

除了画圆方法,还有几个方法需要了解。
setposition # 设置位置,画布的中心位置是坐标0位置
penup() # 抬起笔,移动时不画。
pendown() # 落笔,开始画

请看下面代码,很容易理解:

import turtle

t = 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 turtle

 

class 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 turtle

class 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非常简单吧,如果需要更深入了解或想画一些更漂亮、复杂的图形,参考官方文档。

 


来源:转载于CSDN

点赞人: 王王木木  哆啦俊俊2 

哆啦俊俊2  回复 2021-07-27 23:31:33
会画了!
哆啦俊俊2  回复 2021-07-25 23:37:59
好评!
回复:

Copyright © 2021 .长沙麦涛网络科技有限公司 All rights reserved. 湘ICP备20015126号-2
联系我们