適当のごった煮

Pythonと境界標とQGISを中心にいろいろと

turtleで視力回復トレーニングを作る その2

スポンサードリンク

雑誌『一個人』2016年9月号の『「目の老化」を防ぐ生活術』の付録を参考に、turtleの練習として視力回復トレーニングの作図をしています。turtleで視力回復トレーニングを作る その1の続きです。

繰り返しになりますが、turtleモジュールの練習用の課題として作成しているので、視力回復等の効果を保証するものではないことをご了解ください。

準備

まずはimportしてオブジェクトを作成し、このオブジェクトに対して操作を行っていきます。ペンの形は亀にします。画面サイズは幅960、高さ810を想定しています。また、sleepを使うために「import time」しています。

import turtle
import time

t = turtle.Turtle()
t.shape('turtle')

# 表示画面の幅と高さ t.screen.setup(width, height)で変更可能
print(t.screen.window_width()) # 960を想定
print(t.screen.window_height()) # 810を想定

目次

らせんトレース

circleは、亀の現在地から左に半径に指定した長さの場所が円の中心になるため、10度円を描くごとに半径を長くしたりいろいろ試してみた結果、180度ごとに半径を長くするやり方に落ち着きました。

radius_1 = 50
plus = 25

for i in range(12):
    t.circle(radius_1+i*plus, 180)

t.dot(10)
t.ht()
time.sleep(1.5)
t.st()

for i in range(12, 0, -1):
    t.circle(radius_1+(i-1)*plus, -180)

実行すると以下のような図になります。

f:id:tekito-gottani:20160929200858j:plain

四角らせんトレース

らせんの四角形版です。

repeat_num = 36
nagasa = 2
nobi = 20
back_list = []
for i in range(repeat_num):
    back_list.append(t.pos())
    t.fd(nagasa)
    t.lt(90)
    nagasa += nobi

t.dot(10)
t.ht()
time.sleep(1.5)
t.st()

while back_list:
    t.goto(back_list.pop())

実行すると以下のような図になります。

f:id:tekito-gottani:20160929200928j:plain

読み取りトレーニング

顔を動かさず、目線だけで「あいうえお」順にひらがなを追うトレーニングです。

gojuon = ['あ','い','う','え','お','か','き','く','け','こ',' ',\
          'さ','し','す','せ','そ','た','ち','つ','て','と',' ',\
          'な','に','ぬ','ね','の','は','ひ','ふ','へ','ほ',' ',\
          'ま','み','む','め','も','や','ゆ','よ', ' ',' ',' ',\
          'ら','り','る','れ','ろ','わ','を','ん', ' ',' ',]

color = ['orangered', 'green', 'chartreuse', 'blue', 'aqua',\
         'hotpink', 'black', 'gold', 'orange', 'purple']

font_size = [25, 30, 40, 45]

t.pu()
y = 170
for i in range(6):
    if i % 2 == 0: # 横位置をずらす
        x = -245
    else:
        x = -275
    for j in range(9):
        color_num = random.randint(0, len(color) - 1)
        font_num = random.randint(0, len(font_size) - 1)
        num = random.randint(0, len(gojuon) - 1)
        t.goto(x, y)
        t.pd()
        t.pencolor(color[color_num])
        t.write(gojuon[num], align="center", font=("Arial", font_size[font_num], "normal"))
        t.pu()
        gojuon.pop(num)
        x += 70
    y -= 70
t.ht()

実行すると以下のような図になります。

f:id:tekito-gottani:20160929201039j:plain

参考