使用turtle繪制五角星、分形樹
本文實例為大家分享了使用turtle繪制五角星和分形樹的具體代碼,供大家參考,具體內(nèi)容如下
turtle 庫
與之前程序的區(qū)別:
- 沒有顯示的input()與output()
- 沒有賦值語句
- 大部分語句為<a>.<b>()的形式
表示使用<a>中的方法<b>()
調(diào)用函數(shù)庫<a>中的函數(shù)<b>()
形狀繪制函數(shù):
turtle.forward(distance)
畫筆向前移動distance距離
turtle.backward(distance)
畫筆向后移動distance距離
turtle.right(dgree)
繪制方向向右旋轉(zhuǎn)dgree度
turtle.exitonclick()
點(diǎn)擊關(guān)閉圖形窗口
畫筆控制函數(shù):
turtle.penup()抬起畫筆,之后移動畫筆不繪制
turtle.pendown()落下畫筆,之后移動畫筆繪制形狀
turtle.pensize()設(shè)置畫筆寬度
turtle.pencolor()設(shè)置畫筆顏色,常用的顏色:
white,black,grey,darkgreen,vilot,purple
詳細(xì)API請參考
功能一:
五角星的繪制
""" 作者:陳潔 功能:五角星的繪制 版本:1.0 日期:04/10/2019 """ import turtle def main(): """ 主函數(shù) """ #計數(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): """ 繪制五角星 """ # 計數(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è)置條件語句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): """ 迭代繪制五角星 """ # 計數(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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用turtle庫繪制奧運(yùn)五環(huán)
turtle也叫海龜,是turtle繪圖體系的python實現(xiàn),這篇文章主要介紹了python使用turtle庫繪制奧運(yùn)五環(huán),本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02python創(chuàng)建多個logging日志文件的方法實現(xiàn)
本文主要介紹了python創(chuàng)建多個logging日志文件的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07win10+anaconda安裝yolov5的方法及問題解決方案
這篇文章主要介紹了win10+anaconda安裝yolov5的方法及問題解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04對django 模型 unique together的示例講解
今天小編就為大家分享一篇對django 模型 unique together的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08