上图是 Turtle官方文档 中给出的例子,代码为:
1 2 3 4 5 6 7 8 9 10 |
from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done() |
下面我们简单做个关于Turtle的介绍。
在Python中有很多编写图形程序的方法,一个简单的启动图形化程序设计的方法是使用Python内嵌的Turtle模块。Turtle是Python内嵌的绘制线、圆以及其他形状(包括文本)的图形模块。它很容易学习并且使用简单。
一个Turtle实际上是一个对象,在导入Turtle模块时,就创建了对象,然后,可以调用Turtle对象的各种方法完成不同的操作。
当创建一个Turtle对象时,它的位置被设定在(0,0)处——窗口的中心,而且它的方向被设置为向右。Turtle模块用笔来绘制图形。默认情况下,笔是向下的(就像真实的笔尖触碰着一张纸)。如果笔是向下的,那么当移动Turtle的时候,它就会绘制出一条从当前位置到新位置的线。
下面两个表是控制笔的绘制状态的方法和移动Turtle的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import turtle # Table 1:Turtle Pen Drawing State Methods # Method Description turtle.pendown() # Pulls the pen down -- drawing when moving. turtle.penup() # Pulls the pen up -- no drawing when moving. turtle.pensize(width) # Sets the line thickness to the specified width. # Table 2:Turtle Mothon Methods # Method Description turtle.forward(d) # Moves the turtle forward by the specified distance in the direction the turtle is headed. turtle.backward(d) # Moves the turtle backward by the specified distance in the opposite direction the turtle is headed. The turtle's direction is not changed. turtle.right(angle) # Turns the turtle right by the specified angle. turtle.left(angle) # Turns the turtle left by the specified angle. turtle.goto(x,y) # Moves the turtle to an absolute position. turtle.setx(x) # Moves the turtle's x-coordinate to the specified position. turtle.sety(y) # Moves the turtle's y-coordinate to the specified position. turtle.setheading(angle) # Sets the orientation of the turtle to a specified angle. 0-East, 90-North, 180-West, 270-South. turtle.home() # Moves the turtle to the origin (0,0) and east direction. turtle.circle(r, ext, step) # Draws a circle with the specified radius, extent, and step. turtle.dot(diameter, color) # Draws a circle with the specified diameter and color. turtle.undo() # Undo(repeatedly) the last turtle action(s). turtle.speed(s) # Sets the turtle's speed to an integer between 1 and 10, with 10 being the fastest. |
circle方法有三个参数:radius是必需的,extent和step是可有可无的。extent是一个角度,它决定绘制圆的哪一部分。step决定使用的阶数。如果step是3/4/5/6……,那么circle方法将绘制一个里面包含被圆括住的的三边、四边、五边、六边或更多边形(即正三角形、正方形、五边形、六边形等)。如果不指定阶数,那么circle方法就只画一个圆。
下面表是Turtle笔的颜色、填充和绘制方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Table 3: Turtle Pen Color, Filling, and Drawing Methods # Method Description turtle.color(c) # Sets the pen color. turtle.fillcolor(c) # Stes the pen fill color. turtle.begin_fill() # Calls this method before filling a shape. turtle.end_fill() # Fills the shapes drawn before the last call to begin_fill. turtle.filling() # Returns the fill state: True if filling, False if not filling. function.clear() # Clears the window. The state and the position to the original default value. turtle.reset() # Clears the window and reset the state and position to the original default value. turtle.screensize(w, h) # Sets the width and height of the canvas. turtle.hideturtle() # Makes the turtle invisible. turtle.showturtle() # Makes the turtle visible. turtle.isbisible() # Returns the turtle visible. turtle.write(s, font=("Arial", 8, "normal"))# Writes the string s on the turtle position, Font is a triple consisting of fontname, fontsize, and fonttype. |
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import turtle turtle.pensize(3) turtle.penup() turtle.goto(-100, -50) turtle.pendown() turtle.begin_fill() turtle.color("red") turtle.circle(40, steps=3) turtle.end_fill() turtle.penup() turtle.goto(0, -50) turtle.pendown() turtle.begin_fill() turtle.color("yellow") turtle.circle(50) turtle.end_fill() turtle.penup() turtle.goto(100, -50) turtle.pendown() turtle.begin_fill() turtle.fillcolor("green") turtle.circle(40, steps=6) turtle.end_fill() turtle.penup() turtle.goto(-50, 100) turtle.pendown() turtle.color("blue") turtle.write("Colorful Shapes", font = ("Times", 18,"bold")) turtle.end_fill() turtle.hideturtle() turtle.done() |
输出: