Knowledge mapping
Overview of turtle Library
For more information about the turtle library, visit.
There are three ways to use the import reserved word to reference the turtle Library:
- Import Tuttle, the function call in the Tuttle library adopts Tuttle< Function name > ().
import turtle turtle.circle(200)
- from turtle import *, the function call to the tuttll function library directly takes the form of < function name > () instead of using turtle As a leader.
from turtle import * circle(200)
Alternatively, import only the functions used.
from turtle import circle circle(200)
- import turtle as t, the function call in the turtle library adopts the more concise form of T. < function name > (). The reserved word as is used to give the turtle library the alias t. You can also use aliases other than t.
impory turtle as t t.circle(200)
turtle library and basic functions
This library contains 100 pairs of functions, mainly including form functions, brush state functions, and brush motion functions.
window function
turtle. The setup() function is related to the form and is defined as follows.
turtle.setup(width,height,startx,starty)
Function: set the size and position of the main window
Width: window width. If it is an integer, it represents the pixel value of the; If decimal, it indicates the ratio of window width to screen.
Height: window height. If true, it indicates the pixel value; If decimal, it indicates the ratio of window height to screen.
startx: the pixel distance between the left side of the window and the left side of the screen. If the value is None, the window is located in the horizontal center of the screen.
Start: the pixel distance between the top of the window and the top of the screen. If the value is None, the window is located in the vertical center of the screen.
Brush state function
pencolor() sets the color of the brush. write(str,font=None) outputs the string of the font
Brush motion function
practice
Write a program to draw triangles, quadrangles, pentagons, hexagons and circles. By calling begin of turtle_ Fill() and end_ The fill () method fills the drawing with color. Output a line of text with Times font, 18 font size and bold by calling the write() function. The program code is as follows:
import turtle turtle.pensize(3) turtle.penup() turtle.goto(-200,-50) turtle.pendown() turtle.begin_fill() turtle.color('red') turtle.circle(40,steps=3) turtle.end_fill() turtle.penup() turtle.goto(-100,-50) turtle.pendown() turtle.begin_fill() turtle.color('blue') turtle.circle(40,steps=4) turtle.end_fill() turtle.penup() turtle.goto(0,-50) turtle.pendown() turtle.begin_fill() turtle.color('green') turtle.circle(40,steps=5) turtle.end_fill() turtle.penup() turtle.goto(100,-50) turtle.pendown() turtle.begin_fill() turtle.color('yellow') turtle.circle(40,steps=6) turtle.end_fill() turtle.penup() turtle.goto(200,-50) turtle.pendown() turtle.begin_fill() turtle.color('purple') turtle.circle(40) turtle.end_fill() turtle.color('green') turtle.penup() turtle.goto(-100,50) turtle.pendown() turtle.write(("Cool Colorful Shapes"),font=("Times",18,"bold")) turtle.hideturtle() #Hide the brush's turtle shape turtle.done()#Pause the program and stop the brush drawing
result
random library overview
Random library is used to generate pseudo-random number sequences of various distributions.
Random library and application of random numbers
time library overview
The functions of time library are mainly divided into three aspects: time processing, time formatting and timing.
Time processing mainly includes four functions:
time.time() gets the current timestamp
ime.gmtime(secs) gets the struct corresponding to the current timestamp_ Time object.
time.localtime(secs) gets the struct of the local time corresponding to the current timestamp_ Time right
time.ctime(secs) gets the readable string representation corresponding to the current timestamp, and the internal call time The localtime() function to output the local time.
Time formatting mainly includes three functions:
rime.mktime(t) struct_ The time object t is converted to a timestamp, and T represents the local time.
time.strftime() uses a format string to represent the time format.
time.strptime() this method is the same as time In contrast, strftime() is used to extract the time in the string and generate a struts_ Time object.
Timing mainly includes three functions:
time.sleep()
time.monotonic()
time.perf_counter()
- struct_ Element composition of time object
- Format controller for strftime() method
Example analysis -- snow scenery art drawing
from turtle import * from random import * def drawSnow(): hideturtle() pensize(2) for i in range(100): r,g,b=random(),random(),random() pencolor(r,g,b) penup() setx(randint(-350,350))#Randomly generate a number within the range as the x distance of the snowflake sety(randint(1,270)) pendown() dens=randint(8,12) snowsize=randint(10,14) for j in range(dens): forward(snowsize)#Advance a specified distance in the current direction backward(snowsize)#Back a specified distance in the opposite direction of the current right(360/dens) def drawGround(): hideturtle() for i in range(400): pensize(randint(5,10)) x=randint(-400,350) y=randint(-280,-1) r,g,b=-y/280,-y/280,-y/280 pencolor((r,g,b)) penup() goto(x,y) pendown() forward(randint(40,100)) setup(800,600,200,200)# Snow drawing size tracer(False) bgcolor("black") drawSnow() drawGround() done()
result
After class exercises
- Draw a honeycomb hexagon using the turtle library.
from turtle import * #Draw a hexagon def onesix(l,x,y): penup() goto(x,y) pendown() begin_fill() circle(l,steps=6) color('black','orange') end_fill() #Draw r-circle hexagon def rsix(l,r): xyset=set() findxy(r,xyset) for x,y in xyset: onesix(l,x,y) #Find the xy coordinates of each hexagon in the r-cycle hexagon #Data sets are used because they are not repeated def findxy(r,xyset=set()): x,y=0,0 xyset.add((x,y)) for i in range(r): xyempty=set() for x,y in xyset: xyempty.add((x+(3**0.5)*l,y)) xyempty.add((-x-(3**0.5)*l,y)) xyempty.add((x+(3**0.5)*l/2,y+3*l/2)) xyempty.add((-x-(3**0.5)*l/2,y+3*l/2)) xyempty.add((x+(3**0.5)*l/2,-y-3*l/2)) xyempty.add((-x-(3**0.5)*l/2,-y-3*l/2)) xyset|=xyempty tracer(False) #No animation speed(100)#The speed will be slow hideturtle() l=20 #The length of the side of a hexagon r=6 #Number of turns of honeycomb rsix(l,r) done()
result
2. Draw a rose using the turtle library.
import turtle # Set initial position turtle.penup() turtle.left(90) turtle.fd(200) turtle.pendown() turtle.right(90) # Stamen turtle.fillcolor("red") turtle.begin_fill() turtle.circle(10,180) turtle.circle(25,110) turtle.left(50) turtle.circle(60,45) turtle.circle(20,170) turtle.right(24) turtle.fd(30) turtle.left(10) turtle.circle(30,110) turtle.fd(20) turtle.left(40) turtle.circle(90,70) turtle.circle(30,150) turtle.right(30) turtle.fd(15) turtle.circle(80,90) turtle.left(15) turtle.fd(45) turtle.right(165) turtle.fd(20) turtle.left(155) turtle.circle(150,80) turtle.left(50) turtle.circle(150,90) turtle.end_fill() # Petal 1 turtle.left(150) turtle.circle(-90,70) turtle.left(20) turtle.circle(75,105) turtle.setheading(60) turtle.circle(80,98) turtle.circle(-90,40) # Petal 2 turtle.left(180) turtle.circle(90,40) turtle.circle(-80,98) turtle.setheading(-83) # Leaf 1 turtle.fd(30) turtle.left(90) turtle.fd(25) turtle.left(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(-80,90) turtle.right(90) turtle.circle(-80,90) turtle.end_fill() turtle.right(135) turtle.fd(60) turtle.left(180) turtle.fd(85) turtle.left(90) turtle.fd(80) # Leaf 2 turtle.right(90) turtle.right(45) turtle.fillcolor("green") turtle.begin_fill() turtle.circle(80,90) turtle.left(90) turtle.circle(80,90) turtle.end_fill() turtle.left(135) turtle.fd(60) turtle.left(180) turtle.fd(60) turtle.right(90) turtle.circle(200,60) turtle.hideturtle()
result
3. Draw a color map using the turtle library.
from turtle import * setup(600,800,None,None) pensize(10) speed(10) t=0 #Draw color map for r in range(0,256,80): for g in range(0,256,80): for b in range(0,256,80): pencolor(r/255,g/255,b/255) t+=10 penup() goto(-250,350-t) pendown() forward(500)
result
4. Use the random library to generate a list of 10 random integers between 0 and 100.
from random import * line=[] for i in range(10): line.append(randint(0,100)) print(line)
result
[38, 77, 38, 47, 76, 44, 86, 94, 38, 92]
- Use the time library to convert the current date and time into a format similar to "Sunday, 8.January 2017 11:03PM"
from time import * now=strftime("%A,%d.%B %Y %I:%M%p",localtime()) print(now)
result
Monday,16.August 2021 12:12PM