【发布时间】:2020-06-11 04:26:16
【问题描述】:
我正在创建井字游戏。在此,我创建了一个重置按钮,它指的是重置功能。在那个函数中,我已经重置了 board 和 stop_game 的值。当我在游戏中间点击重置按钮时,它工作正常。但是,当有人获胜然后我点击重置按钮时,它只会重置棋盘,但按下棋盘按钮时,它什么也不做。请帮我解决这个问题。
from tkinter import *
from copy import deepcopy
import random
game = Tk()
game.title("TIC TAC TOE")
game.geometry("450x500")
#game.configure(bg = '#b3b3b3')
player = 'X'
stop_game = False
def callback(r, c):
global player
if player == 'X' and states[r][c] == 0 and stop_game == False:
board[r][c].configure(text = 'X', fg = '#f64c72')
states[r][c] = 'X'
player = 'O'
if player == 'O' and states[r][c] == 0 and stop_game == False:
board[r][c].configure(text = 'O', fg = '#f64c72')
states[r][c] = 'O'
player = 'X'
checkWinner()
def checkWinner():
global stop_game
win_color = '#3b5b5b'
for i in range(3):
if states[i][0] == states[i][1] == states[i][2] != 0:
board[i][0].config(bg = win_color)
board[i][1].config(bg = win_color)
board[i][2].config(bg = win_color)
stop_game = True
for i in range(3):
if states[0][i] == states[1][i] == states[2][i] != 0:
board[0][i].config(bg = win_color)
board[1][i].config(bg = win_color)
board[2][i].config(bg = win_color)
stop_game = True
if states[0][0] == states[1][1] == states[2][2] != 0:
board[0][0].configure(bg = win_color)
board[1][1].configure(bg = win_color)
board[2][2].configure(bg = win_color)
stop_game = True
if states[2][0] == states[1][1] == states[0][2] != 0:
board[2][0].configure(bg = win_color)
board[1][1].configure(bg = win_color)
board[0][2].configure(bg = win_color)
stop_game = True
f = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
board = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
states = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
def reset():
for i in range(3):
for j in range(3):
board[i][j].configure(text = ' ', fg = '#ffda30', bg = "#242582")
states[i][j] = 0
stop_game = False
for i in range(3):
for j in range(3):
f[i][j] = Frame(game, width = 150, height = 150)
f[i][j].propagate(False)
f[i][j].grid(row = i, column = j, sticky = "nsew", padx = 1, pady = 1)
board[i][j] = Button(f[i][j], font = ("Helvatica", 70), bg = "#242582", fg = "#ffda30",
command = lambda r = i, c = j: callback(r, c))
board[i][j].pack(expand = True, fill = BOTH)
reset_game = Button(text = "Reset the game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "#000000",
command = lambda :reset())
reset_game.grid(row = 3, column = 0, columnspan = 2, sticky = 'nsew')
quit_game = Button(text = "Quit game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "red",
command = lambda :game.destroy())
quit_game.grid(row = 3, column = 2, sticky = 'nsew')
game.resizable(False, False)
game.mainloop()
【问题讨论】:
-
你昨天问了同样的问题!
-
那不一样,我用了两次相同的变量名
标签: python tkinter reset tic-tac-toe