import turtle as t from math import sin, cos, radians t.speed(0) # 画方形 def drawRect(*args): # 判断参数数量 if len(args) < 1 or len(args) > 2: raise Exception("参数数量不对") t.begin_fill() if len(args) == 1: for _ in range(0,4): t.fd(args[0]) t.rt(90) else: t.fd(args[0]) t.rt(90) t.fd(args[1]) t.rt(90) t.fd(args[0]) t.rt(90) t.fd(args[1]) t.rt(90) t.end_fill() # 画星星 # 传两个参数,边长和偏移角度 def drawStar(side, heading = 0): t.begin_fill() t.seth(heading) for _ in range(0,5): t.fd(side) t.lt(72) t.fd(side) t.rt(144) t.end_fill() # 抬笔移动到指定位置 def move(x, y): t.penup() t.goto(x, y) t.pendown() # 画中国国旗 def drawChinaFlag(): base = 20 OFFSET_Y = sin(radians(18)) OFFSET_X = cos(radians(18)) # 画旗面 t.color("red", "red") move(-15*base, 10*base) drawRect(30*base, 20*base) # 大五角星边长 side_big = (3 * base - OFFSET_Y * 3 * base) / cos(radians(18)) # 画大五角星 t.color("yellow", "yellow") move(-10 * base - OFFSET_X * 3 * base, 5 * base + OFFSET_Y * 3 * base) drawStar(side_big) # 小五角星边长 side_small = (1 * base - OFFSET_Y * 1 * base) / cos(radians(18)) # 五角星1 move(-5 * base - OFFSET_X * 1 * base, 8 * base + OFFSET_Y * 1 * base) drawStar(side_small,30) # 五角星2 move(-3 * base - OFFSET_X * 1 * base, 6 * base + OFFSET_Y * 1 * base) drawStar(side_small, 15) # 五角星3 move(-3 * base - OFFSET_X * 1 * base, 3 * base + OFFSET_Y * 1 * base) drawStar(side_small, -15) # 五角星4 move(-5 * base - OFFSET_X * 1 * base, 1 * base + OFFSET_Y * 1 * base) drawStar(side_small, -30) t.hideturtle() t.done() def drawUSFlag(): base = 20 # 星星的大小 dis = 0.7 * base small = 0.4 * dis # padding-left 左边距 pl = -12.75 * base # padding-top 上边距 pt = 6 * base t.speed("fast") # 画条 for i in range(0, 13): color = "red" if (i+1) % 2 == 0: color = "white" t.color(color, color) move(-13 * base, (6.5-i) * base) drawRect(26 * base, base) # 画左上角蓝色旗面 color = "blue" t.color(color, color) move(-13 * base, 6.5* base) drawRect(10* base, 7*base)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100# 画星星 color = "white" t.color(color, color) for i in range(0, 9): if (i + 1) % 2 == 0: for j in range(0, 5): move(pl + dis + j * 2 * dis + dis, pt - dis*i) drawStar(0.25 * base) else: for j in range(0, 6): move(pl + dis + j * 2 * dis, pt - dis*i) drawStar(0.25 * base) t.hideturtle() t.done() 123456789101112131415
if __name__ == '__main__': # drawChinaFlag() drawUSFlag() 123