この記事では、pythonのturtleライブラリを使って、自分の車を描く方法を解説します。
もしあなたが turtle モジュールを知らないなら、ここのチュートリアルを見てください。
この記事もチェック:Pythonのturtleモジュールの使い方|座標とか色の指定のやり方を解説する
Python Turtle を使って車を描くためのステップ
Pythonに付属しているturtleライブラリをインポートする必要があり、追加でインストールする必要はありません。
| 
 1 
 | 
import turtle
 | 
次に、車を描くためのアプリケーション画面を作成します。
ウィンドウの名前は好きなようにつけることができます。
この記事では、画面の名前を car_scr にしています。
以下のコードは画面の作成とカスタマイズを行うもので、画面の色やペンの色も指定できます。
| 
 1 
2 
3 
4 
 | 
import turtle
car_scr = turtle
car_scr.color('black')
car_scr.Screen().bgcolor("#ABEBC6")
 | 
それでは、車を描いてくれる関数を作ってみましょう。
車には上半身、車輪、窓など様々なパーツがあります。
それぞれを別々に順次描いていくことにします。
関数 Draw_Car の内部では、開始座標をパラメータとして与える必要があります。
そして、goto関数は関数に渡した位置へのポインタを取得します。
Draw_Car 関数で使用されている関数については、皆さんもよくご存じかと思います。
以下、それぞれの動作について触れていきます。
- 
- 
penup&pendown– 描画するときとしないときを制御します。 
 - 
 - 
- 
fillcolor、begin_fill、end_fill– 特定の図形の色を制御します。 
 - 
 - 
- 
forward&backwardとleft&right– 画面上に特定の方向や角度で描画するのに役立ちます。 
 - 
 
Python Turtleを使った車のベース作成について
まず、以下のコードで車の上半身を描いてみましょう。
見てください、このように素晴らしい出力になりました。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
 | 
def Draw_Car(i,j):
    # Drawing the Upper Body
    car_scr.fillcolor("#2980B9")
    car_scr.penup()
    car_scr.goto(i,j)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.left(90)
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.end_fill()
 | 
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
 | 
def Draw_Car(i,j):
    # Drawing the Upper Body
    car_scr.fillcolor("#2980B9")
    car_scr.penup()
    car_scr.goto(i,j)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.left(90)
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.end_fill()
    #Draw the Windows
    car_scr.fillcolor("#D5DBDB")
    car_scr.penup()
    car_scr.goto(i+100, 50)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.setheading(45)
    car_scr.forward(70)
    car_scr.setheading(0)
    car_scr.forward(100)
    car_scr.setheading(-45)
    car_scr.forward(70)
    car_scr.setheading(90)
    car_scr.end_fill()
    car_scr.penup()
    car_scr.goto(i+200, 50)
    car_scr.pendown()
    car_scr.forward(49.50)
 | 
車の窓を描く
関数を呼び出して、開始点 x と y の値を渡します。
ここでは Draw_Car(-200,0) の関数を呼び出すことにします。
さて、これから窓を描きますが、上のコードとほとんど同じようなコードです。
以下のコードを見てください。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
 | 
def Draw_Car(i,j):
    # Drawing the Upper Body
    car_scr.fillcolor("#2980B9")
    car_scr.penup()
    car_scr.goto(i,j)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.left(90)
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.end_fill()
    #Draw the Windows
    car_scr.fillcolor("#D5DBDB")
    car_scr.penup()
    car_scr.goto(i+100, 50)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.setheading(45)
    car_scr.forward(70)
    car_scr.setheading(0)
    car_scr.forward(100)
    car_scr.setheading(-45)
    car_scr.forward(70)
    car_scr.setheading(90)
    car_scr.end_fill()
    car_scr.penup()
    car_scr.goto(i+200, 50)
    car_scr.pendown()
    car_scr.forward(49.50)
    # Draw the two wheels
    car_scr.penup()
    car_scr.goto(i+100, -10-j)
    car_scr.pendown()
    car_scr.color('black')
    car_scr.fillcolor('black')
    car_scr.begin_fill()
    car_scr.circle(20)
    car_scr.end_fill()
    car_scr.penup()
    car_scr.goto(i+300, -10-j)
    car_scr.pendown()
    car_scr.color('black')
    car_scr.fillcolor('black')
    car_scr.begin_fill()
    car_scr.circle(20)
    car_scr.end_fill()
    car_scr.hideturtle()
 | 
このコードを実行すると、次のような画面が出力されます。
Draw_Car(-200,0)car_scr.done() | 
タートルを使って車の車輪を追加する
最後に、同じような方法で車に車輪を追加する必要があります。
以下の関数の完成形を見てください。
最後に、Turtleポインタを隠すと、きれいな車の画像が表示されます。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
 | 
import turtle
car_scr = turtle
car_scr.color('black')
car_scr.Screen().bgcolor("#ABEBC6")
def Draw_Car(i,j):
    # Drawing the Upper Body
    car_scr.fillcolor("#2980B9")
    car_scr.penup()
    car_scr.goto(i,j)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.left(90)
    car_scr.forward(370)
    car_scr.left(90)
    car_scr.forward(50)
    car_scr.end_fill()
    #Draw the Windows
    car_scr.fillcolor("#D5DBDB")
    car_scr.penup()
    car_scr.goto(i+100, 50)
    car_scr.pendown()
    car_scr.begin_fill()
    car_scr.setheading(45)
    car_scr.forward(70)
    car_scr.setheading(0)
    car_scr.forward(100)
    car_scr.setheading(-45)
    car_scr.forward(70)
    car_scr.setheading(90)
    car_scr.end_fill()
    car_scr.penup()
    car_scr.goto(i+200, 50)
    car_scr.pendown()
    car_scr.forward(49.50)
    # Draw the two wheels
    car_scr.penup()
    car_scr.goto(i+100, -10-j)
    car_scr.pendown()
    car_scr.color('black')
    car_scr.fillcolor('black')
    car_scr.begin_fill()
    car_scr.circle(20)
    car_scr.end_fill()
    car_scr.penup()
    car_scr.goto(i+300, -10-j)
    car_scr.pendown()
    car_scr.color('black')
    car_scr.fillcolor('black')
    car_scr.begin_fill()
    car_scr.circle(20)
    car_scr.end_fill()
Draw_Car(-200,0)
car_scr.done() | 
Python Turtleの実行
以下のコードで画面に車を描いてみましょう。
車を描き終わったら、done関数を使ってアプリケーションの画面を閉じます。


車を描くための完全な Python のタートルコード

上記のコードを実行すると、システム画面に新しい画面が表示され、アプリケーションの画面に車が描画され始めます。
まとめ
Python の Turtle ライブラリを使用して、画面に車を描く方法がわかりました。
お読みいただきありがとうございました。
このチュートリアルが気に入ったのなら、次のチュートリアルにも目を通すことをお勧めします。
- Tkinterを使ったさまざまな図形の描き方
 - Tkinterを使って線を描く – 初心者のための基礎知識
 - OpenCVを使って図形を描く – 完全なハウツーガイド
 
もっと学ぶために読み続けてください。