How do you draw a car?
catalogue
You can draw with python's built-in turtle library, which is very simple but powerful. import turtle is a very popular function library for drawing images in python. It moves the coordinates through the x-axis and y-axis to draw graphics:
For the little turtle, it has "forward", "backward", "rotation" and other behaviors. Relative to the coordinate system, it is also completed through "forward direction", "backward direction", "left direction" and "right direction", and it can change color and size.
How to realize it?
Follow up.
design sketch
overall design
The general implementation method of Python drawing trolley is as follows:
1. Use variables to define the length
2. sleep method of time
3. Graphic planning of automobile design
4. Code implementation car model
Among them, the turtle module, some drawing commands, circle function and time (time base) are used
detailed design
The Pytho painting car uses the turtle module. This module provides a method to write vector graph, which is basically to draw lines, points and curves.
How does the turtle work? First, let python import the turtle module.
import turtle
After introducing the turtle module, calling the Pen function in the turle module, it will create a canvas automatically and bring an arrow (it represents the turtles).
t=turtle.Pen()
The implementation process also imports the time library time # to delay the code execution time
import time
Eg: time after completing the upper part of the car in the implementation code Sleep (3) # delay execution for 3 seconds
The angle and distance variables angle, distance1 and distance2 are also defined
We use the function of t just created to give instructions to the turtle.
t.left(90)
t.right(90)
The turtle spins left and right.
t.reset()
Clear the canvas and put the turtle in the starting position.
t.clear()
Clear the screen and the turtle is still in place.
t.up()
Put the brush down and don't paint
t.down()
Lift up the brush and start painting again
Draw car code:
import turtle #Import turtle Library import time #Import time library t = turtle.Pen()#Define a brush variable angle = 90 #Define angle variables distance1 = 20 #Define distance variable distance2 = 60 #Define distance variable t.color(1,0,0)#Set the car color to red ''' fill color t.color('red') t.color ('black) Equivalent to t.color(1,0,0) and t.color(0,0,0) color The function has three arguments The first parameter specifies how much red there is The second parameter specifies how much green there is The third parameter specifies how much blue there is It is black when they are all 0 It is white when they are all 1 This mix of red, green and blue is called RGB Blue and red mix to produce purple Yellow and red mix to produce orange ''' #Start filling t.begin_fill() #Go east 100 at the origin t.forward(100) #Rotate 90 degrees counterclockwise and go 20 degrees forward t.left(angle) t.forward(distance1) #Rotate 90 degrees counterclockwise and go 20 degrees forward t.left(angle) t.forward(distance1) #Rotate 90 degrees clockwise and walk forward for 20 minutes t.right(angle) t.forward(distance1) #Rotate 90 degrees counterclockwise and go 60 degrees forward t.left(angle) t.forward(distance2) #Rotate 90 degrees counterclockwise and go 20 degrees forward t.left(angle) t.forward(distance1) #Rotate 90 degrees clockwise and walk forward for 20 minutes t.right(angle) t.forward(distance1) #Rotate 90 degrees clockwise and walk forward for 20 minutes t.left(angle) t.forward(distance1) #End filling t.end_fill() #Delay execution for 3 seconds time.sleep(3) #Draw left wheel t.color(0,0,0)#Set the wheel color to black t.penup()#pen-up t.forward(10)#Move forward 10 t.pendown()#Write t.begin_fill()#Start filling t.circle(10)# Draw a circle t.end_fill()# End filling t.setheading(0)#Sets the current specified angle to 0 degrees #Draw right wheel t.penup()#pen-up t.forward(angle)#Move forward 90 t.right(angle)#Rotate 90 degrees clockwise t.forward(10)#Move forward 10 t.setheading(0)# Sets the current specified angle to 0 degrees t.pendown()#Write t.begin_fill()#Start filling t.circle(10)# Draw a circle t.end_fill()# End filling time.sleep(2)
Functions used therein:
t.color()
Change the color of the brush
begin_fill and end_fill is used to fill an area on the canvas
Circle draws a circle of a specified size
setheading makes the turtle face the specified direction
All right, that's it.
appendix
appendix
Import turtle # import turtle Library
Import time # import time library
t = turtle.Pen()
angle = 90
distance1 = 20
distance2 = 60
t.color(1,0,0)
t.begin_fill()
t.forward(100)
t.left(angle)
t.forward(distance1)
t.left(angle)
t.forward(distance1)
t.right(angle)
t.forward(distance1)
t.left(angle)
t.forward(distance2)
t.left(angle)
t.forward(distance1)
t.right(angle)
t.forward(distance1)
t.left(angle)
t.forward(distance1)
t.end_fill()
time.sleep(3)
#Draw left wheel
t.color(0,0,0)
t.penup()
t.forward(10)
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
t.setheading(0)
#Draw right wheel
t.penup()
t.forward(angle)
t.right(angle)
t.forward(10)
t.setheading(0
t.pendown()
t.begin_fill()
t.circle(10)
t.end_fill()
time.sleep(2)