開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
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、#3: RAINING DOTSを取り組んでみる。
#3: RAINING DOTS
コード(Emacs)
Python 3
#!/usr/bin/env python3
import pygame
import random
BLACK = (0, 0, 0)
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])
new_x = locations[i][0] + 1
new_y = locations[i][1] + 1
if new_x > width:
new_x -= width
if new_y > height:
new_y -= height
locations[i] = (new_x, new_y)
pygame.display.update()
screen.fill(BLACK)
pygame.quit()
入出力結果(Terminal, Jupyter(IPython))
$ ./sample3.py $
0 コメント:
コメントを投稿