[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

【カメ】Pythonで激突カメレース

こんにちは。
年中410エラー
システムソリューション部のかわいです。

最近湿気と暑さと住民税で元気が出ないので、今回はPythonライブラリのturtleを使って
カメたちを競わせたいと思います。↑のGIF画像みたいにいろいろできちゃうので、ぜひ好きなカメを好きなだけ動かしてみてください。

動作環境とコード全文

それでは早速、

使用OS:Microsoft Windows 10 Pro
Pyhtonバージョン:3.10

from turtle import *
from random import randint
import time

#ライン引き
for step in range(11):
    write(step, align='center')
    speed(80)
    right(90)
    forward(10)
    pendown()
    forward(150)
    penup()
    backward(160)
    left(90)
    forward(20)

time.sleep(1)

#赤カメ
red = Turtle()
red.color('red')
red.shape('turtle')
red.penup()
red.goto(0, -10)

#青カメ
blue = Turtle()
blue.color('blue')
blue.shape('turtle')
blue.penup()
blue.goto(0, -30)

#黄カメ
yellow = Turtle()
yellow.color('orange')
yellow.shape('turtle')
yellow.penup()
yellow.goto(0, -60)

#緑カメ
green = Turtle()
green.color('green')
green.shape('turtle')
green.penup()
green.goto(0, -90)

#黒カメ
black = Turtle()
black.color('black')
black.shape('turtle')
black.penup()
black.goto(0, 60)

#ネズミ亀
gray = Turtle()
gray.color('gray')
gray.shape('turtle')
gray.penup()
gray.goto(0, -120)

#回転カメ
pink = Turtle()
pink.color('pink')
pink.shape('turtle')
pink.penup()
pink.goto(0, -150)

time.sleep(3)

#それぞれのカメを動かすx100
for kame in range(100):
    red.forward(randint(1, 2))
    blue.forward(randint(-1, 3))
    yellow.forward(randint(-3, 4))
    green.forward(randint(-4, 5))
    black.right(90)
    gray.forward(randint(-3, -2))
    pink.forward(randint(-10, 10))

コード解説

各ライブラリをインポートします。
カメをランダムに走らせるためにrandom
timeはカメを配置してからちょっと待機させるための演出用なのでなくてもok

from turtle import *
from random import randint
import time

正直気にする箇所はここだけ。
0を起点に、レース場として10本線が引かれるまでforで繰り返します。

for step in range(11):
    write(step, align='center')
    speed(80)
    right(90)
    forward(10)
    pendown()
    forward(150)
    penup()
    backward(160)
    left(90)
    forward(20)

color()を使ってカメの色を指定します。
fillcolor()でもう少し細かく色指定ができます。
shape()でカメの形、goto()で最初の配置座標を指定します。後続のカメはデバッグしながら少しずつズラしていけばok

#赤カメ
red = Turtle()
red.color('red')
red.shape('turtle')
red.penup()
red.goto(0, -10)

100回動くようにforで回します。
それぞれのカメごとに(数字, 数字)のどちらかの数字がrandom選択されるようになってます。
今回はめんどくさくて記述してませんが、ゴールした時に「ゴール!」みたいな表示を出せばたぶん雰囲気出ますね。

for kame in range(100):
    red.forward(randint(1, 2))
    blue.forward(randint(-1, 3))
    yellow.forward(randint(-3, 4))
    green.forward(randint(-4, 5))
    black.right(90)
    gray.forward(randint(-3, -2))
    pink.forward(randint(-10, 10))

もともとturtlrは描写ライブラリみたいですが、カメshapeを入れたばっかりにこんな使い方もできます。
(詳細な使い方は公式ドキュメント参照したれり)
Python入門にはうってつけだと思うので、ぜひクーラーのきいた涼しい部屋でカメカメしてみてください。
ではでは

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
13
読み込み中...
13 票, 平均: 1.00 / 113
754
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Kawa Ken


A curious Poke○n who belongs to the System Solution Department.