Python turtle graphics 作例 : 五角形を集めて花のような模様を描いてみる(関数作成)

動画




完成図(静止画)


ソースコード

import turtle, random

def penta(length):
    for i in range(0, 5):
        turtle.forward(length)
        turtle.left(180-108)


def color_red_up(num, step, ini_red, ini_green, ini_blue):
    red = ini_red + num / step
    green = ini_green
    blue = ini_blue
    if red > 1.0:
        red = 1
    if green > 1.0:
        green = 0
    if blue > 1.0:
        blue = 0
    return [red, green, blue]


def rotate_penta(length, size, step, color):
    for i in range(0, 12):
        turtle.fillcolor(color_red_up(i, step, color[0], color[1], color[2]))
        turtle.begin_fill()
        penta(length)
        turtle.end_fill()
        turtle.fd(length/5)
        turtle.right(30)


window_x = 16 * 70
window_y = 9 * 70
turtle.setup(window_x, window_y, 0, 500)

s_min = 10
s_max = 40

#loop_start = 0
#loop_end = 300
#for i in range(loop_start, loop_end):

n = 0
turtle.speed(6)
while True:
    print(n)
    if n < 2:
        turtle.tracer(1, 10)
        turtle.speed(6)
    elif n >= 2 and n < 5:
        turtle.speed(10)
    elif n >= 5 and n < 10:
        turtle.speed(0)
    elif n >= 10 and n < 20:
        turtle.tracer(1,0)
    elif n >= 20:
        turtle.tracer(10,0)

    a = random.randint(s_min, s_max)
    g_x = random.randint(-(window_x/2 - s_max*2), (window_x/2 - s_max*2))
    g_y = random.randint(-(window_y/2 - s_max*2), (window_y/2 - s_max*2))
    rotate_penta(a, a/5, 10, [0, random.random(), random.random()])

    turtle.penup()
    turtle.goto(g_x, g_y)
    turtle.pendown()

    n = n + 1
    if n % 150 == 0:
        turtle.clear()
        n = 0
       
turtle.mainloop()



まとめページ

Python turtle graphics まとめ


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

コメント

スポンサーリンク


このブログの人気の投稿

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

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

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

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

MPV(動画再生ソフト)のキーボード操作方法