使用海龟模块turtle 绘画标准国旗致敬伟大祖国得生日
话不多说,直接上代码:
import math
import turtle
SIZE = 20
# 画边框 红旗宽高比3:2
def draw_petal():
turtle.speed(9)
turtle.color("red", "red")
turtle.penup()
turtle.goto(-SIZE*15, SIZE*10)
turtle.begin_fill()
turtle.pendown()
turtle.forward(SIZE*30)
turtle.right(90)
turtle.forward(SIZE * 20)
turtle.right(90)
turtle.forward(SIZE * 30)
turtle.right(90)
turtle.forward(SIZE * 20)
turtle.right(90)
turtle.end_fill()
#画大五角星
def draw_flower():
turtle.penup()
turtle.goto(-SIZE * 13, SIZE * 6)
turtle.color("yellow", "yellow")
turtle.begin_fill()
turtle.pendown()
for i in range(5):
turtle.forward(SIZE*6)
turtle.right(144)
turtle.end_fill()
t = turtle
u = SIZE
# 画出剩下的4个小五角星
for i in range(4):
t.pu()
if i == 0:
t.goto(-5 * u, 8 * u) # 到达第一个小五角星的中心点
t.seth(math.degrees(math.atan(3 / 5)))
elif i == 1:
t.goto(-3 * u, 6 * u) # 到达第二个小五角星的中心点
t.seth(math.degrees(math.atan(1 / 7)))
elif i == 2:
t.goto(-3 * u, 3 * u) # 到达第三个小五角星的中心点
t.seth(-math.degrees(math.atan(2 / 7)))
else:
t.goto(-5 * u, 1 * u) # 到达第四个小五角星的中心点
t.seth(-math.degrees(math.atan(4 / 5)))
t.bk(SIZE) # 往后退一个格子距离
draw_start()
# 画单个五角星
def draw_start():
turtle.begin_fill()
turtle.left(18) # 使得角尖尖对准中心
turtle.pendown()
for i in range(5):
turtle.forward(2*SIZE)
turtle.right(144)
turtle.end_fill()
turtle.penup()
# 画小线条
def draw_line():
for j in range(15):
turtle.goto(-j*SIZE-SIZE, 0)
turtle.pendown()
turtle.goto(-j*SIZE-SIZE, SIZE*10)
turtle.penup()
for j in range(10):
turtle.goto(0, j*SIZE+SIZE)
turtle.pendown()
turtle.goto(-SIZE*15, j*SIZE+SIZE)
turtle.penup()
# 画栅格
def draw_grid():
turtle.color("black")
turtle.goto(0, SIZE*10)
turtle.pendown()
turtle.goto(0, -SIZE*10)
turtle.penup()
turtle.goto(SIZE * 15, 0)
turtle.pendown()
turtle.goto(-SIZE * 15, 0)
turtle.penup()
draw_line() # 画线条
# 隐藏海龟
def main():
draw_petal()
draw_grid()
draw_flower()
draw_grid()
turtle.goto(SIZE, SIZE)
turtle.color("red", "red")
turtle.done()
if __name__ == "__main__":
main()