2015年10月17日土曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs (Text Editor)
  • Python 3.5 (プログラミング言語)

Python for Kids: A Playful Introduction to Programming (Jason R. Briggs (著) 、No Starch Press)のPart Ⅰ.(Learning to Program)、Chapter 12.(Using tkinter for Better Graphics)、Programming Puzzles #1: Fill the Screen with Triangles(No. 3603)を解いてみる。

Programming Puzzles #1: Fill the Screen with Triangles(No. 3603)

コード(Emacs)

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import tkinter
import random
import time
import sys

app = tkinter.Tk()
canvas = tkinter.Canvas(app, width=400, height=400)
canvas.pack()

def random_triangle(width, height, outline='black', fill=''):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = random.randrange(width)
    y2 = random.randrange(height)
    x3 = random.randrange(width)
    y3 = random.randrange(height)
    canvas.create_polygon(x1, y1, x2, y2, x3, y3,
                          outline=outline, fill=fill)

if sys.argv[1] == '1':
    for x in range(100):
        random_triangle(400, 400)
else:
    for x in range(100):
        color = '#'
        for y in range(6):
            color += hex(random.randrange(16))[2]
        random_triangle(400, 400, outline='', fill=color)
        
app.mainloop()

入出力結果(Terminal, IPython)

$ ./sample1.py 1
$ ./sample1.py 2
$

0 コメント:

コメントを投稿