開発環境
- 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 7.(Functions: There's a Name for That)、PROGRAMMING CHALLENGES、#3: A Better Drawing Programを取り組んでみる。
#3: A Better Drawing Program
コード(Emacs)
Python 3
#!/usr/bin/env python3
import turtle
t = turtle.Pen()
t.speed(0)
stretch_wid = 2
stretch_len = 2
outline = 2
t.turtlesize(stretch_wid, stretch_len, outline)
forward_len = 50
def up():
print(f'up({forward_len})')
t.forward(forward_len)
def left():
print('left')
t.left(45)
def right():
print('right')
t.right(45)
def gt():
print('gt')
global forward_len
forward_len += 1
def lt():
print('lt')
global forward_len
if forward_len > 1:
forward_len -= 1
def wider():
print('wider')
global stretch_wid
stretch_wid += 1
t.turtlesize(stretch_wid, stretch_len, outline)
def thinner():
print('thinner')
global stretch_wid
if stretch_wid > 1:
stretch_wid -= 1
t.turtlesize(stretch_wid, stretch_len, outline)
turtle.onkeypress(up, 'Up')
turtle.onkeypress(left, 'Left')
turtle.onkeypress(right, 'Right')
turtle.onkeypress(gt, '>')
turtle.onkeypress(lt, '<')
turtle.onkeypress(wider, 'W')
turtle.onkeypress(thinner, 'T')
turtle.listen()
input()
入出力結果(Terminal, Jupyter(IPython))
$ ./sample3.py up(50) up(50) up(50) up(50) up(50) left left up(50) up(50) up(50) up(50) up(50) left left up(50) up(50) up(50) up(50) up(50) left left up(50) up(50) up(50) up(50) up(50) right right up(50) up(50) up(50) up(50) up(50) right right up(50) up(50) up(50) up(50) up(50) right right up(50) up(50) up(50) up(50) up(50) left left lt up(49) lt lt lt lt lt lt lt lt lt up(40) gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt gt right right up(60) up(60) up(60) up(60) up(60) right up(60) up(60) right up(60) up(60) wider wider wider wider wider thinner thinner thinner thinner thinner q $
0 コメント:
コメントを投稿