欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python趣味挑戰(zhàn)之turtle庫(kù)繪畫飄落的銀杏樹

 更新時(shí)間:2021年05月31日 15:12:11   作者:yunyun云蕓  
銀杏還是和恐龍同時(shí)代的植物,被稱為活化石,適應(yīng)能力強(qiáng),生長(zhǎng)期漫長(zhǎng),壽命可達(dá)千年.因此,銀杏是長(zhǎng)壽的代表和象征,接下來用Python的turtle庫(kù)來繪畫銀杏樹唯美的一幕,需要的朋友可以參考下

一、導(dǎo)入所需的庫(kù)

import turtle

import random

from math import *

二、生成斐波那契數(shù)列

斐波那契數(shù)列是指前兩項(xiàng)的和加起來等于后一項(xiàng)的一個(gè)數(shù)列,這里使用了兩個(gè)函數(shù)來生成斐波契那數(shù)列。

def Fibonacci_Recursion_tool(n):  #斐波那契數(shù)列方法
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2)
def Fibonacci_Recursion(n):     #生成斐波那契數(shù)列,并存入列表
    result_list = []
    for i in range(1, n + 3):
        result_list.append(Fibonacci_Recursion_tool(i))
    return result_list

調(diào)用函數(shù)生成一個(gè)數(shù)列如下:

yu = Fibonacci_Recursion(top)  #生成斐波契那數(shù)列
print(yu)

運(yùn)行結(jié)果如下:

在這里插入圖片描述

三、定義生成葉子的方法

def leaf(x, y, node):#定義畫葉子的方法
    til = turtle.heading()
    i = random.random()
    an = random.randint(10, 180)
    ye = random.randint(6, 9)/10
    turtle.color(ye, ye*0.9, 0)
    turtle.fillcolor(ye+0.1, ye+0.05, 0)
    turtle.pensize(1)
    turtle.pendown()
    turtle.setheading(an + 90)
    turtle.forward(8*i)
    px = turtle.xcor()
    py = turtle.ycor()
    turtle.begin_fill()
    turtle.circle(7.5*i, 120)  # 畫一段120度的弧線
    turtle.penup()  # 抬起筆來
    turtle.goto(px, py)  # 回到圓點(diǎn)位置
    turtle.setheading(an + 90)  # 向上畫
    turtle.pendown()  # 落筆,開始畫
    turtle.circle(-7.5*i, 120)  # 畫一段120度的弧線
    turtle.setheading(an + 100)
    turtle.circle(10.5*i, 150)
    turtle.end_fill()  # 畫一段150度的弧線
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(til)
    turtle.pensize(node / 2 + 1)

四、定義生成樹的方法

在這里插入圖片描述

這里用x生成隨機(jī)數(shù),用if條件進(jìn)行判斷來決定要不要繼續(xù)畫分支,要不要畫葉子,使樹更加自然,無(wú)規(guī)律性,更好看一點(diǎn),這樣會(huì)導(dǎo)致你每次運(yùn)行時(shí),畫出來的樹都是不一樣的。具體的細(xì)節(jié),我已經(jīng)加上了注釋。如果想調(diào)整空中葉子的比例,樹的分叉程度,修改if判斷語(yǔ)句中的x取值范圍,以增加概率或減小概率即可。至于如何達(dá)到你心中完美的效果就要慢慢去嘗試了。

def draw(node, length, level, yu, button):  #定義畫樹的方法
    turtle.pendown()
    t = cos(radians(turtle.heading()+5)) / 8 + 0.25
    turtle.pencolor(t*1.6, t*1.2, t*1.4) #(r, g, b)顏色對(duì)應(yīng)的RGB值
    turtle.pensize(node/1.2)  #畫筆的尺寸
    x = random.randint(0, 10)  #生成隨機(jī)數(shù)決定要畫樹枝還是畫飄落的葉子
    if level == top and x > 6:  #此時(shí)畫飄落的葉子,x范圍太大會(huì)導(dǎo)致樹太禿
        turtle.forward(length)  # 畫樹枝
        yu[level] = yu[level] - 1
        c = random.randint(2, 10)
        for i in range(1, c):
            leaf(turtle.xcor(), turtle.ycor(), node)
           # 添加0.3倍的飄落葉子
            if random.random() > 0.3:
                turtle.penup()
               # 飄落
                t1 = turtle.heading()
                an1 = -40 + random.random() * 40
                turtle.setheading(an1)
                dis = int(800 * random.random() * 0.5 + 400 * random.random() * 0.3 + 200 * random.random() * 0.2)
                turtle.forward(dis)
                turtle.setheading(t1)
                turtle.right(90)
               # 畫葉子
                leaf(turtle.xcor(), turtle.ycor(), node)
                turtle.left(90)
               # 返回
                t2 = turtle.heading()
                turtle.setheading(an1)
                turtle.backward(dis)
                turtle.setheading(t2)
    elif level==top and x < 7 : #此時(shí)畫枝葉,x范圍太大會(huì)導(dǎo)致飄落的葉子太少
        turtle.penup()
        turtle.forward(length)
    elif level>3 and (x>6) :#三級(jí)樹枝以上,有40%的概率執(zhí)行以下策略
        turtle.pendown()
        turtle.forward(length)
        c = random.randint(4, 6)
        for i in range(3, c):
            leaf(turtle.xcor(), turtle.ycor(),node)
        leaf(turtle.xcor(), turtle.ycor(),node)
        button=1# jump"""
    else:
        turtle.forward(length)  # 畫樹枝
        yu[level] = yu[level] -1
    if node > 0 and button == 0:
        # 計(jì)算右側(cè)分支偏轉(zhuǎn)角度,在固定角度偏轉(zhuǎn)增加一個(gè)隨機(jī)的偏移量
        right = random.random() * 5 + 17
        # 計(jì)算左側(cè)分支偏轉(zhuǎn)角度,在固定角度偏轉(zhuǎn)增加一個(gè)隨機(jī)的偏移量
        left = random.random() * 20 + 19
        # 計(jì)算下一級(jí)分支的長(zhǎng)度
        child_length = length * (random.random() * 0.25 + 0.7)
        # 右轉(zhuǎn)一定角度,畫右分支
        r=random.randint(0, 1)
        if r==1:
          turtle.right(right)
          level = level + 1
          #print("level", level)
        else:
          turtle.left(right)
          level = level + 1
          #print("level", level)
        draw(node - 1, child_length,level,yu,button)
        yu[level] = yu[level] +1
        if yu[level] > 1:
            # 左轉(zhuǎn)一定角度,畫左分支
            if r==1:
               turtle.left(right + left)
               draw(node - 1, child_length, level, yu,button)
               # 將偏轉(zhuǎn)的角度,轉(zhuǎn)回
               turtle.right(left)
               yu[level] = yu[level] - 1
            else:
                turtle.right(right + left)
                draw(node - 1, child_length, level, yu,button)
                # 將偏轉(zhuǎn)的角度,轉(zhuǎn)回
                turtle.left(left)
                yu[level] = yu[level] - 1
        else:
            if r==1:
              turtle.left(right + left)
              turtle.right(left)
            else:
                turtle.right(right + left)
                turtle.left(left)
    turtle.penup()
    #退回到上一級(jí)節(jié)點(diǎn)頂部位置
    turtle.backward(length)
    
5.主函數(shù)部分
主函數(shù)中直接調(diào)用上述函數(shù)就行,top控制樹的高度,turtle.speed控制畫的速度,最后的turtle.write()用來書寫最下方的簽名。

```clike
if __name__ == '__main__':
    turtle.setup(width=1.0, height=1.0) #設(shè)置全屏顯示
    turtle.hideturtle()  # 隱藏turtle
    turtle.speed(0)  # 設(shè)置畫筆移動(dòng)的速度,0-10 值越小速度越快
    # turtle.tracer(0,0)      #設(shè)置動(dòng)畫的開關(guān)和延遲,均為0
    turtle.penup()  # 抬起畫筆
    turtle.left(90)  # 默認(rèn)方向?yàn)槌痻軸的正方向,左轉(zhuǎn)90度則朝上
    turtle.backward(300)  # 設(shè)置turtle的位置,朝下移動(dòng)300
    top = 9  #樹高
    yu = Fibonacci_Recursion(top)  #生成斐波契那數(shù)列
    yu.remove(yu[0])
    #print(yu) 打印斐波那契數(shù)列
    button = 0
    draw(top, 120, 0, yu, button)  # 調(diào)用函數(shù)開始繪制
    turtle.write("      wsw", font=("微軟雅黑", 14, "normal")) #生成簽名
    turtle.done()

運(yùn)行程序后,“海龜”會(huì)幫你畫出整棵樹,你只需要看著它畫就行,需要等待一定的時(shí)間,最后的一種成品如下,是想要的一半葉子在空中的感覺了,哈哈哈哈~

在這里插入圖片描述

到此這篇關(guān)于Python趣味挑戰(zhàn)之turtle庫(kù)繪畫飄落的銀杏樹的文章就介紹到這了,更多相關(guān)turtle庫(kù)繪畫飄落的銀杏樹內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python的PIL模塊來進(jìn)行圖片對(duì)比

    使用Python的PIL模塊來進(jìn)行圖片對(duì)比

    這篇文章主要介紹了使用Python的PIL模塊來進(jìn)行圖片對(duì)比的方法,搜索引擎最基本的圖片搜索也是利用圖片顏色值的對(duì)比來實(shí)現(xiàn)的,需要的朋友可以參考下
    2016-02-02
  • Python能干什么、Python主要應(yīng)用于哪些方面

    Python能干什么、Python主要應(yīng)用于哪些方面

    無(wú)論是從入門級(jí)選手到專業(yè)級(jí)選手都在做的爬蟲,還是Web程序開發(fā)、桌面程序開發(fā)還是科學(xué)計(jì)算、圖像處理, Python都可以勝任。Python為我們提供了非常完善的基礎(chǔ)代碼庫(kù),覆蓋了網(wǎng)絡(luò)、文件、GUI、 數(shù)據(jù)庫(kù)、文本等大量?jī)?nèi)容。用Python開發(fā),許多功能不必從零編寫
    2023-06-06
  • python3中獲取文件當(dāng)前絕對(duì)路徑的兩種方法

    python3中獲取文件當(dāng)前絕對(duì)路徑的兩種方法

    下面小編就為大家分享一篇python3中獲取文件當(dāng)前絕對(duì)路徑的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • pytorch 梯度NAN異常值的解決方案

    pytorch 梯度NAN異常值的解決方案

    這篇文章主要介紹了pytorch 梯度NAN異常值的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺談Python2.6和Python3.0中八進(jìn)制數(shù)字表示的區(qū)別

    淺談Python2.6和Python3.0中八進(jìn)制數(shù)字表示的區(qū)別

    下面小編就為大家?guī)硪黄獪\談Python2.6和Python3.0中八進(jìn)制數(shù)字表示的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Python類中self參數(shù)用法詳解

    Python類中self參數(shù)用法詳解

    這篇文章主要介紹了Python類中self參數(shù)用法詳解,需要的朋友可以參考下
    2020-02-02
  • 用Pycharm實(shí)現(xiàn)鼠標(biāo)滾輪控制字體大小的方法

    用Pycharm實(shí)現(xiàn)鼠標(biāo)滾輪控制字體大小的方法

    今天小編就為大家分享一篇用Pycharm實(shí)現(xiàn)鼠標(biāo)滾輪控制字體大小的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python輕松破解加密壓縮包教程詳解

    Python輕松破解加密壓縮包教程詳解

    相信大家都遇到過這種情況,下載文件的時(shí)候遇到壓縮包又沒有密碼,或者說自己設(shè)置的加密密碼,但是忘記了,就很難受。下面就將為大家介紹如何解決這一問題
    2021-12-12
  • Python數(shù)據(jù)分析Matplotlib?柱狀圖繪制

    Python數(shù)據(jù)分析Matplotlib?柱狀圖繪制

    本文主要介紹了Python數(shù)據(jù)分析Matplotlib柱狀圖繪制,Matplotlib提供了bar()方法繪制柱狀圖,下面具體繪制介紹需要的小伙伴可以參考以一下
    2022-05-05
  • Python類與實(shí)例的使用詳解

    Python類與實(shí)例的使用詳解

    面向?qū)ο笞钪匾母拍罹褪穷悾–lass)和實(shí)例(Instance),必須牢記類是抽象的模板,比如Student類,而實(shí)例是根據(jù)類創(chuàng)建出來的一個(gè)個(gè)具體的“對(duì)象”,每個(gè)對(duì)象都擁有相同的方法,但各自的數(shù)據(jù)可能不同
    2022-08-08

最新評(píng)論