Home > other >  # # python exercises flag
# # python exercises flag

Time:10-29

"" "
The turtle in Python module drawing flag
"" "
The import turtle


Def draw_rectangle (x, y, width, height) :
"" "draw a rectangle. "" ""
Turtle. Goto (x, y)
Turtle. Pencolor () 'red'
Turtle. Fillcolor () 'red'
Turtle. Begin_fill ()
For I in range (2) :
Turtle. Forward (width)
Turtle. Left (90)
Turtle. Forward (height)
Turtle. Left (90)
Turtle. End_fill ()


Def draw_star (x, y, the radius) :
"" "draw a star "" "
Turtle. Setpos (x, y)
Pos1=turtle. Pos ()
Turtle. Circle (72) - the radius,
Pos2=turtle. Pos ()
Turtle. Circle (72) - the radius,
Pos3=turtle. Pos ()
Turtle. Circle (72) - the radius,
Pos4=turtle. Pos ()
Turtle. Circle (72) - the radius,
Pos5=turtle. Pos ()
Turtle. Color (' yellow ', 'yellow')
Turtle. Begin_fill ()
Turtle. Goto (pos3)
Turtle. Goto (pos1)
Turtle. Goto (pos4)
Turtle. Goto (pos2)
Turtle. Goto (pos5)
Turtle. End_fill ()


Def the main () :
"" "the main program "" "
Turtle. Speed (12)
Turtle. Penup ()
X, y=- 270-180
# main body painting flag
Width, height=540, 360,
Draw_rectangle (x, y, width, height)
# make big stars,
Pice=22
Center_x, center_y=x + 5 * pice, y + height - pice * 5
Turtle. Goto (center_x center_y)
Turtle. Left (90)
Turtle. Forward (pice * 3)
Turtle. Right (90)
Draw_star (turtle xcor (), turtle ycor (), pice * 3)
X_poses, y_poses=[10, 12, 12, 10], [2, 4, 7, 9]
# picture
twinkle twinkle little starFor x_pos, y_pos in zip (x_poses, y_poses) :
Turtle. Goto (x + x_pos * pice, y + height - y_pos * pice)
Turtle. Left (turtle) forward (center_x center_y) - turtle. Heading ())
Turtle. Forward (pice)
Turtle. Right (90)
Draw_star (turtle xcor (), turtle ycor (), pice)
# hidden turtles
Turtle. Ht ()
# according to the drawing window
Turtle. Mainloop ()


If __name__=="__main__ ':
main()
  • Related