-
Notifications
You must be signed in to change notification settings - Fork 0
/
31game.pyw
101 lines (94 loc) · 3.38 KB
/
31game.pyw
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import PySimpleGUI as sg
import random
import os
import sys
import time
sg.theme("DarkBrown3")
layout = [[sg.T("31ゲームをしよう! 31を言うと負けだよ。")],
[sg.Im(k="img1"), sg.T(k="txt1")],
[sg.T("数を入力してください。", k="txt2")],
[sg.I("",k="in1",size=(15)),
sg.B(" 入力 ", k="btn", bind_return_key=True)]]
win = sg.Window("31ゲーム", layout, font=(None,14), finalize=True)
def get_resource(filename):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, filename)
return os.path.join(os.path.dirname(__file__), filename)
def getnextnums(n):
global nextnums, choicemsg
nextnums = list(range(n+1, min(32, n+4)))
choicemsg = f"{nextnums} から入力してください。"
win["txt2"].update(choicemsg)
win["in1"].update("")
def question():
global playflag
getnextnums(0)
win["txt1"].update("さあ、ゲームを始めるよ!")
win["img1"].update(get_resource("res/futaba0.png"))
playflag = True
win["btn"].update(f" 入力 ")
def com_turn(comnum):
global playflag
player_num = comnum
keynums = [2,6,10,14,18,22,26,30]
getnextnums(comnum)
time.sleep(1)
# 次が31しか選べない状況の場合
if nextnums == [31]:
win["txt1"].update("31。あなたの勝ちだよ。\nおめでとう!")
win["img1"].update(get_resource("res/futaba1.png"))
win["txt2"].update("ボタンを押すと、また遊べるよ。")
win["btn"].update(f" また遊ぶ ")
playflag = False
return
if 31 in nextnums:
# 選択肢から31を言う事は削除する
nextnums.remove(31)
for n in nextnums:
if n in keynums:
comnum = n
if comnum == player_num:
# 必勝法に当てはまらない場合
comnum = random.choice(nextnums)
if random.randint(0,5) == 0:
# 1/6の確率で、必勝法を忘れる
comnum = random.choice(nextnums)
win["txt1"].update(f"ワタシは、[ {comnum} ] にするよ。")
getnextnums(comnum)
def my_turn():
global playflag
if v["in1"].isdecimal() == False:
win["txt1"].update("数字を入力してね。")
win["in1"].update("")
else:
mynum = int(v["in1"])
if mynum in nextnums:
if mynum == 31:
win["txt1"].update("31って言ったね。\nあなたの負けだよ。")
win["img1"].update(get_resource("res/futaba2.png"))
win["txt2"].update("ボタンを押すと、また遊べるよ。")
win["btn"].update(f" また遊ぶ ")
playflag = False
elif mynum == 30:
win["txt1"].update("31。あなたの勝ちだよ。\nおめでとう!")
win["img1"].update(get_resource("res/futaba1.png"))
win["txt2"].update("ボタンを押すと、また遊べるよ。")
win["btn"].update(f" また遊ぶ ")
playflag = False
else:
com_turn(mynum)
else:
# 入力された数が、範囲外の場合
win["txt1"].update(choicemsg)
win["in1"].update("")
question()
while True:
e, v = win.read()
if e == "btn":
if playflag == False:
question()
else:
my_turn()
if e == None:
break
win.close()