turtle:海龟先生的意思。 Python老是用动物的名字!
首先要引入呀!
不引入当然是不可以用的呀!
Python学习交流群:1007333442,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。
turtle.forward(100):效果图
鼠标前进100步!
后退的话,鼠标就又回来啦!
当然也可以向右向左!鼠标会改变角度!
向右旋转多少度,向左旋转多少度呀之类的!
turtle.left(90):这一下,又旋转回来啦!
鼠标走过的途径不会擦除,会一直保留着!
画出了一个矩形,这是通过变量来控制的。也可以通过循环来控制!
import turtle i = 0 a = 6 b = 45 while True: if i > 33: break turtle.forward(a) turtle.right(b) turtle.forward(a) turtle.left(b) i+=1
来画一个螺旋形状的图形!
改变一下角度就好啦!
import turtle i = 0 a = 6 b = 90 while True: if i > 12: break turtle.forward(a) turtle.right(b) turtle.forward(a) i+=1 a += 10
画出圆:
利用距离,角度。一次增加1.来实现!
import turtle i = 0 while i < 360: turtle.forward(1) turtle.right(1) i += 1
几条边组成的圆,你来控制!
代码:
import turtle i = int(input('你想要几边来密封呢?')) angle = 360.0 / i distance = 1000.0 / i for s in range(i): turtle.forward(distance) turtle.right(angle) turtle.done()
注意,数字格式:
i需要是int类型数据。要不然无法转换!
除数最后得到的是浮点数!
- i = int(input('你想要几边来密封呢?'))
- angle = 360.0 / i
- distance = 1000.0 / i
进行颜色填充:1
目前,我掌握的所填充的必须是一个封闭图形。一下子出来完的那种!
代码:
import turtle i = int(input('你想要几边来密封呢?')) angle = 360.0 / i distance = 1000.0 / i turtle.begin_fill() turtle.color("yellow") turtle.circle(distance,steps=i) turtle.end_fill() turtle.done()
也可以通过调用函数来画图形!
代码:
import turtle def drawShape(sides,length): angle = 360.0/sides for side in range(sides): turtle.forward(length) turtle.right(angle) def moveTurtle(x,y): turtle.penup() turtle.goto(x,y) turtle.pendown() drawShape(4,60) moveTurtle(110,30) drawShape(3,90) turtle.done()
随机生成图像:
代码:
shape决定绘制图形形状:
- 矩形
- 三角形
- 圆
import turtle import random def drawShape(sides,length): angle = 360.0/sides for side in range(sides): turtle.forward(length) turtle.right(angle) def moveTurtle(x,y): turtle.penup() turtle.goto(x,y) turtle.pendown() def drawSquare(length): drawShape(4,length) def drawTriangle(length): drawShape(3,length) def drawCircle(length): drawShape(360,length) def drawRandom(): x = random.randrange(-200,200) y = random.randrange(-200,200) length = random.randrange(75) shape = random.randrange(1,4) moveTurtle(x,y) if shape == 1: drawSquare(length) elif shape == 2: drawTriangle(length) elif shape == 3: length = length % 4 drawCircle(length) for shape in range(100): drawRandom() turtle.done()