詳解Python+Turtle繪制奧運(yùn)標(biāo)志的實(shí)現(xiàn)
最近了解了Python Turtle,非常簡(jiǎn)單有趣。為了培養(yǎng)小朋友興趣,寫個(gè)turtle畫奧運(yùn)標(biāo)志程序。
1. 畫圓
turtle屬于內(nèi)置包,無需安裝。只要導(dǎo)入即可以畫圖,下面先寫幾行代碼畫圓。
import turtle t = turtle.Pen() t.circle(50) t.getscreen()._root.mainloop()
導(dǎo)入turtle之后,創(chuàng)建Pen畫筆t,避免后續(xù)代碼。circle方法是畫圓,最后是消息循環(huán),讓程序等待可以看到畫圖結(jié)果。
2. 畫奧運(yùn)標(biāo)識(shí)
除了畫圓方法,還有幾個(gè)方法需要了解。
setposition # 設(shè)置位置,畫布的中心位置是坐標(biāo)0位置
penup() # 抬起筆,移動(dòng)時(shí)不畫。
pendown() # 落筆,開始畫
請(qǐng)看下面代碼,很容易理解:
import turtle t = turtle.Pen() t.circle(50) t.penup() t.setposition(-120, 0) t.pendown() t.circle(50) t.penup() t.setposition(60, 60) t.pendown() t.circle(50) t.penup() t.setposition(-60, 60) t.pendown() t.circle(50) t.penup() t.setposition(-180, 60) t.pendown() t.circle(50) t.getscreen()._root.mainloop()
僅通過移動(dòng)坐標(biāo)就能達(dá)到目的,但這個(gè)代碼不夠精簡(jiǎn),需要重構(gòu)。
3. 重構(gòu)代碼
每次畫圓,變化部分只有坐標(biāo),這里把坐標(biāo)和半徑抽取為參數(shù),定義drawCircle方法,然后定義每個(gè)圓起始坐標(biāo)并針對(duì)每個(gè)坐標(biāo)調(diào)用drawCircle方法即可。請(qǐng)看代碼:
import turtle class DrawAoYun(turtle.Turtle): """Draw Olympics logo""" def __init__(self): """DrawAoYun Constructor""" turtle.Turtle.__init__(self, shape="turtle") def drawCircle(self, x, y, radius=50): """ Moves the turtle to the correct position and draws a circle """ self.penup() self.setposition(x, y) self.pendown() self.circle(radius) def drawOlympicSymbol(self): """ Iterates over a set of positions to draw the Olympics logo """ positions = [(0, 0), (-120, 0), (60, 60), (-60, 60), (-180, 60)] for pos in positions: self.drawCircle(pos[0], pos[1]) if __name__ == "__main__": t = DrawAoYun() t.drawOlympicSymbol() turtle.getscreen()._root.mainloop()
這里定義類,繼承turtle.Turtle,構(gòu)造函數(shù)中調(diào)用父類__init__進(jìn)行初始化,并設(shè)置畫筆為烏龜樣式。drawCircle方法定義畫圓過程,位置和半徑為參數(shù),半徑默認(rèn)為50。drawOlympicSymbol方法先定義5個(gè)坐標(biāo)列表,然后迭代調(diào)用drawCircle畫圓,即完成了畫奧運(yùn)標(biāo)識(shí)。
4. 美化標(biāo)識(shí)
你可能覺得標(biāo)識(shí)有點(diǎn)單調(diào),沒有顏色。我需要加上藍(lán)色、黑色、紅色和下面黃色和綠色,也要把畫筆加粗點(diǎn),最后在畫上北京2008的文字。
import turtle class DrawAoYun(turtle.Turtle): """Draw Olympics logo""" def __init__(self): """DrawAoYun Constructor""" turtle.Turtle.__init__(self, shape="turtle") self.width(5) def drawCircle(self, x, y, color,radius=50): """ Moves the turtle to the correct position and draws a circle """ self.penup() self.setposition(x, y) self.pendown() self.color(color) self.circle(radius) def drawOlympicSymbol(self): """ Iterates over a set of positions to draw the Olympics logo """ positions = [(0, 0, "green"), (-120, 0, "yellow"), (60, 60, "red"), (-60, 60, "black"), (-180, 60, "blue")] for x, y, color in positions: self.drawCircle(x, y, color) def drawText(self): """ Draw text to the screen """ self.penup() self.setposition(-120, 180) self.pendown() self.color("black") self.width(1) self.write("Beijing 2008", font=("Arial", 16, "bold")) if __name__ == "__main__": t = DrawAoYun() t.drawOlympicSymbol() t.drawText() turtle.getscreen()._root.mainloop()
構(gòu)造函數(shù)中通過width方法設(shè)置為5。drawCircle方法增加顏色參數(shù),每次畫之前使用self.color(color)設(shè)置顏色。drawOlympicSymbol方法中給每個(gè)坐標(biāo)增加顏色元素。
drawText方法通過write方法畫文字,其他代碼基本一樣。
5. 總結(jié)
turtle非常簡(jiǎn)單吧,如果需要更深入了解或想畫一些更漂亮、復(fù)雜的圖形,參考官方文檔。
以上就是詳解Python+Turtle繪制奧運(yùn)標(biāo)志的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Turtle奧運(yùn)標(biāo)志的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)給微信公眾號(hào)發(fā)送消息的方法
這篇文章主要介紹了python實(shí)現(xiàn)給微信公眾號(hào)發(fā)送消息的方法,結(jié)合實(shí)例形式分析了Python針對(duì)微信公眾號(hào)接口操作的相關(guān)技巧,需要的朋友可以參考下2017-06-06利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過python對(duì)數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過程解析
這篇文章主要介紹了Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08詳解Python中如何添加Selenium WebDriver等待
Selenium Web 驅(qū)動(dòng)程序提供兩種類型的等待, 第一個(gè)是隱式等待,第二個(gè)是顯式等待,本文主要為大家介紹了Python如何在Selenium Web驅(qū)動(dòng)程序中添加這兩種等待,需要的可以參考下2023-11-11