Sunday, May 28, 2017

play with python turtle

drawing by python is fun! You can start drawing with python script by importing "turtle module".

1. sample code

# coding: utf-8

import turtle
t = turtle.Pen()
while True:
    color_list = 'pink', 'green', 'purple'
    import random
    scale = random.randint(50, 100)
    print("scale %s" % scale)
    t.left(60)
    for c in color_list:
        i = 1
        print("color %s" % c)
        while i <= 360 / 60:
            print("count %s" % i)
            t.begin_fill()
            t.forward(scale)
            t.fillcolor(c)
            t.left(30)
            t.circle(scale/4, 180)
            t.right(180)
            t.circle(scale/4, 180)
            t.left(30)
            t.forward(scale)
            t.left(180)
            i = i + 1
            t.end_fill()
        scale = scale * 0.8
    t.up()
    location = random.randint(100, 300)
    print("location %s" % location)
    t.left(scale)
    t.forward(location)
    t.left(scale)
    t.forward(location)
    t.down()
turtle.done()

2. execution

% python turtle_work.py