Python使用Turtle模塊繪制國旗的方法示例
turtle模塊:python內(nèi)置的繪圖工具
turtle(海龜)模塊,我們是用它來進行畫圖的,基本上就是畫簡單的直線,點,和曲線。
你可以把它想成一個小海龜,在沙灘上行走,然后留下的各種痕跡,使用Turtle模塊可以繪制很多精美的圖形。
基本操作(Turtle方法) turtle.forward(step):前進step個像素 turtle.back(step):后退step個像素 turtle.right():右轉(zhuǎn)一個角度 turtle.left():左轉(zhuǎn)一個角度 turtle.pencolor(“string”):畫筆顏色 turtle.fillcolor(“string”):填充顏色 turtle.speed(int):運動速度其他的turtle方法可以參見python官網(wǎng)
https://docs.python.org/zh-cn/3.7/library/turtle.html
具體代碼實現(xiàn)# 繪畫# 中國國旗# 轉(zhuǎn)載請標明出處!!import turtleimport timedef draw__stars(tur, step, x, y, arg): ''' 繪制五角星 :param tur: turtle object :param step: 五角星一條邊的長度 :param x: 開始繪制五角星的起點x坐標 :param y: 開始繪制五角星的起點y坐標 :param arg: :return: ''' tur.pencolor(’yellow’) tur.fillcolor(’yellow’) tur.up() tur.goto(x, y) tur.begin_fill() tur.down() tur.right(arg) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.forward(step) tur.right(144) tur.end_fill()def draw__flag(tur, wide, height): ''' 繪制國旗的長方形形狀 :param tur: turtle object :param wide: the width of the flag :param height: the height of the flag :return: None ''' tur.pencolor(’red’) tur.fillcolor(’red’) tur.goto(- wide / 2, height / 2) tur.begin_fill() tur.forward(wide) tur.right(90) tur.forward(height) tur.right(90) tur.forward(wide) tur.right(90) tur.forward(height) tur.end_fill()if __name__ == ’__main__’: ''' main 函數(shù) ''' # tur = turtle.Turtle() turtle.screensize(canvwidth=3000, canvheight=2000, bg=None) # 繪制star的turtle對象 tur_star = turtle.Turtle() # 繪制flag的turtle對象 tur_flag = turtle.Turtle() tur_flag.speed(3) tur_star.speed(3) # 隱藏turtle對象 tur_star.hideturtle() tur_flag.hideturtle() # 間隔3秒,可以沒有,這個是我調(diào)試時加上去的 time.sleep(3) # 繪制長方形 draw__flag(tur_flag, 630, 420) # 繪制五角星,在合適的位置進行繪制五角星 # 調(diào)用五次函數(shù)繪制五顆五角星 draw__stars(tur_star, step=60, x=-280, y=155, arg=0) draw__stars(tur_star, step=25, x=-182, y=177, arg=- 25) draw__stars(tur_star, step=25, x=-175, y=125, arg=41) draw__stars(tur_star, step=25, x=-208, y=79, arg=23) draw__stars(tur_star, step=25, x=-265, y=75, arg=48) # 使畫面鎖定 turtle.done()
運行結(jié)果
到此這篇關(guān)于Python使用Turtle模塊繪制國旗的文章就介紹到這了,更多相關(guān)Python Turtle模塊繪制國旗內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過程和條件語句)2. php使用正則驗證密碼字段的復(fù)雜強度原理詳細講解 原創(chuàng)3. jscript與vbscript 操作XML元素屬性的代碼4. Jsp servlet驗證碼工具類分享5. XML在語音合成中的應(yīng)用6. 基于PHP做個圖片防盜鏈7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)8. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)9. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)10. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
