The previous article introduced the painting method of the five-star red flag
Detailed drawing method of drawing five-star red flag in Python(Tuirtle Library)
Do a flag raising animation on the way
Drawing the red flag still uses the code in the previous article, with only a few changes.
Animation principle:
Does the animation move?
In fact, it won't
The principle of animation is the visual persistence of the eyes
When an object is moving rapidly, what the human eye sees image After disappearing, the human eye can still retain its image for about 0.1-0.4 seconds. This phenomenon is called visual persistence. Is a property of the human eye. When the human eye looks at an object, it is imaged on the retina and input into the human brain by the optic nerve to feel the image of the object. However, when the object is removed, the impression of the optic nerve on the object will not disappear immediately, but will last for 0.1-0.4 seconds. This property of the human eye is called "visual persistence of the eye".
Therefore, the object motion we see is only a dynamic image synthesized by the static image captured by the eye and the operation of the brain
According to this principle, the production of animation is the following steps
1. Draw a still image
2. Wait for a period of time (less than the duration of visual residence)
3. Clear image
4. Draw the same still image, but with a slight offset
5. Wait eliminate.. draw
The trace method and delay method are provided in the turtle module to control the screen refresh and delay, and the clear() method to clear the image
These methods can be used to produce animation effects
The effect of raising the flag is as follows:
The code is as follows:
import time from turtle import * from The_Five_starred_Red_Flag import China_Flag screensize(600, 600) # canvas size bgcolor('black') # The background color is black hideturtle() tracer(48) # Draw flagpole t = Turtle() t.color('white') t.up() t.goto(-200,-190) t.down() t.begin_fill() t.seth(90) t.goto(-200,300) t.lt(90) t.fd(10) t.lt(90) t.goto(-210,-190) t.lt(90) t.fd(10) t.end_fill() t.hideturtle() # Flag raising animation for i in range(150): China_Flag(192,-200,-190+i*2) tracer(48) time.sleep(0.05) if i != 149: clear() done()
The code for drawing the five-star red flag is as follows:
The_Five_starred_Red_Flag.py
# Draw five-star red flag """ Description of national flag system (1949 (promulgated by the presidium of the first plenary session of the Chinese people's Political Consultative Conference on September 28, 2004) The shape and color of the national flag are the same on both sides, and the five stars on the flag are opposite on both sides. For convenience, this document only takes the left side of the flagpole as the standard. For the right side of the flagpole, all the left side mentioned in this document shall be changed to the right side, and all the right side shall be changed to the left side. (1) The flag is red and rectangular, with a ratio of three to two in length and five yellow five pointed stars at the top left of the flag. One star is bigger, Its circumscribed circle diameter is three tenths of the flag height, ranking on the left; The four stars are small, with a circumscribed circle diameter of one tenth of the flag height, and the ring arch is to the right of the big star. The flagpole cover is white. (2) The position and drawing method of the five stars are as follows: A. in order to determine the position of the five stars, first divide the flag face into four equal rectangles, and divide the upper left rectangle up and down into ten equal parts, The left and right are divided into fifteen equal parts. B. the center point of the big five pointed star is at the top five, bottom five, left five and right ten of the rectangle. Its painting method is:Take this point as the center of the circle, Make a circle with a trisection radius. On this circumference, set five equally spaced points, one of which must be directly above the circle. Then connect the two separated points in the five points to form a straight line. The outer contour formed by these five lines, That is, the required big five pointed star. One of the five pointed stars is pointing upward. C. the center point of the four small five pointed stars. The first point is at the top two, bottom eight, left ten and right five of the rectangle, and the second point is at the top four, bottom six The third point is at the top seven, the bottom three, the left twelve and the right three, and the fourth point is at the top nine, the bottom one, the left ten and the right five. Its painting method is:Take the above four points as the center of the circle and divide them into four circles with an equal radius. Set five equally spaced points on each circle, Each point must be located on the connecting line between the center point of the big five pointed star and the above four centers. Then in the same way as the big pentagram, Form a small pentagram. Each of the four small pentagons has an angular tip facing the center of the big pentagram. (3) The general scale of the national flag is set as the following five, which can be selected by all walks of life at their discretion: A. 288cm long and 192 cm high. B. 240 cm long and 160 cm high. C. 192 cm long and 128 cm high. D. 144 cm long and 96 cm high. E. 96 cm long and 64 cm high. """ from turtle import * def star(center_point,first_vertex,radius): """Draw a five pointed star according to the coordinates of the center of the circle and its first vertex""" up() seth(0) goto(center_point) angle = towards(first_vertex) goto(first_vertex) lt(angle) rt(90) # Determine the five vertex coordinates five_vertex_points = [first_vertex] for _ in range(4): circle(-radius,360/5) five_vertex_points.append(pos()) # Start drawing Pentagram goto(first_vertex) color('yellow') down() begin_fill() for index in range(len(five_vertex_points)): goto(five_vertex_points[(index*2)%len(five_vertex_points)]) goto(first_vertex) end_fill() def China_Flag(height,start_x = None,start_y = None): tracer(0) # Set height and width width = (height / 2) * 3 if start_x is None and start_y is None: # Set the drawing start point start_x = -(width/2) start_y = -(height/2) up() goto(start_x,start_y) down() # Draw rectangular flag setheading(0) color('red') begin_fill() for i in range(2): fd(width) lt(90) fd(height) lt(90) end_fill() # Determine the central coordinates of the five stars five_star_center_points = [(start_x+width/2/15*5,start_y+(1/2+5/20)*height), (start_x+width/2/15*10,start_y+(1/2+8/20)*height), (start_x+width/2/15*12,start_y+(1/2+6/20)*height), (start_x+width/2/15*12,start_y+(1/2+3/20)*height), (start_x+width/2/15*10,start_y+(1/2+1/20)*height),] # Determine the coordinates of the first vertex of the five stars big_radius = height/2/10*3 # Radius of circumscribed circle of big five stars small_radius = height/2/10 # Radius of circumscribed circle of small five stars up() goto(five_star_center_points[0]) setheading(90) fd(big_radius) p = pos() first_vertex_points = [p] # First vertex coordinates for point in five_star_center_points[1:]: goto(point) seth(0) angle = towards(five_star_center_points[0]) lt(angle) fd(small_radius) first_vertex_points.append(pos()) up() # Draw Pentagram # Big Pentagram star(five_star_center_points[0], first_vertex_points[0], big_radius) # Four little five pointed stars for i in range(1,5): star(five_star_center_points[i],first_vertex_points[i],small_radius) if __name__ == '__main__': screensize(600, 400) # canvas size bgcolor('black') # The background color is black speed(0) # The speed is the fastest China_Flag(192,50,15) hideturtle() done()
Why didn't you learn python at the age of 35?