Draw simple graphics using the Turtle library in Python

Source: Internet
Author: User

Turtle Graphics Library

Turtle Library is a python built-in graphical module, belonging to one of the standard libraries, in the Python installation directory of the Lib folder, commonly used functions are the following:

    • Brush control functions
      1. penup(): Lift up the brush;
      2. pendown(): Falling brush;
      3. pensize(width): Brush width;
      4. pencolor(color): Brush color;
    • Motion control functions
      1. forward(d)/fd(d): Straight line D pixels;
      2. circle(r, extent = None): Draw radius R, Angle is extent arc, center default is the location of R on the left side of the turtle;
    • Direction control function
      1. setheading(angle)/seth(angle): Change the way forward;
      2. left(angle): Turtle turn left;
      3. right(angle): Turtle Right turn;
Use of the Turtle Library
#coding=utf-8#绘制蟒蛇import turtleturtle.penup()turtle.pencolor("red")turtle.forward(-250)turtle.pendown()turtle.pensize(10)turtle.right(45)for i in range(4):    turtle.circle(40, 80)    turtle.circle(-40, 80)turtle.circle(40, 80 / 2)turtle.fd(40)turtle.circle(16, 180)turtle.fd(40 * 2 / 3)turtle.done()
Results

#coding=utf-8# 绘制五角星import turtleturtle.pensize(5)turtle.pencolor("red")turtle.forward(200)for i in range(4):    turtle.right(144)    turtle.fd(200)turtle.done()
Results

#绘制时钟 # Coding=utf-8import Turtle as Ttfrom datetime import *# The current date belongs to the day of the Week Def Week (t): Week = ["Monday", "Tuesday", "Wednesday", "Week" Four "," Friday "," Saturday "," Sunday "] return Week[t.weekday ()]# get current time def date (t): y = t.year m = t.month D = t.day cur_    hour = T.hour;    Cur_min = T.minute;    Cur_sec = T.second; Return "%s-%d-%d%d:%02d:%02d"% (Y, M, D, Cur_hour, Cur_min, cur_sec) # move brush, distance is distancedef movepen (distance): Tt.penup () tt.pensize (5) Tt.pencolor ("Blue") tt.fd (distance) Tt.pendown () # Draw the needle def makehands (name, length): # Empty the window, Reset Turtule status to Initial state Tt.reset () Movepen (-length * 0.1) # start record polygon vertex tt.begin_poly () tt.fd (length * 1.1) # Stop Record polygon Vertex tt.end_poly () # Returns the recorded polygon Handform = Tt.get_poly () tt.register_shape (name, Handform) # Initialize def initial () : Global Sechand, Minhand, Hurhand, printer # Reset direction North (UP), positive angle to clockwise Tt.mode ("logo") # Set up and initialize the needle makehands ("Secha nd ", Minhand) makehands (" Hurhand "," makehands ") Sechand = tT.turtle () sechand.shape ("Sechand") Minhand = TT. Turtle () minhand.shape ("Minhand") Hurhand = TT. Turtle () hurhand.shape ("Hurhand") for hand in Sechand, Minhand, HurHand:hand.shapesize (1, 1, 4) hand. Speed (0) # output text printer = TT. Turtle () # Hide Brush Printer.hideturtle () Printer.penup () # Draw dial Frame def drawclock (R): # empty window, reset turtule status to initial state tt.re            Set () # Brush size tt.pensize (5) for I in range: Movepen (R) If I% 5 = = 0:TT.FD (20) Movepen (-r-20) Movepen (R +) If i = = 0: # Write text tt.write (int (1 2), align= "Center", font= ("Consolas", +, "bold")) elif i = = 30:movepen (+) TT.W Rite (int (I/5), align= "Center", font= ("Consolas", +, "bold")) Movepen ( -25) elif (i = = or i)        = = +): Movepen (tt.write) (int (I/5), align= "Center", font= ("Consolas", "bold"))        Movepen ( -20) else:tt.write (int (I/5), align= "Center", font= ("Consolas", "bold"))        Movepen (-r-20) Else: # Draws a specified radius and color point Tt.dot (5, "Red") Movepen (-R) Tt.right (6) # Dynamic display of the needle Def Handsmove (): t = datetime.today () Second = T.second + T.microsecond * 0.000001 minute = T.minute + second/60.0 hour = T.hour + minute/60.0 Sechand.seth (6 * second) Minhand.seth (6 * minute) Hurha  Nd.seth (hour) tt.tracer (False) printer.fd (+) Tt.pencolor ("green") Printer.write (Week (t), align= "center", Font = ("blackbody", +)) Printer.back (Printer.write) (Date (t), align= "center", Font = ("Consolas", 14)) # Sets the current brush position to Origin, Direction East Printer.home () Tt.tracer (True) # after 100ms continues to call the Handsmove function Tt.ontimer (handsmove, 100) # Call the defined function, open and close the animation, for Update sheet setup delay; tt.tracer (False) initial () Drawclock () tt.tracer (True) Handsmove () Tt.mainloop ()
Results

Draw simple graphics using the Turtle library in Python

Related Article

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.