2018年2月12日月曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 9.(User Interaction: Get into the Game)、PROGRAMMING CHALLENGES、#2: PAINTING IN COLORSを取り組んでみる。

#2: PAINTING IN COLORS

コード(Emacs)

Python 3

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

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
colors = [red, green, blue]
width = 800
height = 600
radius = 15

pygame.init()
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('Click and drag to draw')

keep_going = True
mousedown = False

while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mousedown = True
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
    if mousedown:
        spot = pygame.mouse.get_pos()
        if pygame.mouse.get_pressed()[0]:
            color = red
        elif pygame.mouse.get_pressed()[1]:
            color = green
            print(color)
        elif pygame.mouse.get_pressed()[2]:
            color = blue
            print(color)
        print(color)
        pygame.draw.circle(screen, color, spot, radius)
    pygame.display.update()

pygame.quit()

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

$ ./sample2.py
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 0)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
(0, 0, 255)
$

0 コメント:

コメントを投稿