#五星紅旗有14億護旗手#上熱搜
現在是2019年8月4日,距離十月一日還有一個多月,慶祝建國70周年本篇叫你用python動态或五星紅旗。
這個不是效果圖下面的才是效果圖。
#!/usr/bin/env python # -*- coding: utf-8 –*- ''' 對于turtle類的一些封裝方法,包括畫正多邊形,正多角形和五星紅旗。''' import turtle import math def draw_polygon(aTurtle, size=50, n=3): ''' 繪制正多邊形 args: aTurtle: turtle對象實例 size: int類型,正多邊形的邊長 n: int類型,是幾邊形 ''' for i in xrange(n): aTurtle.forward(size) aTurtle.left(360.0/n) def draw_n_angle(aTurtle, size=50, num=5, color=None): ''' 繪制正n角形,默認為黃色 args: aTurtle: turtle對象實例 size: int類型,正多角形的邊長 n: int類型,是幾角形 color: str, 圖形顔色,默認不填色 ''' if color: aTurtle.begin_fill() aTurtle.fillcolor(color) for i in range(num): aTurtle.forward(size) aTurtle.left(360.0/num) aTurtle.forward(size) aTurtle.right(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): ''' 根據起始位置、結束位置和外接圓半徑畫五角星 args: aTurtle: turtle對象實例 start_pos: int的二元tuple,要畫的五角星的外接圓圓心 end_pos: int的二元tuple,圓心指向的位置坐标點 radius: 五角星外接圓半徑 color: str, 圖形顔色,默認不填色 ''' aTurtle = aTurtle or turtle.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.degrees(math.pi*9/10)) draw_n_angle(aTurtle, size, 5, color) def draw_5_star_flag(times=20.0): ''' 繪制五星紅旗 args: times: 五星紅旗的規格為30*20, times為倍數,默認大小為10倍, 即300*200 ''' width, height = 30*times, 20*times # 初始化屏幕和海龜 window = turtle.Screen() aTurtle = turtle.Turtle() aTurtle.hideturtle() aTurtle.speed(10) # 畫紅旗 aTurtle.penup() aTurtle.goto(-width/2, height/2) aTurtle.pendown() aTurtle.begin_fill() aTurtle.fillcolor('red') aTurtle.fd(width) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.fd(width) aTurtle.right(90) aTurtle.fd(height) aTurtle.right(90) aTurtle.end_fill() # 畫大星星 draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow') # 畫四個小星星 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') # 點擊關閉窗口 window.exitonclick() if __name__ == '__main__': draw_5_star_flag()
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!