Python turtle graphics 作例 : たくさんの木を描いてみる

動画



完成図(静止画)


ソースコード

import turtle, random

def tree(length, angle):
    if length < 2:
        return
    turtle.pencolor("brown")
    turtle.pensize(5)
    turtle.setheading(90)
    turtle.forward(length)
    turtle.pencolor("green")
    turtle.setheading(angle)
    turtle.forward(length)
    turtle.penup()
    turtle.forward(-length)
    turtle.pendown()
    turtle.pencolor("brown")
    turtle.setheading(90)
    turtle.forward(length * 0.5)
    turtle.pencolor("green")
    turtle.setheading(180-angle)
    turtle.forward(length)
    turtle.penup()
    turtle.forward(-length)
    turtle.pendown()
    tree(length / 2, angle)


window_x = 960
window_y = 720
turtle.setup(window_x, window_y,None,None)
turtle.speed(0)
delay = 10
steps = 1
k = 0
for i in range(0, 1000):
    turtle.penup()
    turtle.goto(0, 0)
    turtle.goto(random.randint(-window_x/2, window_x/2), random.randint(-window_y/2, window_y/2))
    turtle.pendown()
    tree(random.randint(10, 30), random.randint(-60, 60))
    turtle.tracer(steps, delay)
    k = k + 1
    if k == 10:
        delay = delay - 1
        if delay < 0:
            delay = 0
        steps = steps + 1
        k = 0

turtle.mainloop()



まとめページ

Python turtle graphics まとめ


スポンサーリンク
Amazon商品リンク : Pythonプログラミング関連


コメント

スポンサーリンク


このブログの人気の投稿

Ubuntu Softwareが起動しないのでいろいろと調べてみる(Ubuntu 20.04.1 LTS)

gnuplotでプロットなどの色をcolornameの指定で変更する

gnuplot : グラフにグリッド線を描く方法(set grid)

gnuplot : プロット画像のサイズ指定について(set sizeとの違い)

Pythonのformat()を使って1桁の16進数でも2桁で出力する方法