2018年1月26日金曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 4.(Loops Are Fun (You Can Say That Again))、PROGRAMMING CHALLENGES、#1: SPIRAL ROSETTESを取り組んでみる。

#1: SPIRAL ROSETTES

コード(Emacs)

Python 3

#!/usr/bin/env python3
import turtle

t = turtle.Pen()
t.speed(0)

t.penup()
turtle.bgcolor('black')
sides = int(turtle.numinput(
    'Number of sides',
    'How many sides in your spiral of spirals (2-6)?', 6, 2, 6))
number_of_circles = int(turtle.numinput(
    'Number of circles', 'How many circles in your rosette?', 5))
colors = ['red', 'yellow', 'blue', 'green', 'purple', 'orange']

for m in range(100):
    t.forward(m * 4)
    position = t.position()
    heading = t.heading()
    t.pendown()
    t.width(m / 10)
    for n in range(number_of_circles):
        t.pencolor(colors[n % number_of_circles])
        t.circle(int(m / number_of_circles))
        t.right(360 / number_of_circles)
    t.penup()
    t.setx(position[0])
    t.sety(position[1])
    t.setheading(heading)
    t.left(360 / sides + 2)

print('done')
input()

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

$ ./sample1.py
done
q
$

0 コメント:

コメントを投稿