Copyright: please leave a message like Give me praise problematic -------------------------------- will be updated from time to time , because learning, so happy, because the share, so convenient! Reprint please indicate the source, ha ha! https://blog.csdn.net/Appleyk/article/details/86410871
Out of boredom and fun, the installation of turtle module, p3 installation may be a problem, as follows :
Solution:
Instructions on turtle module functions, a lot of online search, the following comments are very detailed, I will not say more, go directly demo below:
#!/usr/bin/env Python3
# -*- encoding:utf-8 -*-
import turtle,random
'''
获取随机颜色RGB代码
'''
def getRandomRGB():
colorArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
color = ""
for i in range(6):
color += colorArr[random.randint(0,14)]
return "#"+color
if __name__ =="__main__":
tObj = turtle.Turtle()
#设置小乌龟爬的速度,0最快,1最慢,范围[0,10]
tObj.speed(0)
#小乌龟每次绘图时,头偏移的角度,请选择能被360整除的数字
angle = 2
#小乌龟绘制一个圆形,总过需要绘制多少次
count = 360//angle
for i in range(count):
#设置乌龟画笔的颜色,这里采用随机色
tObj.pencolor(getRandomRGB())
#当前角度向前移动100像素
tObj.forward(100)
#当前角度向右旋转30度 == 小乌龟的头向右偏30°
tObj.right(30)
#此时,小乌龟的头已经偏向右边了,然后我们再让小乌龟向前移动20像素
tObj.forward(40)
#然后将小乌龟的头,向左再偏60°,算上之前的向右偏了30°,等于小乌龟相对于最开始的角度向左偏了30°
tObj.left(60)
#向前移动50,右移+左移 ==> 构成了一个折线
tObj.forward(50)
#此时,停止移动,将小乌龟的头再偏回来,也就是最开始的角度,算一下,其实就是向右偏30°
tObj.right(30)
#抬起画笔,停止画
tObj.penup()
#移动了那么多,我们让小乌龟回到起点位置
tObj.setposition(0,0)
#落下画笔,准备画
tObj.pendown()
#再次转动小乌龟的头,我们选择逆时针旋转angle°,不然小乌龟总是会朝着同一个方向绘制我们的图像
tObj.left(angle)
#小乌龟完成使命
turtle.done()
What is said to be a small turtle, in fact, not in the code inside, it is an object in memory, which, it is an address, the final effect is as follows:
If we want to know the turtle function of specific instructions, we can see the source code, for example, the method forward:
final effect:
See the little turtles yet, it is that the figure above the arrow, ha ha