## 2018年2月4日日曜日

### Python - Functions: There's a Name for That(Mirrored Smileys, turtle module, methods(speed, hideturtle, bgcolor, penup, pendown, setpos, pencolor, fillcolor, begin_fill, end_fill, circle), flip)

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

#1: Mirrored Smileys

コード(Emacs)

Python 3

```#!/usr/bin/env python3
import random
import turtle

t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor('black')

def draw_smiley(x, y, dir):
t.penup()
t.setpos(x, y)
t.pendown()

t.pencolor('yellow')
t.fillcolor('yellow')
t.begin_fill()
t.circle(50)
t.end_fill()

def op(y):
if dir:
return y
return -y

t.setpos(x - 15, y + op(60))
t.fillcolor('blue')
t.begin_fill()
t.circle(10)
t.end_fill()

t.setpos(x + 15, y + op(60))
t.begin_fill()
t.circle(10)
t.end_fill()

t.setpos(x - 25, y + op(40))
t.pencolor('black')
t.width(10)
t.goto(x - 10, y + op(20))
t.goto(x + 10, y + op(20))
t.goto(x + 25, y + op(40))
t.width(1)

def draw_kaleido(x, y):
print(f'{x} x {y}')
for x0, y0 in [(x, y), (-x, y)]:
draw_smiley(x0, y0, True)
t.penup()
t.left(180)
for x0, y0 in [(-x, -y), (x, -y)]:
draw_smiley(x0, y0, False)
t.left(180)

turtle.onscreenclick(draw_kaleido)

input()
```

```\$ ./sample1.py
-274.0 x 246.0
274.0 x 97.0
-21.0 x -153.0
-204.0 x -8.0
-398.0 x -80.0
q
\$
```