Turtle Drawing (Turtle graphics)

Source: Internet
Author: User

A simple drawing tool, called the Turtle drawing (Turtle graphics), which was introduced in the python2.6 version, is the internal library of Python, which can be used to importimport turtle

Let's start by explaining the basics 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

1.1 Setting the canvas size

Turtle.screensize (Canvwidth=none, Canvheight=none, Bg=none)
Parameters are canvas width (per pixel), height, 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)

Parameters:
width, Height: When the input width and height are integers, the pixels are represented; is a decimal, the percentage that occupies 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 in the center of the screen

Such as:

turtle.setup(width=0.6, height=0.6)turtle.setup(width=800, height=800, startx=100, starty=100)
2. The state of the brush 2.1 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: 坐标原点 (position), 面朝x轴正方向 (direction), turtle drawing, that is, using 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)
1) turtle.pensize (): Sets the width of the brush;
2) Turtle.pencolor (); No parameters are passed in, the current brush color is returned, the incoming parameter sets the brush color, which can be a string such as "green", "red", or an RGB 3-tuple,

    >>> pencolor(‘brown‘)    >>> tup = (0.2, 0.8, 0.55)    >>> pencolor(tup)    >>> pencolor()    ‘#33cc8c‘

3) Turtle.speed: Set the brush movement speed, the brush draws the speed range [0,10] integer, the higher the number the faster

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 () Do not draw a graphic when moving, lift the pen, use it for another place to draw
Turtle.speed (Speed) The speed range [0,10] integer drawn by the brush
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

(2) Brush control command:

Command Description
Turtle.pensize (width) Width when drawing a drawing
Turtle.pencolor () Brush color
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 arrows display;
Turtle.showturtle () Corresponds to the Hideturtle () function

(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, which are the font name, size and type; font is optional, font parameters are optional
3. Detailed instructions

3.1 Turtle.circle (RADIUS, extent=none, Steps=none)
Description: Draws a circle at a given radius
Parameters:
Radius (RADIUS); The radius is positive (negative), which indicates that the center circle is drawn 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) # Full Circle;
Circle (50,steps=3) # triangle;
Circle (120, 180) # semicircle

4. Drawing Example 4.1 sun flower

?

import turtle as timport timet.color("red", "yellow")t.speed(10)t.begin_fill()for _ in range(50): t.forward(200) t.left(170)end_fill()time.sleep(1)
4.2 Draw the Little Python

?

Import Turtledef Drawsnake (rad, Angle,Len, Neckrad):For _In range (Len): Turtle.circle (rad, Angle) turtle.circle (- rad, Angle) turtle.circle (rad, angle/ 2) Turtle.forward (rad/2) # straight ahead Turtle.circle (Neckrad, 180) Turtle.forward (rad/4) if __name__ =  "__main__": Turtle.setup (1500, 1400, 0,  0) turtle.pensize (30) # Brush size Turtle.pencolor ( "green") Turtle.seth (-40) # Forward Direction drawsnake (70, 80, 2, 15)      
4.3 Draw the Pentagram

?

import turtle Import timeturtle.pensize (5) turtle.pencolor ( "yellow") Turtle.fillcolor ( "red") Turtle.begin_fill () for _ in range (5): Turtle.forward (200) turtle.right ( 144) Turtle.end_fill () time.sleep (2) Turtle.penup () Turtle.goto (-150,-120) Turtle.color (  "Violet") Turtle.write ( "Done", font= ( 40,  ' normal ') time.sleep ( 1)              
4.4 Piglet Paige

Thank River Green pepper Shredded pork, piglet, very vivid:

# Coding:utf-8Import TurtleAs T# Draw Piggy Page# =======================================t.pensize (4) T.hideturtle () T.colormode (255) T.color ((255,155,192),"Pink") T.setup (840,) T.speed (10)# nose T.pu () T.goto (-100,T.PD () T.seth (-30) T.begin_fill () A =0.4For IIn range (120):If0 <= I <30Or<= I <90:a = A +0.08 t.lt (3)# Turn left 3 degrees T.FD (a)# go ahead, step a.Else:a = A-0.08 t.lt (3) T.FD (a) T.end_fill () T.pu () T.seth () T.FD () T.seth (0) T.FD (T.PD () T.pencolor (255,155,192) T.seth (T.begin_fill () t.circle (5) T.color (160,82,T.end_fill () T.pu () T.seth (0) T.FD () T.PD () T.pencolor (255,155,192) T.seth (T.begin_fill () t.circle (5) T.color (160,82,) T.end_fill ()# Head T.color ((255,155,192),"Pink") T.pu () T.seth () T.FD () T.seth (0) T.FD (0) T.PD () T.begin_fill () T.seth ((T.circle) (300,-30) T.circle (100,-60) T.circle (80,-100) T.circle (150,-20) T.circle (60,-95) T.seth (161) T.circle (-300,T.pu () T.goto (-100,T.PD () T.seth (-30) A =0.4For IIn range (60):If0 <= I <30Or<= I <90:a = A +0.08 t.lt (3)# Turn left 3 degrees T.FD (a)# go ahead, step a.Else:a = A-0.08 t.lt (3) T.FD (a) T.end_fill ()# Ear T.color ((255,155,192),"Pink") T.pu () T.seth () T.FD (-7) T.seth (0) T.FD (T.PD () T.begin_fill () T.seth ((T.circle) (-50,) T.circle (-10,T.circle (-50,T.end_fill () T.pu () T.seth () T.FD (-12) T.seth (0) T.FD (T.PD () T.begin_fill () T.seth ((T.circle) (-50,) T.circle (-10,T.circle (-50,) T.end_fill ()#眼睛t. Color (255,155,192),"White") T.pu () T.seth () T.FD (-20) T.seth (0) T.FD (-95) T.PD () T.begin_fill () t.circle (T.end_fill () T.color ("Black") T.pu () T.seth () T.FD () T.seth (0) T.FD (-3) T.PD () T.begin_fill () t.circle (3) T.end_fill () T.color (255,155,192),"White") T.pu () T.seth () T.FD (-25) T.seth (0) T.FD (T.PD () T.begin_fill () t.circle (T.end_fill () T.color ("Black") T.pu () T.seth () T.FD () T.seth (0) T.FD (-3) T.PD () T.begin_fill () t.circle (3) T.end_fill ()# Cheek T.color ((255,155,192)) T.pu () T.seth () T.FD (-95) T.seth (0) T.FD (() T.PD () T.begin_fill () t.circle () T.end_fill ()# Mouth T.color (239,69,) T.pu () T.seth () T.FD (T.seth (0) T.FD (-100) T.PD () T.seth (-80) T.circle (30,(T.circle) (40,80)# Body T.color ("Red", (255,99,) T.pu () T.seth () T.FD (-20) T.seth (0) T.FD (-78) T.PD () T.begin_fill () T.seth (-130) T.circle (100,Ten) T.circle (300,) T.seth (0) T.FD () T.seth (90) t.circle (300,30) t.circle (100,3) T.color ((255,155,192), ( 255,100,100)) T.seth ( -135) t.circle ( -80,63) t.circle ( -150,24) T.end_fill () # Hand T.color ((255,155,192)) T.pu () T.seth (90) T.FD ( -40) t.seth (0) t.fd ( -27) t.pd () T.seth ( -160) t.circle (300,15) T.pu () t.seth (0) t.fd (0) T.seth () T.seth ( -10) t.circle ( -20,90) T.pu () T.seth (All) t.fd (+) t.seth (0) t.fd (237) T.PD () T.seth ( -20) t.circle ( -300,15) T.pu () T.seth (T.FD) t.seth (0) t.fd (0) t.pd () T.seth ( -170) t.circle (20,90) # feet T.pensize (Ten) T.color ((240,128,128)) T.pu () T.seth (T.FD) ( -75) t.seth (0) t.fd ( -180) t.pd () T.seth ( -90) t.fd (+) T.seth ( -180) T.color ("Black") T.pensize (T.FD) ( T.pensize (T.color) ((+, +)) T.pu () T.seth (+) T.FD (+) t.seth (0) t.fd (All) t.pd () T.seth ( -90) t.fd (+) T.seth ( -180) T.color ("Black") T.pensize (T.FD) (20) # Tail t.pensize (4) T.color ((255, 155, 192)) T.pu () T.seth (all t.fd) T.seth (0) T.FD (T.PD) t.seth (0) t.circle (+) t.circle (+, +) t.circle (+) T.done ()  

Turtle Drawing (Turtle graphics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.