2018年2月11日日曜日

開発環境

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、#1: RANDOMLY COLORED DOTSを取り組んでみる。

#1: RANDOMLY COLORED DOTS

コード(Emacs)

Python 3

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

pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Click and drag to draw')
keep_going = True
radius = 15
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()
        color = [random.randrange(256) for _ in range(3)]
        pygame.draw.circle(screen, color, spot, radius)
        print(color)
    pygame.display.update()

pygame.quit()

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

$ ./sample1.py
[233, 194, 135]
[214, 233, 106]
[177, 237, 17]
[6, 138, 203]
[45, 18, 30]
[217, 232, 97]
[188, 166, 229]
[237, 20, 99]
[84, 15, 98]
[37, 157, 131]
[216, 2, 23]
[196, 126, 93]
[197, 48, 155]
[224, 245, 98]
[129, 197, 42]
[97, 173, 230]
[232, 98, 128]
[51, 110, 89]
[144, 12, 71]
[153, 171, 43]
[139, 48, 134]
[134, 191, 68]
[117, 158, 50]
[37, 164, 69]
[209, 217, 2]
[185, 6, 22]
[141, 148, 109]
[25, 235, 138]
[110, 201, 214]
[52, 2, 139]
[34, 13, 105]
[14, 54, 186]
[2, 49, 85]
[197, 129, 240]
[167, 37, 66]
[64, 143, 248]
[241, 134, 225]
[78, 207, 46]
[52, 152, 249]
[66, 182, 176]
[49, 245, 33]
[53, 114, 242]
[20, 13, 229]
[130, 2, 195]
[54, 44, 158]
[215, 38, 252]
[196, 202, 52]
[5, 233, 129]
[159, 70, 61]
[86, 214, 110]
[110, 216, 4]
[210, 11, 146]
[136, 132, 236]
[75, 206, 48]
[249, 131, 83]
[166, 190, 185]
[150, 133, 163]
[223, 191, 109]
[202, 51, 186]
[255, 133, 120]
[162, 239, 46]
[200, 69, 18]
[183, 255, 231]
[3, 163, 75]
[139, 173, 186]
[122, 230, 103]
[133, 224, 245]
[137, 115, 198]
[203, 144, 228]
[178, 168, 183]
[165, 31, 225]
[46, 142, 254]
[18, 80, 178]
[217, 170, 64]
[173, 237, 43]
[252, 5, 167]
[183, 28, 19]
[37, 77, 193]
[24, 36, 205]
[215, 121, 133]
[38, 128, 76]
[159, 250, 66]
[81, 89, 2]
[223, 154, 84]
[59, 155, 87]
[64, 107, 135]
[191, 54, 175]
[10, 81, 8]
[163, 61, 113]
[28, 121, 155]
[110, 19, 9]
[82, 179, 218]
[89, 187, 103]
[115, 160, 198]
[57, 101, 114]
[148, 102, 251]
[144, 149, 205]
[18, 175, 234]
[44, 31, 91]
[55, 162, 34]
[197, 252, 146]
[80, 122, 28]
[158, 7, 138]
[119, 70, 17]
[32, 72, 38]
[151, 16, 187]
[211, 143, 145]
[91, 158, 185]
[74, 133, 205]
[109, 250, 193]
[75, 179, 191]
[228, 197, 180]
[34, 171, 9]
[72, 26, 166]
[106, 119, 226]
[188, 69, 130]
[122, 98, 129]
[89, 55, 149]
[219, 23, 117]
[15, 75, 30]
[181, 17, 115]
[255, 203, 141]
[250, 214, 185]
[250, 101, 173]
[52, 216, 71]
[161, 227, 250]
[142, 216, 227]
[41, 31, 220]
[207, 11, 100]
[214, 110, 217]
[199, 228, 66]
[90, 219, 155]
[182, 16, 163]
[73, 191, 84]
[111, 200, 98]
[79, 18, 199]
[222, 3, 228]
[234, 48, 82]
[174, 129, 52]
[23, 155, 12]
[134, 67, 218]
[239, 96, 72]
[166, 175, 199]
[192, 119, 242]
[150, 229, 132]
[214, 222, 16]
[68, 109, 245]
[90, 137, 130]
[179, 66, 177]
[208, 42, 5]
[26, 82, 254]
[200, 204, 186]
[7, 153, 111]
[19, 138, 126]
[18, 170, 116]
[121, 81, 177]
[44, 202, 230]
[198, 26, 95]
[182, 6, 12]
[175, 204, 242]
[9, 124, 242]
[119, 225, 240]
[136, 255, 215]
[125, 56, 193]
[79, 125, 108]
[148, 41, 159]
[133, 101, 167]
[240, 225, 79]
[42, 96, 118]
[172, 94, 47]
[238, 125, 55]
[30, 239, 246]
[95, 55, 52]
[22, 172, 84]
[120, 85, 215]
[242, 240, 7]
[49, 113, 114]
[86, 191, 72]
[228, 146, 115]
[118, 69, 68]
[177, 242, 207]
[193, 123, 116]
[199, 9, 251]
[52, 246, 186]
[8, 217, 177]
[201, 151, 81]
[242, 96, 42]
[56, 171, 63]
[204, 130, 14]
[140, 234, 33]
[146, 144, 36]
[198, 187, 17]
[200, 44, 222]
[163, 127, 49]
[103, 18, 76]
[229, 186, 86]
[118, 175, 48]
[69, 22, 58]
[220, 253, 61]
[158, 75, 115]
[128, 161, 102]
[40, 57, 0]
[100, 57, 128]
[138, 240, 189]
[146, 152, 199]
[25, 178, 196]
[124, 35, 120]
[136, 224, 200]
[108, 231, 118]
[161, 150, 190]
[121, 114, 41]
[116, 197, 247]
[246, 208, 148]
[209, 126, 21]
[223, 18, 195]
[246, 228, 3]
[155, 107, 108]
[130, 0, 252]
[178, 197, 22]
[220, 128, 250]
[249, 164, 183]
[123, 19, 235]
[52, 123, 0]
$

0 コメント:

コメントを投稿