Created
January 12, 2018 13:56
-
-
Save nexusyuumilo/d423ec02d0406cc1c530393ba391a1c0 to your computer and use it in GitHub Desktop.
ランダムあみだくじ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
# turtleでランダムあみだくじを書く | |
import turtle | |
import random | |
# turtleを使う準備 | |
s = turtle.Screen() | |
s.mode("logo") | |
t = turtle.RawTurtle(s) | |
# 線の本数を設定 | |
n = 7 | |
# n体のturtleを要素として持つリスト | |
ts = [turtle.RawTurtle(s) for i in range(1, n+1)] | |
# 線の開始位置へ移動 | |
t.penup() | |
t.right(180) | |
x = -n*10*3 | |
y = 150 | |
t.setpos(x, y) | |
# 縦線をn本書く | |
for i in range(1, n+1): | |
t.pendown() | |
t.stamp() | |
t.penup() | |
ts[i-1].penup() | |
ts[i-1].right(180) | |
ts[i-1].shape("turtle") | |
ts[i-1].setpos(x, y) | |
ts[i-1].pendown() | |
ts[i-1].fd(300) | |
ts[i-1].right(180) | |
x += 70 | |
t.setx(x) | |
t.ht() | |
# 横線をランダムに書く、同時に曲がる地点を記録 | |
# 曲がる地点を記録するリスト | |
r_corner = [] | |
l_corner = [] | |
# 1周目 | |
for i in range(1, n+1): | |
r = random.randint(1, 3) | |
if i < 4: | |
if i % 2 == 0: | |
ts[i-1].fd(40) | |
else: | |
ts[i-1].fd(80) | |
if r == 1 or r == 2: | |
l_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].right(90) | |
ts[i-1].fd(70) | |
r_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].right(180) | |
ts[i-1].fd(70) | |
ts[i-1].right(90) | |
elif i > 4: | |
if i % 2 != 0: | |
ts[i-1].fd(40) | |
else: | |
ts[i-1].fd(80) | |
if r == 1 or r == 2: | |
r_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].left(90) | |
ts[i-1].fd(70) | |
l_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].left(180) | |
ts[i-1].fd(70) | |
ts[i-1].left(90) | |
# 2、3周目 | |
a = 0 | |
while a < 2: | |
for i in range(1, n+1): | |
r = random.randint(1, 3) | |
if i < 4: | |
ts[i-1].fd(80) | |
if r == 1 or r == 2: | |
l_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].right(90) | |
ts[i-1].fd(70) | |
r_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].right(180) | |
ts[i-1].fd(70) | |
ts[i-1].right(90) | |
elif i > 4: | |
ts[i-1].fd(80) | |
if r == 1 or r == 2: | |
r_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].left(90) | |
ts[i-1].fd(70) | |
l_corner.append([int(round(ts[i-1].xcor())), int(round(ts[i-1].ycor()))]) | |
ts[i-1].left(180) | |
ts[i-1].fd(70) | |
ts[i-1].left(90) | |
a += 1 | |
# 横線の数を調整 | |
r = random.randint(1, 3) | |
r2 = random.randint(1, 4) | |
ts[1].fd(60) | |
if r == 1 or r == 2: | |
l_corner.append([int(round(ts[1].xcor())), int(round(ts[1].ycor()))]) | |
ts[1].right(90) | |
ts[1].fd(70) | |
r_corner.append([int(round(ts[1].xcor())), int(round(ts[1].ycor()))]) | |
ts[1].right(180) | |
ts[1].fd(70) | |
ts[1].right(90) | |
if r2 == 1 or r2 == 2: | |
ts[1].fd(20) | |
r_corner.append([int(round(ts[1].xcor())), int(round(ts[1].ycor()))]) | |
ts[1].left(90) | |
ts[1].fd(70) | |
l_corner.append([int(round(ts[1].xcor())), int(round(ts[1].ycor()))]) | |
ts[1].left(180) | |
ts[1].fd(70) | |
ts[1].left(90) | |
r = random.randint(1, 3) | |
r2 = random.randint(1, 4) | |
ts[5].fd(20) | |
if r == 1 or r == 2: | |
l_corner.append([int(round(ts[5].xcor())), int(round(ts[5].ycor()))]) | |
ts[5].right(90) | |
ts[5].fd(70) | |
r_corner.append([int(round(ts[5].xcor())), int(round(ts[5].ycor()))]) | |
ts[5].right(180) | |
ts[5].fd(70) | |
ts[5].right(90) | |
if r2 == 1 or r2 == 2: | |
ts[5].fd(20) | |
r_corner.append([int(round(ts[5].xcor())), int(round(ts[5].ycor()))]) | |
ts[5].left(90) | |
ts[5].fd(70) | |
l_corner.append([int(round(ts[5].xcor())), int(round(ts[5].ycor()))]) | |
ts[5].left(180) | |
ts[5].fd(70) | |
ts[5].left(90) | |
# 亀をみんな下に持ってくる | |
for i in range(1, n+1): | |
ts[i-1].right(180) | |
ts[i-1].sety(-150) | |
ts[i-1].right(180) | |
# 真ん中の亀を操作して最後の仕上げ | |
cent = ts[3] | |
cent.fd(60) | |
# 右下半分に横線追加 | |
b = 0 | |
while b < 3: | |
l_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.right(90) | |
cent.fd(70) | |
r_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.left(90) | |
cent.fd(40) | |
b += 1 | |
# 右上半分に横線追加 | |
b = 0 | |
while b < 3: | |
r_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.left(90) | |
cent.fd(70) | |
l_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.right(90) | |
cent.fd(40) | |
b += 1 | |
# 折り返し地点で向きを変える | |
cent.right(180) | |
cent.fd(80) | |
# 左上半分に横線追加 | |
b = 0 | |
while b < 3: | |
r_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.right(90) | |
cent.fd(70) | |
l_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.left(90) | |
cent.fd(40) | |
b += 1 | |
# 左下半分に横線追加 | |
b = 0 | |
while b < 3: | |
l_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.left(90) | |
cent.fd(70) | |
r_corner.append([int(round(cent.xcor())), int(round(cent.ycor()))]) | |
cent.right(90) | |
cent.fd(20) | |
# 座標を微調整 | |
if b != 2: | |
cent.fd(20) | |
b += 1 | |
# 亀がみんな上を向く | |
cent.right(180) | |
# 上に移動、下を向く | |
for tur in ts: | |
tur.shape("turtle") | |
tur.sety(150) | |
tur.right(180) | |
# 亀の色を7色にする | |
ts[0].fillcolor("red") | |
ts[1].fillcolor("green") | |
ts[2].fillcolor("gray") | |
ts[3].fillcolor("cyan") | |
ts[4].fillcolor("magenta") | |
ts[5].fillcolor("yellow") | |
ts[6].fillcolor("blue") | |
for t in ts: | |
t.shape("classic") | |
t.stamp() | |
t.shape("turtle") | |
# あみだを下る | |
for tu in ts: | |
for i in range(15): | |
tu.fd(20) | |
if [int(round(tu.xcor())), int(round(tu.ycor()))] in r_corner: | |
tu.right(90) | |
tu.fd(35) | |
tu.fd(35) | |
tu.left(90) | |
elif [int(round(tu.xcor())), int(round(tu.ycor()))] in l_corner: | |
tu.left(90) | |
tu.fd(35) | |
tu.fd(35) | |
tu.right(90) | |
# 当たりをランダムに決める | |
win = random.randint(1, 7) | |
ts[win-1].turtlesize(1.2, 1.2) | |
ts[win-1].turtlesize(1.4, 1.4) | |
ts[win-1].turtlesize(1.6, 1.6) | |
ts[win-1].turtlesize(1.8, 1.8) | |
ts[win-1].turtlesize(2, 2) | |
ts[win-1].turtlesize(2.2, 2.2) | |
ts[win-1].turtlesize(2.4, 2.4) | |
ts[win-1].turtlesize(2.6, 2.6) | |
ts[win-1].turtlesize(2.8, 2.8) | |
ts[win-1].turtlesize(3, 3) | |
for i in range(50): | |
ts[win-1].fd(1) | |
# 閉じる | |
s.exitonclick() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment