亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

python和turtle繪制國(guó)旗 python turtle繪制國(guó)旗


python和turtle繪制國(guó)旗 python turtle繪制國(guó)旗

文章插圖
今天主要講一下怎么通過python和turtle繪制國(guó)旗 。先看效果圖 。
運(yùn)行效果圖
第一步:通過cad或caxa繪制出國(guó)旗形狀(必須按照國(guó)家標(biāo)準(zhǔn)規(guī)定的尺寸進(jìn)行) 。
用cad或caxa繪制出國(guó)旗
第二步:通過cad或caxa將需要繪制的國(guó)旗尺寸進(jìn)行標(biāo)注,通過python繪制時(shí)需要用到 。
標(biāo)注尺寸
第三步:打開pycharm,創(chuàng)建python文件,結(jié)合第二步的尺寸定義畫布坐標(biāo)(大?。┎?shí)現(xiàn)繪制國(guó)旗代碼(調(diào)用turtle庫)的編寫,實(shí)現(xiàn)繪制國(guó)旗的效果 。
import turtlet=turtle.Pen()t.hideturtle() #隱藏畫筆的形狀#畫國(guó)旗背景圖t.color('red')t.fillcolor('red')t.penup()t.goto(-480,320)t.pendown()t.begin_fill()t.forward(960)t.right(90)t.forward(640)t.right(90)t.forward(960)t.right(90)t.forward(640)t.end_fill()#繪制中心五角星t.penup()t.goto(-320,256)t.right(162)t.pendown()t.color('yellow')t.fillcolor('yellow')t.begin_fill()t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.end_fill()#繪制右上第一個(gè)小五角星t.penup()t.goto(-147.5,285.4)t.right(167.04)t.pendown()t.fillcolor('yellow')t.begin_fill()t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第二個(gè)小五角星t.penup()t.goto(-110.1,220.7)t.right(94.83)t.pendown()t.fillcolor('yellow')t.begin_fill()t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第三個(gè)小五角星t.penup()t.goto(-96,128)t.right(170.13)t.pendown()t.fillcolor('yellow')t.begin_fill()t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第四個(gè)小五角星t.penup()t.goto(-148.7,61.9)t.right(164.66)t.pendown()t.fillcolor('yellow')t.begin_fill()t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#添加文字t.penup()t.color('red')t.goto(-100,-380)t.pendown()t.write("我愛你中國(guó)",font=("Times",30,"bold"))
第四步:通過觀察代碼,發(fā)現(xiàn)繪制五角星時(shí),其中有幾步是重復(fù)的,可以通過for語句進(jìn)行循環(huán),減少代碼數(shù)量 。
import turtle as t#隱藏畫筆的形狀t.hideturtle()#設(shè)置畫布大小t.screensize(1,1)#畫國(guó)旗背景圖t.color('red')t.fillcolor('red')t.penup()t.goto(-480,320)t.pendown()t.begin_fill()t.forward(960)t.right(90)t.forward(640)t.right(90)t.forward(960)t.right(90)t.forward(640)t.end_fill()#繪制中心五角星t.penup()t.goto(-320,256)t.right(162)t.pendown()t.color('yellow')t.fillcolor('yellow')t.begin_fill()for i in range(1,5):#循環(huán)4次t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.end_fill()#繪制右上第一個(gè)小五角星t.penup()t.goto(-147.5,285.4)t.right(167.04)t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1,5):#循環(huán)4次t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第二個(gè)小五角星t.penup()t.goto(-110.1,220.7)t.right(94.83)t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1,5):#循環(huán)4次t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第三個(gè)小五角星t.penup()t.goto(-96,128)t.right(170.13)t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1,5):#循環(huán)4次t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#繪制右上第四個(gè)小五角星t.penup()t.goto(-148.7,61.9)t.right(164.66)t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1,5):#循環(huán)4次t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#添加文字t.penup()t.color('red')t.goto(-100,-380)t.pendown()t.write("我愛你中國(guó)",font=("Times",30,"bold"))
第五步:再次觀察代碼,可以通過將for語句創(chuàng)建為函數(shù),并通過調(diào)用函數(shù)的形式繪制五角星,以此達(dá)到再次優(yōu)化代碼的效果 。
import turtle as t#隱藏畫筆的形狀t.hideturtle()#設(shè)置畫布大小t.screensize(1,1)#設(shè)置背景循環(huán)函數(shù)def bj():t.pendown()t.begin_fill()for i in range(1,3):t.forward(960)t.right(90)t.forward(640)t.right(90)t.end_fill()#設(shè)置中心五角星循環(huán)函數(shù)def wj1():t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1, 5): # 循環(huán)4次t.forward(69.7)t.left(72)t.forward(69.7)t.right(144)t.forward(69.7)t.left(72)t.forward(69.7)t.end_fill()#設(shè)置小五角星循環(huán)函數(shù)def wj2():t.pendown()t.fillcolor('yellow')t.begin_fill()for i in range(1, 5): # 循環(huán)4次t.forward(23.2)t.left(72)t.forward(23.2)t.right(144)t.forward(23.2)t.left(72)t.forward(23.2)t.end_fill()#畫國(guó)旗背景圖t.color('red')t.fillcolor('red')t.penup()t.goto(-480,320)bj()#繪制中心五角星t.penup()t.goto(-320,256)t.right(72)t.color('yellow')wj1()#繪制右上第一個(gè)小五角星t.penup()t.goto(-147.5,285.4)t.right(167.04)wj2()#繪制右上第二個(gè)小五角星t.penup()t.goto(-110.1,220.7)t.right(94.83)wj2()#繪制右上第三個(gè)小五角星t.penup()t.goto(-96,128)t.right(170.13)wj2()#繪制右上第四個(gè)小五角星t.penup()t.goto(-148.7,61.9)t.right(164.66)wj2()#添加文字t.penup()t.color('red')t.goto(-100,-380)t.pendown()t.write("我愛你中國(guó)",font=("Times",30,"bold"))
【python和turtle繪制國(guó)旗 python turtle繪制國(guó)旗】


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助: