2018年2月2日金曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 6.(Random Fun and Games: Go Ahead, Take a Chance!)、PROGRAMMING CHALLENGES、#2: REALISTIC MIRRORED SPIRALSを取り組んでみる。

#2: REALISTIC MIRRORED SPIRALS

コード(Emacs)

Python 3

#!/usr/bin/env python3
import random
import turtle

t = turtle.Pen()
t.speed(0)
turtle.bgcolor('black')
colors = ['red', 'yellow', 'blue', 'green',
          'orange', 'purple', 'white', 'gray']

for n in range(50):
    t.pencolor(random.choice(colors))
    size = random.randrange(10, 41)
    sides = random.randrange(3, 10)
    thick = random.randrange(1, 4)
    t.width(thick)
    # x = random.randrange(0, turtle.window_width() // 2)
    # y = random.randrange(0, turtle.window_height() // 2)
    x = random.randrange(0, 600 // 2)
    y = random.randrange(0, 600 // 2)
    angle = t.heading()
    angles = [angle, 180 - angle, angle - 180, 360 - angle]
    for i, (a, (x0, y0)) in enumerate(
            zip(angles, [(x, y), (-x, y), (-x, -y), (x, -y)])):
        t.penup()
        t.setpos(x0, y0)
        t.setheading(a)
        t.pendown()
        if i % 2 == 0:
            f = t.left
        else:
            f = t.right
        for m in range(size):
            t.forward(m * 2)
            f(360 / sides + 2)

print('done')
input()

入出力結果(Terminal, Jupyter(IPython))

$ ./sample2.py
done
q
$ ./sample2.py
done
close
$ ./sample2.py
done
q
$ ./sample2.py
done
q
$ ./sample2.py
done
q
$ 

0 コメント:

コメントを投稿

関連コンテンツ