【python】待君有餘暇,看春賞櫻花,這不得來一場浪漫的櫻花旅~

專注的阿熊發表於2022-10-28

import turtle

from random import random

from random import randint

def draw_petal(turtle_obj, flower):

     # 繪製掉落的花瓣

     for i in range(int(flower)):

         # 有正有負就可以讓畫筆往二個方向走

         x = flower - 4 * flower * random()

         # 花瓣整體寬度 (-10, 10)

         y = 10 - 20 * random()

         # 提筆,向前 y ,左轉 90 ,走 x ,落筆

         turtle_obj.penup()

         turtle_obj.forward(y)

         turtle_obj.left(90)

         turtle_obj.forward(x)

         turtle_obj.pendown()

         # 珊瑚色

         turtle_obj.pencolor("lightcoral")

         # 畫圓

         turtle_obj.circle(1)

         # 回到起點

         # 提筆,後退 x ,右轉 90, 後退 y ,落筆

         turtle_obj.penup()

         turtle_obj.backward(x)

         turtle_obj.right(90)

         turtle_obj.backward(y)

         turtle_obj.pendown()

# 畫樹枝部分

def draw_tree(turtle_obj, branch, tree_color):

     # 設定一個最小分支長度

     min_branch = 4

     if branch > min_branch:

         if branch < 8:

             # 0.5 的機率,向左、右分支

             if randint(0, 1) == 0:

                 # 左為白色

                 turtle_obj.pencolor("snow")

             else:

                 # 右為珊瑚色

                 turtle_obj.pencolor("lightcoral")

             # 枝幹

             turtle_obj.pensize(branch / 2)

         elif 8 <= branch <= 16:

             # 0.33 的機率,分為左、中、右分支

             if randint(0, 2) == 0:

                 # 左為白色

                 turtle_obj.pencolor("snow")

             else:

                 # 中、右為珊瑚色

                 turtle_obj.pencolor("lightcoral")

             # 樹枝

             turtle_obj.pensize(branch / 4)

         else:

             # 褐色

             turtle_obj.pencolor(tree_color)

             # 細枝

             turtle_obj.pensize(branch / 10)

         # 最開始的樹幹長度

         turtle_obj.forward(branch)

         # 隨機度數因子

         a = 1.5 * random()

         # 順時針旋轉隨機角度( 0 30 度)

         turtle_obj.right(20 * a)

         # 隨機長度因子

         b = 1.5 * random()

         # 往右畫,直到畫不動為止

         draw_tree(turtle_obj, branch - 10 * b, tree_color)

         # 左轉隨機角度

         turtle_obj.left(40 * a)

         # 往左畫,跟單網gendan5.com直到畫不動位置

         draw_tree(turtle_obj, branch - 10 * b, tree_color)

         # 右轉一定角度

         turtle_obj.right(20 * a)

         # 提筆

         turtle_obj.penup()

         # 遞迴結束回到起點

         turtle_obj.backward(branch)

         turtle_obj.pendown()

def get_screen(width, height, color, speed):

     # 建立畫幕

     screen_obj = turtle.Screen()

     # 畫布大小: (width, height) ,顏色: color

     screen_obj.screensize(width, height, bg=color)

     screen_obj.setup(1.0, 1.0)

     # speed 倍加速

     screen_obj.tracer(speed)

     return screen_obj

def trees(tree_num):

     # 顏色

     color = ['brown', 'tan', 'black']

     for j in range(tree_num):

         # 樹幹顏色

         tree_color = color[randint(0, len(color) - 1)]

         # 畫筆大小

         pensize = randint(2, 5)

         # 前進畫素

         forward = ((-1) ** pensize) * pensize * randint(20, 50)

         # 後退畫素

         if pensize <= 3:

             backward = ((-1) ** pensize) * (5 - pensize) * randint(10, 15)

         else:

             backward = pensize * randint(45, 50)

         # 建立畫筆

         turtle_obj = turtle.Turtle()

         # 畫筆粗細

         turtle_obj.pensize(pensize)

         # 提筆,向前 forward ,左轉 90 backward ,落筆

         turtle_obj.penup()

         turtle_obj.forward(forward)

         turtle_obj.left(90)

         turtle_obj.backward(backward)

         turtle_obj.pendown()

         # 畫筆顏色:褐色

         turtle_obj.pencolor(tree_color)

         # 枝幹粗細

         branch = pensize * 15

         # 落花數

         flowers = branch

         # j 棵樹

         draw_tree(turtle_obj, branch, tree_color)

         # 花瓣

         draw_petal(turtle_obj, flowers)

if __name__ == '__main__':

     # 建立畫幕

     my_screen_width = 800

     my_screen_height = 600

     my_screen_color = 'wheat'

     my_screen_speed = 5

     my_screen_obj = get_screen(my_screen_width, my_screen_height,

                                my_screen_color, my_screen_speed,)

     # 櫻花樹

     # 棵數

     my_tree_num = 10

     trees(my_tree_num)

     # 點選關閉畫布

     my_screen_obj.exitonclick()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2920917/,如需轉載,請註明出處,否則將追究法律責任。

相關文章