猫史档案馆


【Python作品分享】五星红旗【作品秀】

用户:春嵐春嵐查看:0 回复:0 评论:0 创建时间:2021-11-27T22:36:46


【作品展示】

center_image

 

【作品介绍】

绘制五星红旗

 

【作品源代码】

import turtle

myscreen = turtle.Screen()
myscreen.setworldcoordinates(0, 0, 800, 800)

mnb = turtle.Pen()
mnb.speed(0)


def rectangle(x, y, width, height, color):
    setpen(x, y, color)
    mnb.begin_fill()
    for i in range(2):
        mnb.forward(width)
        mnb.right(90)
        mnb.forward(height)
        mnb.right(90)
    mnb.end_fill()


def star(x, y, angle, length, color):
    # 设置画笔
    setpen(x, y, color)
    # 设置画笔朝向
    mnb.setheading(angle)
    # 开始绘制
    mnb.begin_fill()
    for i in range(5):
        mnb.forward(length)
        mnb.left(72)
        mnb.forward(length)
        mnb.right(144)
    mnb.end_fill()


def setpen(x, y, color):
    mnb.penup()
    mnb.setheading(0)
    mnb.goto(x, y)
    mnb.pencolor(color)
    mnb.fillcolor(color)
    mnb.pendown()
    mnb.setheading(0)


def main():
    per = 20
    mx, my = 100, 700
    # 旗面
    width, height = 30 * per, 20 * per
    rectangle(mx, my, width, height, "red")
    # 大五角星
    import math
    R = 3 * per
    ox, oy = mx + 5 * per, my - 5 * per
    fa = R * math.sin(math.radians(72))
    fo = R * math.cos(math.radians(72))
    ax, ay = ox - fa, oy + fo
    length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
    star(ax, ay, 0, length, "yellow")

    R = 1 * per
    ox, oy = mx + 10 * per, my - 2 * per
    anlge = math.degrees(math.atan(3 / 5))
    oh = R * math.sin(math.radians(anlge))
    ah = R * math.cos(math.radians(anlge))
    ax, ay = ox - ah, oy - oh
    length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
    star(ax, ay, anlge + 18, length, "yellow")

    R = 1 * per
    ox, oy = mx + 12 * per, my - 4 * per
    anlge = math.degrees(math.atan(1 / 7))
    oh = R * math.sin(math.radians(anlge))
    ah = R * math.cos(math.radians(anlge))
    ax, ay = ox - ah, oy - oh
    length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
    star(ax, ay, anlge + 18, length, "yellow")

    R = 1 * per
    ox, oy = mx + 12 * per, my - 7 * per
    anlge = math.degrees(math.atan(2 / 7))
    oh = R * math.sin(math.radians(anlge))
    ah = R * math.cos(math.radians(anlge))
    ax, ay = ox - ah, oy - oh
    length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
    star(ax, ay, -(anlge - 18), length, "yellow")

    R = 1 * per
    ox, oy = mx + 10 * per, my - 9 * per
    anlge = math.degrees(math.atan(4 / 5))
    oh = R * math.sin(math.radians(anlge))
    ah = R * math.cos(math.radians(anlge))
    ax, ay = ox - ah, oy - oh
    length = (R - R * math.cos(math.radians(72))) / math.cos(math.radians(18))
    star(ax, ay, -(anlge - 18), length, "yellow")

    # 隐藏画笔
    mnb.hideturtle()
    turtle.done()


main()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页