我创建了一个名为 Tic Tac Toe 的游戏。有 2 名玩家,其中一名是 X,其中一名是 Os,您所要做的就是连续获得 3 个符号,而不会被其他人阻止。
游戏的 GUI 如下所示:
代码:
from guizero import App, TextBox, PushButton, Text, info
empty = ' '
player = "X"
def clicked(z):
button = buttonlist[int(z)] # Finds out which button was pressed
global empty, player
if button.text != empty:
pass # If button already pushed do nothing
else:
# Marks button with user's go
button.text = player
# Switches players
if player == "X":
player = "O"
else:
player = "X"
return
def instructions():
info("Instructions","There are 2 players one of them are Xs one of them are Os. All you have to do is you have to try getting your symbol 3 times in a row without the other player blocking you")
def quit_game():
app.destroy()
app = App(title="Tic Tac Toe", layout="grid", width=200, height=250)
buttonlist = [] # Empty list to contain a list of Buttons
Player1_label = Text(app, text="Player 1", align="top", grid=[0, 0, 2 , 1])
Player1 = TextBox(app, text=empty, align="top", grid=[2, 0, 3, 1])# Player 1 enters the username
Player2_label = Text(app, text="Player 2", align="top", grid=[0, 1, 2 , 1])
Player2 = TextBox(app, text=empty, align="top", grid=[2, 1, 3, 1])# Player 2 enters the username
Instructions = PushButton(app, command=instructions, text="Instructions", grid=[0, 5, 3, 1])# Display the instructions
Quit = PushButton(app, command=quit_game ,text="Quit",grid=[10, 0, 3, 2])
# Create Buttons for game, in 3 rows of 3
z=0
for x in range(3):
for y in range(2, 5):
buttonlist.append(PushButton(app, text=empty, args=str(z), grid=[x, y], command=clicked))
z+=1
app.display()
我遇到的问题是显示谁是赢家。我知道如何让一个窗口出现以显示获胜者,而这样做的行是:
app.info("Tic Tac Toe Winner","The winner is" + Player1.value)
但我遇到的问题是在比赛结束时知道谁是赢家,所以并显示他们是赢家,我也有一种感觉,要找出赢家,这与buttonlist
我制作的名单有关,但我只是不知道该怎么做,所以如果有人可以帮助我,我会很高兴。
谢谢