使用turtle繪制五角星、分形樹
本文實(shí)例為大家分享了使用turtle繪制五角星和分形樹的具體代碼,供大家參考,具體內(nèi)容如下
turtle 庫(kù)
與之前程序的區(qū)別:
- 沒(méi)有顯示的input()與output()
- 沒(méi)有賦值語(yǔ)句
- 大部分語(yǔ)句為<a>.<b>()的形式
表示使用<a>中的方法<b>()
調(diào)用函數(shù)庫(kù)<a>中的函數(shù)<b>()
形狀繪制函數(shù):
turtle.forward(distance)
畫筆向前移動(dòng)distance距離
turtle.backward(distance)
畫筆向后移動(dòng)distance距離
turtle.right(dgree)
繪制方向向右旋轉(zhuǎn)dgree度
turtle.exitonclick()
點(diǎn)擊關(guān)閉圖形窗口

畫筆控制函數(shù):
turtle.penup()抬起畫筆,之后移動(dòng)畫筆不繪制
turtle.pendown()落下畫筆,之后移動(dòng)畫筆繪制形狀
turtle.pensize()設(shè)置畫筆寬度
turtle.pencolor()設(shè)置畫筆顏色,常用的顏色:
white,black,grey,darkgreen,vilot,purple
詳細(xì)API請(qǐng)參考
功能一:
五角星的繪制
""" 作者:陳潔 功能:五角星的繪制 版本:1.0 日期:04/10/2019 """ import turtle def main(): """ 主函數(shù) """ #計(jì)數(shù)器 count = 1 while count<=5: turtle.forward(100) turtle.right(144) count += 1 turtle.exitonclick() if __name__ == '__main__': main()
遇到困難:
混淆if條件函數(shù)與while循環(huán)函數(shù)
功能二:
加入循環(huán)操作繪制重復(fù)不同大小的五角星
"""
作者:陳潔
功能:五角星的繪制
版本:2.0
日期:04/10/2019
新增功能:加入循環(huán)操作繪制重復(fù)不同大小的五角星
"""
import turtle
def draw_pentagram(size):
"""
繪制五角星
"""
# 計(jì)數(shù)器
count = 1
# 繪制五角星
while count <= 5:
turtle.forward(size)
turtle.right(144)
count += 1
def main():
"""
主函數(shù)
"""
turtle.penup()
turtle.backward(200)
turtle.pendown()
turtle.pensize(2)
turtle.pencolor('red')
size = 50
while size<=100:
#調(diào)用函數(shù)
draw_pentagram(size)
size += 10
turtle.exitonclick()
if __name__ == '__main__':
main()
功能三:
使用迭代函數(shù)繪制重復(fù)不同大小的五角星
注意:設(shè)置條件語(yǔ)句if size <= 100: draw_recursive_pentagram(size)
"""
作者:陳潔
功能:五角星的繪制
版本:3.0
日期:04/10/2019
新增功能:使用迭代函數(shù)繪制重復(fù)不同大小的五角星
"""
import turtle
def draw_pentagram(size):
"""
繪制五角星
"""
def draw_recursive_pentagram(size):
"""
迭代繪制五角星
"""
# 計(jì)數(shù)器
count = 1
# 繪制五角星
while count <= 5:
turtle.forward(size)
turtle.right(144)
count += 1
#五角星繪制完成,更新參數(shù)
size += 10
if size <= 100:
draw_recursive_pentagram(size)
def main():
"""
主函數(shù)
"""
turtle.penup()
turtle.backward(200)
turtle.pendown()
turtle.pensize(2)
turtle.pencolor('red')
size = 50
draw_recursive_pentagram(size)
if __name__ == '__main__':
main()
功能四:用迭代函數(shù)繪制分形樹
"""
作者:陳潔
功能:分形樹
版本:1.0
日期:04/10/2019
新增功能:使用迭代函數(shù)繪制分形樹
"""
import turtle
def draw_branch (branch_length):
"""
繪制分形樹
"""
if branch_length >5:
#繪制右側(cè)樹枝
turtle.forward(branch_length)
print('向前繪制',branch_length)
turtle.right(20)
print('右轉(zhuǎn)',20)
draw_branch(branch_length - 15)
#繪制左側(cè)樹枝
turtle.left(40)
print('左轉(zhuǎn)',40)
draw_branch(branch_length - 15)
#返回之前的樹枝
turtle.right(20)
print('右轉(zhuǎn)',20)
turtle.backward(branch_length)
def main():
"""
主函數(shù)
"""
turtle.left(90)
turtle.penup()
turtle.backward(150)
turtle.pendown()
draw_branch(100)
turtle.exitonclick()
if __name__ == '__main__':
main()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用turtle庫(kù)繪制奧運(yùn)五環(huán)
turtle也叫海龜,是turtle繪圖體系的python實(shí)現(xiàn),這篇文章主要介紹了python使用turtle庫(kù)繪制奧運(yùn)五環(huán),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02
python創(chuàng)建多個(gè)logging日志文件的方法實(shí)現(xiàn)
本文主要介紹了python創(chuàng)建多個(gè)logging日志文件的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
使用Python自動(dòng)化破解自定義字體混淆信息的方法實(shí)例
今天小編就為大家分享一篇關(guān)于使用Python自動(dòng)化破解自定義字體混淆信息的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
對(duì)Keras自帶Loss Function的深入研究
這篇文章主要介紹了對(duì)Keras自帶Loss Function的深入研究,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
使用Python來(lái)做一個(gè)屏幕錄制工具的操作代碼
本文給大家分享使用Python來(lái)做一個(gè)屏幕錄制工具,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python實(shí)戰(zhàn)練習(xí)之終于對(duì)肯德基下手
讀萬(wàn)卷書不如行萬(wàn)里路,學(xué)的扎不扎實(shí)要通過(guò)實(shí)戰(zhàn)才能看出來(lái),本篇文章手把手帶你爬下肯德基的官網(wǎng),大家可以在過(guò)程中查缺補(bǔ)漏,看看自己掌握程度怎么樣2021-10-10
win10+anaconda安裝yolov5的方法及問(wèn)題解決方案
這篇文章主要介紹了win10+anaconda安裝yolov5的方法及問(wèn)題解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
python中return的返回和執(zhí)行實(shí)例
今天小編就為大家分享一篇python中return的返回和執(zhí)行實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
對(duì)django 模型 unique together的示例講解
今天小編就為大家分享一篇對(duì)django 模型 unique together的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08

