詳解Python+Turtle繪制奧運標志的實現(xiàn)
最近了解了Python Turtle,非常簡單有趣。為了培養(yǎng)小朋友興趣,寫個turtle畫奧運標志程序。
1. 畫圓
turtle屬于內(nèi)置包,無需安裝。只要導入即可以畫圖,下面先寫幾行代碼畫圓。
import turtle t = turtle.Pen() t.circle(50) t.getscreen()._root.mainloop()
導入turtle之后,創(chuàng)建Pen畫筆t,避免后續(xù)代碼。circle方法是畫圓,最后是消息循環(huán),讓程序等待可以看到畫圖結(jié)果。
2. 畫奧運標識
除了畫圓方法,還有幾個方法需要了解。
setposition # 設(shè)置位置,畫布的中心位置是坐標0位置
penup() # 抬起筆,移動時不畫。
pendown() # 落筆,開始畫
請看下面代碼,很容易理解:
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()
僅通過移動坐標就能達到目的,但這個代碼不夠精簡,需要重構(gòu)。
3. 重構(gòu)代碼
每次畫圓,變化部分只有坐標,這里把坐標和半徑抽取為參數(shù),定義drawCircle方法,然后定義每個圓起始坐標并針對每個坐標調(diào)用drawCircle方法即可。請看代碼:
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__進行初始化,并設(shè)置畫筆為烏龜樣式。drawCircle方法定義畫圓過程,位置和半徑為參數(shù),半徑默認為50。drawOlympicSymbol方法先定義5個坐標列表,然后迭代調(diào)用drawCircle畫圓,即完成了畫奧運標識。
4. 美化標識
你可能覺得標識有點單調(diào),沒有顏色。我需要加上藍色、黑色、紅色和下面黃色和綠色,也要把畫筆加粗點,最后在畫上北京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方法中給每個坐標增加顏色元素。
drawText方法通過write方法畫文字,其他代碼基本一樣。
5. 總結(jié)
turtle非常簡單吧,如果需要更深入了解或想畫一些更漂亮、復雜的圖形,參考官方文檔。
以上就是詳解Python+Turtle繪制奧運標志的實現(xiàn)的詳細內(nèi)容,更多關(guān)于Python Turtle奧運標志的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實現(xiàn)給微信公眾號發(fā)送消息的方法
這篇文章主要介紹了python實現(xiàn)給微信公眾號發(fā)送消息的方法,結(jié)合實例形式分析了Python針對微信公眾號接口操作的相關(guān)技巧,需要的朋友可以參考下2017-06-06利用python對月餅數(shù)據(jù)進行可視化(看看哪家最劃算)
通過python對數(shù)據(jù)進行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對月餅數(shù)據(jù)進行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09詳解Python中如何添加Selenium WebDriver等待
Selenium Web 驅(qū)動程序提供兩種類型的等待, 第一個是隱式等待,第二個是顯式等待,本文主要為大家介紹了Python如何在Selenium Web驅(qū)動程序中添加這兩種等待,需要的可以參考下2023-11-11