使用Python的turtle模塊畫國(guó)旗
Python的turtle模塊畫國(guó)旗主要用到兩個(gè)函數(shù):draw_rentangle和draw_star。
至于函數(shù)的調(diào)用就和我們學(xué)的C,C++是一樣的。對(duì)于turtle畫國(guó)旗的程序中,首先是查找國(guó)旗的畫法,才能用程序?qū)崿F(xiàn)。自己在實(shí)現(xiàn)的過程中主要是對(duì)turtle.circle()沒有準(zhǔn)確掌握,所以花了一些不必要的時(shí)間。turtle.circle畫弧時(shí),海龜(turtle)的方向就是弧的切線方向,也就是說turtle的垂直方向就是圓心在的直線上,給定參數(shù)radius就可以畫了,程序中第二注意的地方就是小五角星和大五角星的位置關(guān)系,主要是程序中的turtle.left(turtle.towards(center_x,center_y)-turtle.heading()),當(dāng)然,我看有的人用了round()函數(shù)來獲取近似值,但是,默認(rèn)的已經(jīng)足夠了。下面是本人寫的程序和結(jié)果演示。
import time
import turtle
import os
'''
想要學(xué)習(xí)Python?Python學(xué)習(xí)交流群:984632579滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載!
'''
def draw_rectangle(start_x,start_y,rec_x,rec_y):
turtle.goto(start_x,start_y)
turtle.color('red')
turtle.fillcolor('red')
turtle.begin_fill()
for i in range(2):
turtle.forward(rec_x)
turtle.left(90)
turtle.forward(rec_y)
turtle.left(90)
turtle.end_fill()
def draw_star(center_x,center_y,radius):
turtle.setpos(center_x,center_y)
#find the peak of the five-pointed star
pt1=turtle.pos()
turtle.circle(-radius,72)
pt2=turtle.pos()
turtle.circle(-radius,72)
pt3=turtle.pos()
turtle.circle(-radius,72)
pt4=turtle.pos()
turtle.circle(-radius,72)
pt5=turtle.pos()
#draw the five-pointed star
turtle.color('yellow','yellow')
turtle.fill(True)
turtle.goto(pt3)
turtle.goto(pt1)
turtle.goto(pt4)
turtle.goto(pt2)
turtle.goto(pt5)
turtle.fill(False)
#start the project
turtle.speed(5)
turtle.penup()
#draw the rectangle
star_x=-320
star_y=-260
len_x=660
len_y=440
draw_rectangle(star_x,star_y,len_x,len_y)
#draw the big star
pice=660/30
big_center_x=star_x+5*pice
big_center_y=star_y+len_y-pice*5
turtle.goto(big_center_x,big_center_y)
turtle.left(90)
turtle.forward(pice*3)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice*3)
#draw the small star
turtle.goto(star_x+10*pice,star_y+len_y-pice*2)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the second star
turtle.goto(star_x+pice*12,star_y+len_y-pice*4)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the third
turtle.goto(star_x+pice*12,star_y+len_y-7*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
#draw the final
turtle.goto(star_x+pice*10,star_y+len_y-9*pice)
turtle.left(turtle.towards(big_center_x,big_center_y)-turtle.heading())
turtle.forward(pice)
turtle.right(90)
draw_star(turtle.xcor(),turtle.ycor(),pice)
turtle.ht()
time.sleep(3)
os._exit(1)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python OpenCV之圖片縮放的實(shí)現(xiàn)(cv2.resize)
這篇文章主要介紹了Python OpenCV之圖片縮放的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Python實(shí)現(xiàn)前向和反向自動(dòng)微分的示例代碼
自動(dòng)微分技術(shù)(稱為“automatic differentiation, autodiff”)是介于符號(hào)微分和數(shù)值微分的一種技術(shù),它是在計(jì)算效率和計(jì)算精度之間的一種折衷。本文主要介紹了Python如何實(shí)現(xiàn)前向和反向自動(dòng)微分,需要的可以參考一下2022-12-12
使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法
今天小編就為大家分享一篇使用Windows批處理和WMI設(shè)置Python的環(huán)境變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例
本文主要介紹了python 邊緣擴(kuò)充方式的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Python中內(nèi)置函數(shù)filter函數(shù)用法詳解
filter()函數(shù)是Python內(nèi)置的另一個(gè)有用的高階函數(shù),filter()函數(shù)接收一個(gè)函數(shù)f和一個(gè)序列,函數(shù)f的作用是對(duì)每個(gè)元素進(jìn)行判斷,返回True或False,下面這篇文章主要給大家介紹了關(guān)于Python中內(nèi)置函數(shù)filter函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2024-05-05
Python多進(jìn)程Process和管道Pipe的使用方式
這篇文章主要介紹了Python多進(jìn)程Process和管道Pipe的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
Python內(nèi)置庫之webbrowser模塊用法詳解
webbrowser模塊是Python自帶的標(biāo)準(zhǔn)庫,無需安裝,可以直接在Python中使用該模塊來打開網(wǎng)頁、PDF文件等,本文給大家詳細(xì)介紹了Python webbrowser模塊用法,需要的朋友可以參考下2023-08-08
Django框架視圖層URL映射與反向解析實(shí)例分析
這篇文章主要介紹了Django框架視圖層URL映射與反向解析,結(jié)合實(shí)例形式分析了Django框架普通url映射、命名URL參數(shù)映射、分布式URL映射、反向解析等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
2021年的Python 時(shí)間軸和即將推出的功能詳解
這篇文章主要介紹了2021年的Python 時(shí)間軸和即將推出的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
NVIDIA安裝CUDA的實(shí)現(xiàn)(圖文教程)
本文主要介紹了NVIDIA安裝CUDA的實(shí)現(xiàn),包括系統(tǒng)要求、軟件下載、安裝步驟以及常見問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

