(3) 全局控制命令:
命令说明turtle.clear() 清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset() 清空窗口,重置turtle状态为起始状态
turtle.undo() 撤销上一个turtle动作
turtle.isvisible() 返回当前turtle是否可见
stamp() 复制当前图形
turtle.write(s[,font=("font-name",font_size,"font_type")]) 写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型;font为可选项, font的参数也是可选项
3、命令详解
3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以给定半径画圆
参数:
radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)
举例:
circle(50) # 整圆;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圆
4、绘图举例
4.1长方形:
#导入turtle包的所有内容: from turtle import *#设置笔刷宽度: width(9) #前进: forward(300) #右转90度: right(90) #笔刷颜色: pencolor('red') forward(200) right(90) pencolor('green') forward(600) right(90) pencolor('blue') forward(200) right(90) pencolor('black') forward(300) right(90) #调用done()使得窗口等待被关闭,否则将立刻关闭窗口: done()
4.2 三个正方形:
import turtle turtle.screensize(canvwidth=None, canvheight=None, bg="blue") from turtle import * width(10) def drawSquare(sides,length): angle = 360/sides for again in range(sides): turtle.forward(length) turtle.right(angle) def moveTurtle(x,y): turtle.penup() turtle.goto(x,y) turtle.pendown() drawSquare(4,160) moveTurtle(200,200) drawSquare(4,160) moveTurtle(-200,200) drawSquare(4,160) turtle.done()
4.3 正弦波
import turtle import math # turtleSet function makes the code a bit more compact def turtleSet(name, shape, speed, color, pen, pos_x, pos_y): name.ht() name.up() name.shape(shape) name.speed(speed) name.color(color) name.pensize(pen) name.setposition(pos_x, pos_y) name.down() name.st() wn = turtle.Screen() background = turtle.Turtle() background.speed(0) background.up() background.goto(325, 325) background.down() background.right(90) background.fillcolor("red") background.begin_fill() for i in range(4): # Draw a 650 x 650 square background.forward(650) background.right(90) background.end_fill() height = 200 #amplitude period = 25 # Period, but without any specific unit. circle = turtle.Turtle() turtleSet(circle, 'circle', 10, "green", 2, height, 0) circle.left(90) sine = turtle.Turtle() turtleSet(sine, 'circle', 10, "black", 2, -325, 0) sine.st() sine.left(45) line = turtle.Turtle() turtleSet(line, 'circle', 10, "blue", 2, -286, 0) line.left(90) time = turtle.Turtle() turtleSet(time, 'arrow', 10, "yellow", 2, -325, 0) time.right(0) for i in range(650): x = height*math.cos(i/period) y = height*math.sin(i/period) sine.goto(i-325, y) circle.goto(x, y) line.goto(-286, y) time.goto(i-325, 0) wn.exitonclick()
4.3 鹦鹉螺