1. Basic Features
In the turtle graphics, we can write instructions to make a virtual (imagined) Turtles move back and forth on the screen. The turtle with a pen, we can let the turtle no matter where mobile use only the pen to draw lines. By writing code, moving turtle in a variety of cool patterns, we can draw a surprising picture. Using turtle graphics, we can not only just a few lines of code to create impressive visual effects, but also can follow the turtles to see how each line of code affect its movement. This can help us understand the logic of the code. So turtle graphics is also often used as a way of learning Python novice.
1.1 Turtle motion (motion control)
This section contains a number of common motion control functions
turtle.goto(x,y)
Positioning the pen coordinates (x, y)
turtle.forward(distance)
Movement in the positive direction distance long distance
turtle.backward(distance)
Long distance movement in the negative direction of distances
turtle.right(angle)
Right deviation angle degrees
turtle.left(angle)
Partial degree angle to the left
turtle.home()
back to the start
turtle.circle(radius, extent=None, steps=None)
Videos radius circle radius, extent circle angle
turtle.speed(speed)
To speed the speed of movement
To see so many functions must have a big head, we have the example to explain.
First, draw a square side length of 100, and then to 3/4 of a radius of the circle 50 drawn.
# Control the speed of the brush turtle.speed (. 5 ) # the brush is positioned to the origin turtle.goto (0,0) # starts at the origin, to draw a square with a side of 100 for I in Range (. 4 ): # Forward movement distance of 100 turtle.forward (100 ) # rightward bias 90 degrees turtle.right (90 ) # the brush is positioned to the origin turtle.home () # draw a radius of 100, representing 3/4 circle turtle.circle (50,270)
result:
1.2 Pen control (control brush)
This section contains the control function of the brush, used as follows:
turtle.pendown()
Put pen to paper, in this state will draw movement trajectory
turtle.pendown()
Started to write, in this state will not draw movement trajectory
turtle.pensize(width=None)
Brush thickness
turtle.pencolor(*args)
Brush Color
turtle.fillcolor(*args)
Fill Color
turtle.begin_fill()
Begins to fill
turtle.end_fill()
The end of the filling
turtle.write(arg, move=False, align=”left”, font=(“Arial”, 8, “normal”))
Write text
Similarly, we have to explain with an example.
Draw a square and fill, and finally write some text
# Control pen color turtle.pencolor ( ' Red ' ) # pen down turtle.pendown () # Set the fill color turtle.fillcolor ( ' Blue ' ) # begins to fill turtle.begin_fill () # from the origin, draw a side length square 100 for I in Range (. 4 ): # forward motion distance 100 turtle.forward (200 is ) # rightward bias 90 degrees turtle.right (90 ) # end of the filling turtle.end_fill () turtle.penup () Turtle .goto ( 100, -100 ) turtle.write (' Crossin programming classroom ' )
The final result is this
At the same time, we can also set the brush playing weight, speed and other attributes of the brush,
1.3 Window control (Windows Control)
There are two commonly used functions
turtle.bgcolor(*args)
Set the background color
turtle.bgpic(picname=None)
Filling background image
setting code is as follows
turtle.bgcolor('red') turtle.bgpic(r'yourpic.png')
Example 2. Drawing Turtle
We explain turtle drawing with a few simple examples of usage.
2.1 circle with a square
import turtle for i in range(360): turtle.setheading(i) for i in range(4): turtle.forward(100) turtle.left(90)
360 squares arranged at intervals of 1 degree, a few lines of code can generate a beautiful pattern rule.
2.2 red five-pointed star
Use fill function to draw a big red star
import turtle turtle.color('red','red') turtle.begin_fill() for i in range(5): turtle.forward(100) turtle.right(144) turtle.end_fill()
结果如图:
动态时钟
# coding=utf-8 import turtle from datetime import * # 抬起画笔,向前运动一段距离放下 def Skip(step): turtle.penup() turtle.forward(step) turtle.pendown() def mkHand(name, length): # 注册Turtle形状,建立表针Turtle turtle.reset() Skip(-length * 0.1) # 开始记录多边形的顶点。当前的乌龟位置是多边形的第一个顶点。 turtle.begin_poly() turtle.forward(length * 1.1) # 停止记录多边形的顶点。当前的乌龟位置是多边形的最后一个顶点。将与第一个顶点相连。 turtle.end_poly() # 返回最后记录的多边形。 handForm = turtle.get_poly() turtle.register_shape(name, handForm) def Init(): global secHand, minHand, hurHand, printer # 重置Turtle指向北 turtle.mode("logo") # 建立三个表针Turtle并初始化 mkHand("secHand", 135) mkHand("minHand", 125) mkHand("hurHand", 90) secHand = turtle.Turtle() secHand.shape("secHand") minHand = turtle.Turtle() minHand.shape("minHand") hurHand = turtle.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 3) hand.speed(0) # 建立输出文字Turtle printer = turtle.Turtle() # 隐藏画笔的turtle形状 printer.hideturtle() printer.penup() def SetupClock(radius): # 建立表的外框 turtle.reset() turtle.pensize(7) for i in range(60): Skip(radius) if i % 5 == 0: turtle.forward(20) Skip(-radius - 20) Skip(radius + 20) if i == 0: turtle.write(int(12), align="center", font=("Courier", 14, "bold")) elif i == 30: Skip(25) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-25) elif (i == 25 or i == 35): Skip(20) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-20) else: turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-radius - 20) else: turtle.dot(5) Skip(-radius) turtle.right(6) def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] def Date(t): y = t.year m = t.month d = t.day return "%s %d%d" % (y, m, d) def Tick(): # 绘制表针的动态显示 t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.setheading(6 * second) minHand.setheading(6 * minute) hurHand.setheading(30 * hour) turtle.tracer(False) printer.forward(65) printer.write(Week(t), align="center", font=("Courier", 14, "bold")) printer.back(130) printer.write(Date(t), align="center", font=("Courier " , 14, " Bold " )) printer.home () turtle.tracer (True) # after 100ms continue to call tick turtle.ontimer (Tick, 100 ) DEF main (): # Open / Close turtle animation, and update drawing set delay. turtle.tracer (False) the Init () SetupClock ( 160. ) turtle.tracer (True) the Tick () turtle.mainloop () IF the __name__ == " __main__ " : main ()
The result is a dynamic clock, only a screenshot of the next FIG.