import turtle //導入模塊
成都創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為湘潭企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站制作,湘潭網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
import time
import os
def draw_square(org_x, org_y, x, y): //定義紅旗繪制函數(shù)
turtle.setpos(org_x, org_y) //定義畫筆初始位置
turtle.color('red', 'red') //顏色
turtle.begin_fill() //開始繪制
turtle.fd(x) //繪制偏轉(zhuǎn)方向和角度
turtle.lt(90)
turtle.fd(y)
turtle.lt(90)
turtle.fd(x)
turtle.lt(90)
turtle.fd(y)
turtle.end_fill() //繪制結(jié)束
def draw_star(center_x, center_y, radius): //定義星星繪制函數(shù)
print(center_x, center_y) //顯示位置
turtle.pencolor('black') //畫筆軌跡顏色
turtle.setpos(center_x, center_y) //中心點位置
pt1 = turtle.pos() //偏轉(zhuǎn)角度計算
turtle.circle(-radius, 360 / 5)
pt2 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt3 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt4 = turtle.pos()
turtle.circle(-radius, 360 / 5)
pt5 = turtle.pos()
turtle.color('yellow', 'yellow') //星星顏色
turtle.begin_fill() //開是繪制
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.end_fill() //繪制結(jié)束
print(turtle.pos())
turtle.pu() //隱藏畫筆軌跡
draw_square(-320, -260, 660, 440) //繪制紅旗
star_part_x = -320 //自定義星星大小等屬性
star_part_y = -260 + 440
star_part_s = 660 / 30
center_x, center_y = star_part_x + star_part_s * 5, star_part_y - star_part_s * 5 //計算星星中心點位置
turtle.setpos(center_x, center_y)
turtle.lt(90)
draw_star(star_part_x + star_part_s * 5, star_part_y - star_part_s * 2, star_part_s * 3) //繪制星星
turtle.goto(star_part_x + star_part_s * 10, star_part_y - star_part_s * 2) //同上
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 12, star_part_y - star_part_s * 4)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 12, star_part_y - star_part_s * 7)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.goto(star_part_x + star_part_s * 10, star_part_y - star_part_s * 9)
turtle.lt(round(turtle.towards(center_x, center_y)) - turtle.heading())
turtle.fd(star_part_s)
turtle.rt(90)
draw_star(turtle.xcor(), turtle.ycor(), star_part_s)
turtle.ht()
time.sleep(5) //設(shè)置掛起時間
os._exit(1)
把整個國旗換成直角坐標系。
在Python中繪制標準國旗并不簡單,我們采用的方法在數(shù)學上稱為解析法。把整個國旗換成直角坐標系,中心坐標為(0,0)。每個小格邊長20,則國旗左上角坐標為(-300,200)、國旗長600,高400。Turtle是小海龜繪圖庫,Math是數(shù)學庫,要用到里面的三角函數(shù)和反三角函數(shù),以及圓周率pi值。
Python是一種解釋型、面向?qū)ο?、動態(tài)數(shù)據(jù)類型的高級程序設(shè)計語言。 Python于1989年底發(fā)明,第一個公開發(fā)行版發(fā)行于1991年。
#?python6.6
import?turtle
def?test():
#?加納共和國國旗呈長方形,長與寬之比為3∶2。
#?自上而下由紅、黃、綠三個平行相等的橫長方形組成,黃色部分中間是一顆黑色五角星。
flag_h?=?300
flag_w?=?450
star_h?=?flag_h/3
turtle.pensize(2)
turtle.speed(5)
turtle.hideturtle()
def?draw_rectangle(color):
turtle.pencolor(color)
turtle.fillcolor(color)
turtle.pendown()
turtle.begin_fill()
turtle.forward(flag_w)
turtle.right(90)
turtle.forward(star_h)
turtle.right(90)
turtle.forward(flag_w)
turtle.right(90)
turtle.forward(star_h)
turtle.end_fill()
turtle.penup()
turtle.back(star_h)
turtle.right(90)
turtle.penup()
turtle.goto(-flag_w?/?2,?flag_h?/?2)
draw_rectangle("red")
draw_rectangle("yellow")
draw_rectangle("green")
#?五角星
turtle.penup()
turtle.goto(0,?star_h/2)
turtle.pencolor("black")
turtle.fillcolor("black")
turtle.right(90-18)
turtle.pendown()
turtle.begin_fill()
for?i?in?range(5):
turtle.forward(star_h)
turtle.right(180-36)
turtle.end_fill()
turtle.done()
if?__name__?==?"__main__":
test()