2018年2月9日金曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 8.(Timers and Animation: What Would Disney Do?)、PROGRAMMING CHALLENGES、#2: 100 RANDOM DOTSを取り組んでみる。

#2: 100 RANDOM DOTS

コード(Emacs)

Python 3

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

width = 800
height = 600

n = 100
colors = [[random.randrange(0, 256) for _ in range(3)]
          for _ in range(n)]
locations = [(random.randrange(0, width + 1), random.randrange(0, height + 1))
             for _ in range(n)]
size = [random.randrange(10, 101) for _ in range(n)]

pygame.init()
screen = pygame.display.set_mode([width, height])

keep_going = True
radius = 50
while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
    for i in range(n):
        pygame.draw.circle(screen, colors[i], locations[i], size[i])
    pygame.display.update()

pygame.quit()

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

$ ./sample2.py
$ ./sample2.py
$ 

0 コメント:

コメントを投稿