In my classes at Udacity, I learned about python's turtle method, a classic graphical module for teaching kids programming that originated in the logo language. python itself has this module built in, and its visual approach can help kids understand some of the basic concepts of programming.
I saw a lot of beautiful graphics drawn by turtle in the homework submission forum, and couldn't think of anything to draw, so I decided to practice with the five star red flag.
preliminary
Five Star Red Flag Drawing Parameters
Turtle Official Documentation
Basic operation of the turtle
# Initialize the screen window = () # New instance of turtle object import turtle aTurtle = () # Turtle setup () # Hide the arrows (10) # Setting the speed # Forward and backward, left and right (100) # 100 pixels forward (90) # Turn 90° to the right (100) (90) # Fill color aTurtle.begin_fill() ('yellow') DoSomethinghere() aTurtle.end_fill() # Lift up and put down the pen so that the operation is performed without leaving a mark () (start_pos) (radius) ()
Drawing the five-star red flag code
github address: /dc11287081ee67075da8
#!/usr/bin/env python # -*- coding: utf-8 –*- ''' Some wrapper methods for the turtle class, including drawing square polygons, square polygons, and five-starred flags.''' __author__ = 'Hu Wenchao' import turtle import math def draw_polygon(aTurtle, size=50, n=3): ''' Drawing square polygons args. aTurtle: instance of turtle object size: int type, the length of the sides of the polygon. n: int type, how many sides ''' for i in xrange(n): (size) (360.0/n) def draw_n_angle(aTurtle, size=50, num=5, color=None): ''' Draw positive n-angles, default is yellow args. aTurtle: instance of turtle object size: int type, the length of the sides of the polygon. n: int type, how many polygons color: str, the color of the graphic, no color by default ''' if color: aTurtle.begin_fill() (color) for i in xrange(num): (size) (360.0/num) (size) (2*360.0/num) if color: aTurtle.end_fill() def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None): ''' Draw a pentagram based on the start position, end position and radius of the outer circle args. aTurtle: turtle object instance start_pos: binary tuple of int, the center of the outer circle of the pentagram to be drawn end_pos: int's binary tuple, the location of the center of the circle. radius: radius of the outer circle of the pentagram. color: str, the color of the graphic. ''' aTurtle = aTurtle or () size = radius * (/5)/(*2/5) ((math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0]))) () (start_pos) (radius) () ((*9/10)) draw_n_angle(aTurtle, size, 5, color) def draw_5_star_flag(times=20.0): ''' Drawing the Five Star Red Flag args. times: the size of the red flag is 30*20, times is the number of times, the default size is 10 times, i.e., 300*200. ''' width, height = 30*times, 20*times # Initialize screens and turtles window = () aTurtle = () () (10) # Painting the red flag () (-width/2, height/2) () aTurtle.begin_fill() ('red') (width) (90) (height) (90) (width) (90) (height) (90) aTurtle.end_fill() # Drawing big stars draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow') # Draw four little stars stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)] for pos in stars_start_pos: draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow') # Click to close the window () if __name__ == '__main__': draw_5_star_flag()
Results:
summarize
Above is this article on Python using the Turtle module to draw the five-starred red flag code example of the whole content, I hope you can help. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!