1 from turtle import * 2 import time 3 4 setup(600,800,0,0) 5 speed(0) 6 penup() 7 seth(90) 8 fd(340) 9 seth(0) 10 pendown() 11 12 speed(5) 13 begin_fill() 14 fillcolor('red') 15 circle(50,30) 16 17 for i in range(10): 18 fd(1) 19 left(10) 20 21 circle(40,40) 22 23 for i in range(6): 24 fd(1) 25 left(3) 26 27 circle(80,40) 28 29 for i in range(20): 30 fd(0.5) 31 left(5) 32 33 circle(80,45) 34 35 for i in range(10): 36 fd(2) 37 left(1) 38 39 circle(80,25) 40 41 for i in range(20): 42 fd(1) 43 left(4) 44 45 circle(50,50) 46 47 time.sleep(0.1) 48 49 circle(120,55) 50 51 speed(0) 52 53 seth(-90) 54 fd(70) 55 56 right(150) 57 fd(20) 58 59 left(140) 60 circle(140,90) 61 62 left(30) 63 circle(160,100) 64 65 left(130) 66 fd(25) 67 68 penup() 69 right(150) 70 circle(40,80) 71 pendown() 72 73 left(115) 74 fd(60) 75 76 penup() 77 left(180) 78 fd(60) 79 pendown() 80 81 end_fill() 82 83 right(120) 84 circle(-50,50) 85 circle(-20,90) 86 87 speed(1) 88 fd(75) 89 90 speed(0) 91 circle(90,110) 92 93 penup() 94 left(162) 95 fd(185) 96 left(170) 97 pendown() 98 circle(200,10) 99 circle(100,40) 100 circle(-52,115) 101 left(20) 102 circle(100,20) 103 circle(300,20) 104 speed(1) 105 fd(250) 106 107 penup() 108 speed(0) 109 left(180) 110 fd(250) 111 circle(-300,7) 112 right(80) 113 circle(200,5) 114 pendown() 115 116 left(60) 117 begin_fill() 118 fillcolor('green') 119 circle(-80,100) 120 right(90) 121 fd(10) 122 left(20) 123 circle(-63,127) 124 end_fill() 125 126 penup() 127 left(50) 128 fd(20) 129 left(180) 130 131 pendown() 132 circle(200,25) 133 134 penup() 135 right(150) 136 137 fd(180) 138 139 right(40) 140 pendown() 141 begin_fill() 142 fillcolor('green') 143 circle(-100,80) 144 right(150) 145 fd(10) 146 left(60) 147 circle(-80,98) 148 end_fill() 149 150 penup() 151 left(60) 152 fd(13) 153 left(180) 154 155 pendown() 156 speed(1) 157 circle(-200,23) 158 159 160 161 exitonclick()
第二种
1 import turtle 2 import math 3 4 5 def p_line(t, n, length, angle): 6 """Draws n line segments.""" 7 for i in range(n): 8 t.fd(length) 9 t.lt(angle) 10 11 12 def polygon(t, n, length): 13 """Draws a polygon with n sides.""" 14 angle = 360 / n 15 p_line(t, n, length, angle) 16 17 18 def arc(t, r, angle): 19 """Draws an arc with the given radius and angle.""" 20 arc_length = 2 * math.pi * r * abs(angle) / 360 21 n = int(arc_length / 4) + 1 22 step_length = arc_length / n 23 step_angle = float(angle) / n 24 25 # Before starting reduces, making a slight left turn. 26 t.lt(step_angle / 2) 27 p_line(t, n, step_length, step_angle) 28 t.rt(step_angle / 2) 29 30 31 def petal(t, r, angle): 32 """Draws a 花瓣 using two arcs.""" 33 for i in range(2): 34 arc(t, r, angle) 35 t.lt(180 - angle) 36 37 38 def flower(t, n, r, angle, p): 39 """Draws a flower with n petals.""" 40 for i in range(n): 41 petal(t, r, angle) 42 t.lt(p / n) 43 44 45 def leaf(t, r, angle, p): 46 """Draws a 叶子 and fill it.""" 47 t.begin_fill() # Begin the fill process. 48 t.down() 49 flower(t, 1, r, angle, p) 50 t.end_fill() 51 52 53 def main(): 54 window = turtle.Screen() # creat a screen 55 window.bgcolor("white") 56 window.title("draw a flower") 57 lucy = turtle.Turtle() 58 lucy.shape("turtle") 59 lucy.color("red") 60 lucy.width(3) 61 # lucy.speed(10) 62 63 # Drawing flower 64 flower(lucy, 7, 60, 100, 360) 65 66 # Drawing pedicel 67 lucy.color("brown") 68 lucy.rt(90) 69 lucy.fd(200) 70 71 # Drawing leaf 1 72 lucy.width(1) 73 lucy.rt(270) 74 lucy.color("green") 75 leaf(lucy, 40, 80, 180) 76 lucy.rt(140) 77 lucy.color("black") 78 lucy.fd(30) 79 lucy.lt(180) 80 lucy.fd(30) 81 82 # Drawing leaf 2 83 lucy.rt(120) 84 lucy.color("green") 85 leaf(lucy, 40, 80, 180) 86 lucy.color("black") 87 lucy.rt(140) 88 lucy.fd(30) 89 lucy.ht() # hideturtle 90 window.exitonclick() 91 92 93 main()
效果图: