0. はじめに
- MYJLab Advent Calendar 2021 1日目の記事です.
- Google Colaboratory と Turtle(タートルグラフィックス)でのアルファベットの描き方をまとめました.
- Turtleに命令を出して動かすことで簡単に文字を描くことができます.
1. Turtle(タートルグラフィックス)の基本操作
- 本家サイト:turtle --- タートルグラフィックスを参考にしています.
Turtleの動き
前進
turtle.forward(distance)
turtle.fd(distance)
後退
turtle.back(distance)
turtle.bk(distance)
turtle.backward(distance)
右回転
turtle.right(angle)
turtle.rt(angle)
左回転
turtle.left(angle)
turtle.lt(angle)
指定した位置に移動
turtle.goto(x, y)
turtle.setpos(x, y)
turtle.setposition(x, y)
指定した位置(x座標のみ)に移動
turtle.setx(x)
指定した位置(y座標のみ)に移動
turtle.sety(y)
指定した向きに変更
turtle.setheading(to_angle)
turtle.seth(to_angle)
原点に移動
turtle.home()
Turtleの状態
タートルを見えないようにする
turtle.hideturtle()
turtle.ht()
タートルを見えるようにする
turtle.showturtle()
turtle.st()
Penの制御
ペンを下ろす
turtle.pendown()
turtle.pd()
turtle.down()
ペンを上げる
turtle.penup()
turtle.pu()
turtle.up()
ペンの線の太さの変更
turtle.pensize(width)
turtle.width(width)
ペンの色の変更
turtle.pencolor(colorstring)
turtle.pencolor(r,g,b)
2. Google Colaboratory で Turtle を使う準備
- turtleライブラリの読み込みをおこなう
preparation.ipynb
!pip3 install ColabTurtle
import ColabTurtle.Turtle as turtle
from ColabTurtle.Turtle import *
turtle.initializeTurtle()
- 描画エリアのサイズとペンの動く速さを指定するとき
initial.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 描画エリアのサイズの初期値:800 x 500,ペンの動く速さの初期値:4
# ペンの動く速さ:1から10までの整数,値が大きいほど速い
3. Turtle で描くアルファベット大文字一覧
- 一画目:赤色
- 二画目:青色
- 三画目:黄色
- 四画目:緑色
A
A.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(160)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(100)
turtle.penup()
# 一画目→二画目準備
turtle.backward(100)
turtle.left(40)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(100)
turtle.penup()
# 二画目→三画目準備
turtle.backward(100)
turtle.right(40)
turtle.forward(70)
turtle.left(110)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(48)
turtle.penup()
turtle.hideturtle()
B
B.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(25)
for i in range(18):
turtle.right(10)
turtle.forward(4)
turtle.forward(20)
turtle.penup()
# 二画目→三画目準備
turtle.left(180)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(30)
for i in range(18):
turtle.right(10)
turtle.forward(4)
turtle.forward(27)
turtle.penup()
turtle.hideturtle()
C
C.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(40)
# 一画目
turtle.pendown()
turtle.pencolor("red")
for i in range(28):
turtle.left(10)
turtle.forward(8)
turtle.penup()
turtle.hideturtle()
D
D.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(25)
for i in range(18):
turtle.right(10)
turtle.forward(8)
turtle.forward(18)
turtle.penup()
turtle.hideturtle()
E
E.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(60)
turtle.penup()
# 二画目→三画目準備
turtle.backward(60)
turtle.right(90)
turtle.forward(45)
turtle.left(90)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(50)
turtle.penup()
# 三画目→四画目準備
turtle.backward(50)
turtle.right(90)
turtle.forward(45)
turtle.left(90)
# 四画目
turtle.pendown()
turtle.pencolor("green")
turtle.forward(60)
turtle.penup()
turtle.hideturtle()
F
F.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(60)
turtle.penup()
# 二画目→三画目準備
turtle.backward(60)
turtle.right(90)
turtle.forward(45)
turtle.left(90)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(50)
turtle.penup()
turtle.hideturtle()
G
G.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(40)
# 一画目
turtle.pendown()
turtle.pencolor("red")
for i in range(28):
turtle.left(10)
turtle.forward(8)
turtle.penup()
# 一画目→二画目準備
turtle.left(40)
turtle.forward(20)
turtle.right(90)
turtle.backward(30)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(30)
turtle.right(90)
turtle.forward(40)
turtle.penup()
turtle.hideturtle()
H
H.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
turtle.forward(75)
turtle.right(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(90)
turtle.penup()
# 二画目→三画目準備
turtle.backward(45)
turtle.left(90)
turtle.backward(75)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(75)
turtle.penup()
turtle.hideturtle()
I
I.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
turtle.backward(15)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(30)
turtle.penup()
# 二画目→三画目準備
turtle.backward(30)
turtle.right(90)
turtle.forward(90)
turtle.left(90)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(30)
turtle.penup()
turtle.hideturtle()
J
J.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(70)
for i in range(18):
turtle.right(10)
turtle.forward(5)
turtle.penup()
turtle.hideturtle()
K
K.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(45)
turtle.right(45)
turtle.backward(65)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(65)
turtle.penup()
# 二画目→三画目準備
turtle.left(90)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(65)
turtle.penup()
turtle.hideturtle()
L
L.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(60)
turtle.penup()
turtle.hideturtle()
M
M.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
turtle.forward(80)
turtle.right(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(90)
turtle.penup()
# 二画目→三画目準備
turtle.backward(90)
turtle.left(90)
turtle.backward(80)
turtle.right(60)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(80)
turtle.penup()
# 三画目→四画目準備
turtle.right(60)
turtle.backward(80)
# 三画目
turtle.pendown()
turtle.pencolor("green")
turtle.forward(80)
turtle.penup()
turtle.hideturtle()
N
N.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.left(90)
turtle.forward(60)
turtle.right(90)
turtle.backward(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(90)
turtle.penup()
# 二画目→三画目準備
turtle.left(33)
turtle.backward(108)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(108)
turtle.penup()
turtle.hideturtle()
O
O.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(100)
# 一画目
turtle.pendown()
turtle.pencolor("red")
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(90)
turtle.forward(30)
turtle.left(10)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(350)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(270)
turtle.forward(30)
turtle.left(10)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.penup()
turtle.hideturtle()
P
P.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(40)
turtle.right(20)
for i in range(13):
turtle.right(10)
turtle.forward(5)
turtle.right(30)
turtle.forward(40)
turtle.penup()
turtle.hideturtle()
Q
Q.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(100)
# 一画目
turtle.pendown()
turtle.pencolor("red")
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(90)
turtle.forward(30)
turtle.left(10)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(350)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.setheading(270)
turtle.forward(30)
turtle.left(10)
for i in range(7):
turtle.left(10)
turtle.forward(8)
turtle.up()
# 一画目→二画目準備
turtle.left(100)
turtle.forward(70)
turtle.left(60)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(60)
turtle.penup()
turtle.hideturtle()
R
R.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(40)
turtle.right(20)
for i in range(13):
turtle.right(10)
turtle.forward(5)
turtle.right(30)
turtle.forward(40)
turtle.penup()
# 二画目→三画目準備
turtle.left(180)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(40)
turtle.right(60)
turtle.forward(40)
turtle.penup()
turtle.hideturtle()
S
S.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(20)
# 一画目
turtle.pendown()
turtle.pencolor("red")
for i in range(14):
turtle.left(10)
turtle.forward(7)
turtle.left(20)
turtle.forward(15)
turtle.left(70)
turtle.forward(80)
turtle.right(70)
turtle.forward(15)
turtle.right(20)
for i in range(14):
turtle.right(10)
turtle.forward(7)
turtle.penup()
turtle.hideturtle()
T
T.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(90)
turtle.penup()
# 一画目→二画目準備
turtle.backward(90)
turtle.left(90)
turtle.backward(40)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(80)
turtle.penup()
turtle.hideturtle()
U
U.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=8)
# 一画目準備
turtle.left(180)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(60)
for i in range(18):
turtle.left(10)
turtle.forward(7)
turtle.forward(54)
turtle.penup()
turtle.hideturtle()
V
V.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.right(155)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(100)
turtle.penup()
# 一画目→二画目準備
turtle.right(50)
turtle.backward(100)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(100)
turtle.penup()
turtle.hideturtle()
W
W.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.right(160)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(100)
turtle.penup()
# 一画目→二画目準備
turtle.right(40)
turtle.backward(90)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(90)
turtle.penup()
# 二画目→三画目準備
turtle.backward(90)
turtle.left(40)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(90)
turtle.penup()
# 三画目→四画目準備
turtle.right(40)
turtle.backward(100)
# 四画目
turtle.pendown()
turtle.pencolor("green")
turtle.forward(100)
turtle.penup()
turtle.hideturtle()
X
X.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.right(140)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(100)
turtle.penup()
# 一画目→二画目準備
turtle.right(40)
turtle.backward(75)
turtle.right(40)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(100)
turtle.penup()
turtle.hideturtle()
Y
Y.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.right(140)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(60)
turtle.penup()
# 一画目→二画目準備
turtle.right(78)
turtle.backward(60)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(60)
turtle.penup()
# 二画目→三画目準備
turtle.left(38)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(50)
turtle.penup()
turtle.hideturtle()
Z
Z.ipynb
initializeTurtle(initial_window_size=(300, 300), initial_speed=5)
# 一画目準備
turtle.right(90)
# 一画目
turtle.pendown()
turtle.pencolor("red")
turtle.forward(60)
turtle.penup()
# 一画目→二画目準備
turtle.right(125)
# 二画目
turtle.pendown()
turtle.pencolor("blue")
turtle.forward(100)
turtle.penup()
# 二画目→三画目準備
turtle.left(125)
# 三画目
turtle.pendown()
turtle.pencolor("yellow")
turtle.forward(60)
turtle.penup()
turtle.hideturtle()
4. おわりに
- 過去のアドベントカレンダー