Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Created July 25, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishimotz/67b1e1cd245eadb2b2597db650a8c772 to your computer and use it in GitHub Desktop.
Save nishimotz/67b1e1cd245eadb2b2597db650a8c772 to your computer and use it in GitHub Desktop.
まわり将棋 2020-07-25
# まわり将棋
# 2020-07-25 by nishimotz
import tkinter as tk
import random
class Application(tk.Frame):
def __init__(self, root):
super().__init__(root)
self.root = root
self.root.title("まわり将棋")
self.pack()
self.player_progress = 0
self.player_text = "歩"
self.create_widgets()
def create_widgets(self):
self.cv = tk.Canvas(self, width=440, height=440)
self.cv.create_rectangle(0, 0, 440, 440, fill="white", width=0)
for i in range(1, 11):
x = 40 * i
y = 40
l = 40 * 9
self.cv.create_line(x, y, x, y+l)
x = 40
y = 40 * i
self.cv.create_line(x, y, x+l, y)
self.cv.create_text(60, 60, text=self.player_text, tag="player")
self.cv.pack(side="top")
self.lb = tk.Label(self)
self.lb["text"] = "金将を投げてください"
self.lb.pack(side="top")
self.bt = tk.Button(self)
self.bt["text"] = "金将を投げる"
self.bt["command"] = self.button_pressed
self.bt.pack(side="top")
def set_player_pos(self, x, y):
self.cv.coords("player", x, y)
def set_player_text(self, text):
self.cv.itemconfigure("player", text=text)
def set_message(self, text):
self.lb["text"] = text
def calc_score(self):
# 50% : 0
# 49% : 1
# 0.8% : 5
# 0.1% : 10
# 0.1% : 20
return random.choice(
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1,
random.choice(
[5, 5, 5, 5, 5, 5, 5, 5, 10, 20]
)
]
)
def button_pressed(self):
# 金将を四個投げる
scores = [0, 0, 0, 0]
for i in range(4):
scores[i] = self.calc_score()
message = ", ".join([str(i) for i in scores]) + " が出た。"
score_sum = sum(scores)
message += f"{score_sum}マス進んだ。"
self.player_progress += score_sum
# 四隅に止まったら交換またはあがり
# 進んでいない場合は何もしない
if score_sum > 0 and self.player_progress in (8, 16, 24, 32):
if self.player_text == "歩":
self.player_text = "香"
message += "香車になった。"
elif self.player_text == "香":
self.player_text = "桂"
message += "桂馬になった。"
elif self.player_text == "桂":
self.player_text = "銀"
message += "銀将になった。"
elif self.player_text == "銀":
self.player_text = "角"
message += "成銀になった。"
elif self.player_text == "角":
self.player_text = "飛"
message += "飛車になった。"
elif self.player_text == "飛":
self.player_text = "王"
message += "王将になった。"
elif self.player_text == "王":
message += "あがり!"
# 32で一周
if self.player_progress >= 32:
self.player_progress -= 32
# 座標の計算
# 0..7:上端 8..15:右端 16..23:下端 24..31:左端
if 0 <= self.player_progress <= 7:
x = 60 + 40 * self.player_progress
y = 60
elif 8 <= self.player_progress <= 15:
x = 60 + 40 * 8
y = 60 + 40 * (self.player_progress - 8)
elif 16 <= self.player_progress <= 23:
x = 60 + 40 * (24 - self.player_progress)
y = 60 + 40 * 8
else:
x = 60
y = 60 + 40 * (32 - self.player_progress)
# プレイヤーとメッセージの更新
self.set_player_pos(x, y)
self.set_player_text(self.player_text)
self.set_message(message)
root = tk.Tk()
app = Application(root=root)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment