使用python3的turtle庫(kù)制作美國(guó)國(guó)旗

代碼



def main():
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(0)
    whiterectangle(t, -190, 100, 380, 200)
    strip(t, -190, 100, 380, 200)
    bluerectangle(t, -190, 100, 380, 200)
    stars(t, -190, 100, 380, 200)


def whiterectangle(t, x, y, w, h):
    t.color('white', 'white')
    t.up()
    t.goto(x, y)
    t.begin_fill()
    t.down()
    t.goto(x + w, y)
    t.goto(x + w, y - h)
    t.goto(x, y - h)
    t.goto(x, y)
    t.end_fill()
    t.up()


def strip(t, x, y, w, h):
    for i in range(7):
        t.up()
        t.goto(x, y - 2 * (h / 13) * i)
        t.down()
        t.color('red', 'red')
        t.begin_fill()
        t.goto(x + w, y - 2 * (h / 13) * i)
        t.goto(x + w, y - 2 * (h / 13) * i - (h / 13))
        t.goto(x, y - 2 * (h / 13) * i - (h / 13))
        t.goto(x, y - 2 * (h / 13) * i)
        t.end_fill()
        t.up()


def bluerectangle(t, x, y, w, h):
    t.color('blue', 'blue')
    t.up()
    t.goto(x, y)
    t.begin_fill()
    t.down()
    t.goto(x + .4 * w, y)
    t.goto(x + .4 * w, y - (7 / 13) * h)
    t.goto(x, y - (7 / 13) * h)
    t.goto(x, y)
    t.end_fill()


def star(t, x1, y1):
    t.up()
    t.goto(x1, y1)
    t.left(36)
    t.down()
    t.fillcolor('white')
    t.begin_fill()
    for i in range(5):
        t.pencolor('white')
        t.forward(8.4)
        t.left(144)
    t.end_fill()
    t.right(36)


def stars(t, x, y, w, h):
    t.up()
    for i in range(5):
        for j in range(6):
            t.up()
            t.down()
            star(t, x + (1 / 18) * w * .4 + j * (1 / 6) * w * .4,\
            y - (2 / 11) * (7 / 13) * h - 2 * i * (1 / 11) * (7 / 13) * h)
    for i in range(4):
        for j in range(5):
            t.up()
            t.down()
            star(t, x + (2 / 17) * w * .4 + j * (3 / 17) * w * .4,\
            y - (3 / 11) * (7 / 13) * h - 2 * i * (1 / 11) * (7 / 13) * h)


main()
turtle.done()

效果圖

image.png

注意

其中一些數(shù)據(jù)是估計(jì)得來(lái) 并不精準(zhǔn)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,045評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,327評(píng)論 4 61
  • 最好的青春是拿來(lái)奮斗的,最美的時(shí)光是祭奠寂寞的。而往往青春總是伴隨著事與愿違的擦不掉的錯(cuò)誤,還有那作死的矯情和骨子...
    格小七閱讀 338評(píng)論 2 1
  • Strings Management// Set a label's textlabel.text = "Popu...
    coderzcj閱讀 298評(píng)論 0 0
  • 夏天,對(duì)我來(lái)說(shuō)是一個(gè)又愛(ài)又恨的季節(jié),喜歡西瓜和人字拖。可是太過(guò)于狂熱的太陽(yáng)卻讓人受不了。夏天下雨還好,不過(guò)空...
    易心一意不問(wèn)歸期閱讀 918評(píng)論 0 1

友情鏈接更多精彩內(nèi)容