The "Little Turtle" turtle is a popular drawing function library in the Python language, imagine a small turtle, starting at a coordinate system origin with a horizontal axis of x, Y, and (0,0), moving in the plane coordinate system based on the control of a set of function directives, Thus drawing a graph on the path it crawls.
Basic knowledge of turtle drawing:
1. Canvases (canvas)
The canvas is turtle for us to expand for the drawing area, and we can set its size and initial position.
Set the canvas size
Turtle.screensize (Canvwidth=none,canvheight=none,bg=none), the parameters are the width of the canvas (in pixels), the height, the background color.
such as: Turtle.screensize (800,600, "green")
Turtle.screensize () #返回默认大小 (400,300)
Turtle.setup (width=0.5,height=0.75,startx=none,starty=none), parameter: width,height: When the input width and height are integers, the pixels are represented, and when they are decimals, the proportions that occupy the computer screen ( Startx,starty): This coordinates the position of the vertex in the upper-left corner of the rectangle window, and if it is empty, the window is centered on the screen.
such as: Turtle.setup (width=0.6,height=0.6)
Turtle.setup (width=800,height=800,startx=100,starty=100)
2. Brushes
2.1 State of the brush
On the canvas, there is a default coordinate that is the axis of the center of the canvas, with one face facing the x-axis to the small turtle on the origin of the coordinates. Here we describe the little turtle using two words: coordinates origin (position), facing the x-axis positive direction (direction), turtle drawing, is to use the position direction to describe the state of the Small turtle (brush).
2.2 Properties of brushes
Brushes (Properties of the brush, color, width of the drawing line, etc.)
1) turtle.pensize (): Sets the width of the brush;
2) Turtle.pencolor (): No parameters passed in, return the current brush color, incoming parameters set the brush color, can be a string such as "green", "red", can also be a RGB3 tuple.
3) Turtle.speed: Sets the brush movement speed, the brush draws the speed range [0,∞] integer, the larger the faster the number. The current version is not as far as the outside said only 0 to 10, pro-Test 100,300 can be.
2.3 Drawing Commands
Manipulating turtle drawings There are a number of commands that can be divided into 3 types: one for motion commands, one for brush control, and one for global control commands.
(1) Brush Motion command
Command |
Description |
Turtle.forward (distance) |
Move distance pixel length to the current brush direction |
Turtle.backward (distance) |
Moves the distance pixel length in the opposite direction to the current brush |
Turtle.right (degree) |
Move Clockwise degree° |
Turtle.left (degree) |
Move degree° Counterclockwise |
Turtle.pendown () |
Draws a graphic when it is moved, and is drawn by default |
Turtle.goto (x, y) |
Move the brush to the location where coordinates are X, y |
Turtle.penup () |
Lift pen move, no drawing, for another place to draw |
Turtle.circle () |
Draw a circle with a positive radius (negative), indicating that the center of the circle is on the left (right) of the brush |
SETX () |
Move the current x axis to the specified position |
Sety () |
Moves the current y-axis to the specified position |
Setheading (angle) |
Sets the current orientation to angle angle |
Home () |
Sets the current brush position as the origin, toward the east. |
Dot (r) |
Draws a dot that specifies the diameter and color |
(2) Brush control commands
Command |
Description |
Turtle.fillcolor (colorstring) |
Draw the fill color of a graphic |
Turtle.color (Color1, Color2) |
Set Pencolor=color1 at the same time, Fillcolor=color2 |
Turtle.filling () |
Returns whether the current is in the fill state |
Turtle.begin_fill () |
Prepare to begin filling the drawing |
Turtle.end_fill () |
Fill complete |
Turtle.hideturtle () |
Hide the turtle shape of a brush |
Turtle.showturtle () |
Show the turtle shape of a brush |
(3) Global control commands
Command |
Description |
Turtle.clear () |
Clears the Turtle window, but the position and state of the turtle do not change |
Turtle.reset () |
Clears the window, resets the turtle state to the start state |
Turtle.undo () |
Undo last Turtle Action |
Turtle.isvisible () |
Returns whether the current turtle is visible |
Stamp () |
Copy the current drawing |
Turtle.write (s [, font= ("Font-name", Font_size, "Font_type")]) |
Write text, S is the text content, font is the parameters of the fonts, respectively, the font name, size and type; font is optional, and the font parameter is optional |
(4) Other orders
Command |
Description |
Turtle.mainloop () or Turtle.done () |
Start Event Loop-invokes Tkinter's Mainloop function. Must be the last statement in the Turtle graphics program. |
Turtle.mode (Mode=none) |
Set the Turtle mode ("Standard", "logo" or "World") and perform a reset. If no pattern is given, the current mode is returned.
Mode |
Initial Turtle Title |
Positive angle |
Standard |
Right (east) |
Counterclockwise |
Logo |
Up (north) |
Clockwise |
|
Turtle.delay (Delay=none) |
Sets or returns the drawing delay in milliseconds. |
Turtle.begin_poly () |
Begins recording the vertices of a polygon. The current turtle position is the first vertex of a polygon. |
Turtle.end_poly () |
Stops recording vertices of a polygon. The current turtle position is the last vertex of the polygon. will be connected to the first vertex. |
Turtle.get_poly () |
Returns the last recorded polygon. |
3. Detailed instructions
3.1turtle.circle (Radius,extent=none,steps=none)
Description: Draws a circle at a given radius
Parameters:
Radius: The radius is positive (negative), indicating that the center circle is on the left (right) of the brush;
Extent (radians) (optional);
Steps (optional) (the inner tangent regular polygon of a circle radius radius, the number of polygon edges is steps).
Example:
circle(50)#整圆;
circle(50,steps=3)#三角形;
circle(120,180)#半圆
4. Example
1. Sun Flower
# coding=utf-8 Import Turtle Import time # set Pencolor=color1, Fillcolor=color2 turtle.color ("Red", "yellow")
2. Pentagram
# Coding=utf-8 Import Turtle Import Time turtle.pensize (5) Turtle.pencolor ("Yellow") Turtle.fillcolor ("Red") Turtle.begin_fill () for _ in range (5): Turtle.forward ($) turtle.right (144) Turtle.end_fill () Time.sleep (2) Turtle.penup () Turtle.goto ( -150,-120) turtle.color ("Violet") turtle.write ("Done", font= (' Arial ', +, ' normal '))
3. Clock Program
# Coding=utf-8 Import Turtle from datetime import * # lift the brush, move forward a distance drop def Skip (step): Turtle.penup () Turtle.forward (St EP) Turtle.pendown () def mkhand (name, length): # Register Turtle shape, build needle turtle turtle.reset () Skip (-length * 0.1) # start recording The vertex of the polygon. The current turtle position is the first vertex of a polygon. Turtle.begin_poly () Turtle.forward (length * 1.1) # Stops the vertex of the recording polygon. The current turtle position is the last vertex of the polygon. will be connected to the first vertex. Turtle.end_poly () # Returns the last recorded polygon. Handform = Turtle.get_poly () turtle.register_shape (name, handform) def Init (): Global Sechand, Minhand, Hurhand, print ER # reset Turtle point to North Turtle.mode ("logo") # Create three hands Turtle and initialize Mkhand ("Sechand", 135) Mkhand ("Minhand",) Mkhand (" Hurhand "," 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) # Build Output text Turtle printer = Turtle. Turtle () # Hides the Turtle shape of the brush printer.hideturtle () Printer.penup () def setupclock (RADIUS): # Set the outer frame of the table Turtle.reset () turtle.pensize (7) for I in range: Skip (RADIUS) If I% 5 = = 0:turtle.forward (-radius-20) Skip (radius +) if i = = 0:turtle.write (int), align= "Center", font= ("Courier", +, "bold")) elif i = = 30:skip (25) Turtle.write (int (I/5), align= "Center", font= ("Courier", "bold")) Skip ( -25) elif (i = = or i = = 35): Skip (Turtle.write) (int (I/5), align= "Center", font= ("Courier", "bold")) Skip ( -20) Els E:turtle.write (int (I/5), align= "Center", font= ("Courier", +, "bold")) Skip (-radius-20) else:t Urtle.dot (5) Skip (-radius) turtle.right (6) def Week (t): Week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" "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 (): # Draw TableThe dynamic display of the needle 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 (* hour) Turtle.tracer (False) Printer.forward (+) Printer.write (Week (t), align= "Center", font= ("Courier", +, "bo LD ")) Printer.back (Printer.write) (Date (t), align=" Center ", font= (" Courier "," bold ")) Printer.home () Turtle.tracer (True) # 100ms continues to call the tick Turtle.ontimer (tick, +) def main (): # turns turtle animation on/off and sets a delay for updating sheets. Turtle.tracer (False) Init (setupclock) turtle.tracer (True) Tick () Turtle.mainloop () if __name__ = = "__mai N__ ": Main ()
Draw a square with an edge length of 60, fill it with red, and the border is blue
#-*-coding:utf-8-*-import turtleturtle.reset () a= 60turtle.fillcolor ("Red") Turtle.pencolor ("Blue") turtle.pensize ( Turtle.fill (True) Turtle.left (a) Turtle.forward (a) Turtle.left (Turtle.forward) (a) Turtle.left (90) Turtle.forward (a) turtle.left (a) Turtle.forward (a) Turtle.fill (False)
One last example:
From NumPy import *from random import Randomimport turtleturtle.reset () x = Array ([[. 5],[.5]]) p = [0.85,0.92,0.99,1.00]A1 = Array ([[. 0.04], [ -0.04,.85]]) B1 = Array ([[[[0],[1.6]]) A2 = Array ([[[0.20,-0.26], [0.23,0.22]]) b2 = Array ([[0 ],[1.6]]) A3 = Array ([[[ -0.15,0.28], [0.26,0.24]]) B3 = Array ([[0],[0.44]]) A4 = Array ([[[0,0], [0,0.16]]) Turtle.color ("blue") cnt = 1while True: cnt + = 1 if cnt = =: Break r = random () if R < p[0]:
x = Dot (A1, x) + B1 elif r < p[1]: x = dot (A2, x) + b2 elif r < p[2]: x = dot (A3, x) + B3
else: x = dot (A4, x) #print x[1] turtle.up () Turtle.goto (x[0][0] * 50,x[1][0] * 40-240) tu Rtle.down () Turtle.dot ()
PS: When I first used turtle (Windows) I had the can not find the moudle named "Tkinter" because I canceled the installation of the TCL component when I installed Python. If reloading Python is not a solution, see the Great gods of csdn for a moment.
Python paint "Little Turtle" turtle