詳解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中三元運(yùn)算符的簡(jiǎn)潔性及多用途實(shí)例探究
這篇文章主要為大家介紹了Python中三元運(yùn)算符的簡(jiǎn)潔性及多用途實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南)
這篇文章主要介紹了WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Tornado 多進(jìn)程實(shí)現(xiàn)分析詳解
這篇文章主要介紹了Tornado 多進(jìn)程實(shí)現(xiàn)分析詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python實(shí)現(xiàn)手寫一個(gè)類似django的web框架示例
這篇文章主要介紹了Python實(shí)現(xiàn)手寫一個(gè)類似django的web框架,結(jié)合具體實(shí)例形式分析了Python自定義簡(jiǎn)單控制器、URL路由、視圖模型等功能,實(shí)現(xiàn)類似Django框架的web應(yīng)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
使用python本地部署DeepSeek運(yùn)行時(shí)報(bào)錯(cuò)?OSError:?[WinError?193]?%1?不是
文章介紹了在本地使用Python部署DeepSeek時(shí)遇到的OSError:?[WinError?193]?錯(cuò)誤,通過檢查錯(cuò)誤信息,發(fā)現(xiàn)與numpy版本有關(guān),解決方法是卸載并重新安裝numpy,最終,問題得到解決,感興趣的朋友跟隨小編一起看看吧2025-02-02
Python with語句上下文管理器兩種實(shí)現(xiàn)方法分析
這篇文章主要介紹了Python with語句上下文管理器兩種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python上下文管理器的相關(guān)概念、功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-02-02

