Draw flag with Python's turtle module

Source: Internet
Author: User
Tags sin

EditDraw flag with Python's turtle module

In Udacity class, learned the Python turtle method, this is a very classic to teach children programming graphics module, originally originated from the logo language. Python itself has built-in this module, and its visual approach can help children understand some of the basic concepts of programming.
In the job submission forum to see a lot of turtle drawing out of the beautiful graphics, can not think of anything to draw things, so decided to take flag to practice practiced hand.

Pre-preparation
    • Flag Drawing parameters
    • Turtle Official documents
Basic operation of Turtle
# Initialize Screenwindow= Turtle. Screen ()# New Turtle Object InstanceImportTurtleaturtle = Turtle. Turtle ()# Turtle SettingsAturtle.hideturtle ()# Hide ArrowsAturtle.speed (Ten)# Set Speed# Forward and backward, turn left and rightATURTLE.FD ( -)# forward 100 megapixelsAturtle.right ( -)# Turn right 90 °Aturtle.back ( -) Aturtle.left ( -)# Fill ColorAturtle.begin_fill () Aturtle.fillcolor (' Yellow ') Dosomethinghere () Aturtle.end_fill ()# lift the pen up and down, so the operation won't leave a traceAturtle.penup () Aturtle.goto (start_pos) aturtle.fd (RADIUS) Aturtle.pendown ()
Drawing Flag Code

GitHub Address: Https://gist.github.com/dc11287081ee67075da8

#!/usr/bin/env python#-*-coding:utf-8–*-"For some encapsulation methods of the Turtle class, including drawing regular polygon, positive and flag. " "__author__ =' Hu Wenchao 'ImportTurtleImportMath def draw_polygon(aturtle, size=, n=3):    "' Draw regular Polygon Args:aTurtle:turtle object instance size:int type, positive polygon side length n:int type, is a few sides"     forIinchXrange (N): aturtle.forward (size) Aturtle.left (360.0/n) def draw_n_angle(aturtle, size=, num=5, Color=none):    "' Draws a positive n-angle shape, the default is the yellow Args:aTurtle:turtle object instance Size:int type, the positive side length n:int type, is a corner color: STR, graphic color, default no coloring " '    ifColor:aTurtle.begin_fill () aturtle.fillcolor (color) forIinchXrange (num): aturtle.forward (size) Aturtle.left (360.0/num) Aturtle.forward (size) aturtle.right (2*360.0/num)ifColor:aTurtle.end_fill ()def  draw_5_angle   (Aturtle=none, Start_pos= 0 , 0 ) , End_pos = 0 , 10 ) , Radius=100 , Color=none) :     "' circumscribed circle the Star Args:aTurtle:turtle object instance based on the starting position, the end position and the radius of the Start_pos:int two tuple, the circumscribed Circle center of the pentagram to be drawn End_po S:int two tuple, the center point points to the location of the coordinate point radius: Pentagram circumscribed circle radius color:str, graphic color, default does not fill the color ""Aturtle = AturtleorTurtle. Turtle () size = radius * Math.sin (math.pi/5)/math.sin (math.pi*2/5) Aturtle.left (Math.degrees (Math.atan2 (end_pos[1]-start_pos[1], end_pos[0]-start_pos[0]))) Aturtle.penup () Aturtle.goto (start_pos) aturtle.fd (RADIUS) Aturtle.pendown () Aturtle.right (math.degree S (math.pi*9/Ten)) Draw_n_angle (aturtle, size,5, color) def draw_5_star_flag(times=20.0):    "Draw flag Args:times: Flag specifications for 30*20, times for multiples, the default size is 10 times, that is, 300*200"width, height = -*times, -*times# Initialize screen and turtlewindow = Turtle. Screen () Aturtle = Turtle. Turtle () Aturtle.hideturtle () Aturtle.speed (Ten)# Draw a red flagAturtle.penup () Aturtle.goto (-width/2, height/2) Aturtle.pendown () Aturtle.begin_fill () Aturtle.fillcolor (' Red ') aturtle.fd (width) aturtle.right ( -) aturtle.fd (height) aturtle.right ( -) aturtle.fd (width) aturtle.right ( -) aturtle.fd (height) aturtle.right ( -) Aturtle.end_fill ()# Draw a big star #Draw_5_angle (Aturtle, start_pos= (-Ten*times,5*times), end_pos= (-Ten*times,8*times), radius=3*times, color=' Yellow ')# draw four Little starsStars_start_pos = [(-5,8), (-3,6), (-3,3), (-5,1)] forPosinchStars_start_pos:draw_5_angle (Aturtle, start_pos= (pos[0]*times, pos[1]*times), end_pos= (-Ten*times,5*times), radius=1*times, color=' Yellow ')# click Close WindowWindow.exitonclick ()if__name__ = =' __main__ ': Draw_5_star_flag ()

Draw flag with Python's turtle module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.