小言_互联网的博客

庆祝国庆节,我也来画五星红旗!(使用turtle绘制国旗)

256人阅读  评论(0)

# coding=utf-8
import time
import turtle
import math

def draw_polygon(aTurtle, size=50, n=3):
    for i in range(n):
        aTurtle.forward(size)
        aTurtle.left(360.0 / n)

def draw_n_angle(aTurtle, size=50, num=5, color=None):
    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):
    aTurtle = aTurtle or turtle.Turtle()
    # size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)计算公式错误,但不影响显示
    size = radius * math.sin(math.pi / 5) / math.sin(math.pi * 3 / 10)  # 修正
    angle = math.degrees(math.atan2(end_pos[1] - start_pos[1], end_pos[0] - start_pos[0]))
    print(angle)
    aTurtle.penup()
    aTurtle.goto(start_pos)
    aTurtle.setheading(0)
    aTurtle.left(angle)
    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):
    # turtle.bgpic(r'/Users/haobing1/Desktop/timg.jpg')
    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')
        # 点击关闭窗口
    aTurtle.hideturtle()
    aTurtle.penup()
    aTurtle.goto(-width / 2+500, height / 2-450)
    # aTurtle.write("国庆节快乐\n", align="right", font=("楷体", 16, "bold"))
    aTurtle.write("        绘制人:姓名", align="right", font=("楷体", 16, "normal"))
    window.exitonclick()

if __name__ == '__main__':
    draw_5_star_flag()

 


转载:https://blog.csdn.net/qq_35447659/article/details/101307640
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场