一、说明
做你还记得小时候玩井字游戏吗?这是一个简单的游戏,只需一支笔或铅笔就可以在一张纸上玩。但是你知道你也可以使用Python的Tkinter库创建一个井字游戏吗?在本文中,我们将介绍使用 Tkinter 创建井字游戏的过程。在本文结束时,您将对如何使用 GUI 框架创建简单游戏有一个基本的了解,并且您将与您的朋友和家人一起玩一个有趣的游戏!
二、导入库和模块:
import tkinter as tk
from tkinter import messagebox
代码首先导入所需的模块和库,即 和模块,这些模块和库是创建用户界面和消息提示所必需的。tkinter
messagebox
Python 会在 2023 年消亡吗?
揭穿该语言在 2023 年消亡的神话。
medium.com
三、创建窗口和看板:
window = tk.Tk()
window.title("Tic Tac Toe")
# Create board
def create_board():
for i in range(3):
for j in range(3):
button = tk.Button(window, text="", font=("Arial", 50), height=2, width=6, bg="lightblue", command=lambda row=i, col=j: handle_click(row, col))
button.grid(row=i, column=j, sticky="nsew")
create_board()
该函数用于创建游戏的主窗口。该函数用于使用该方法创建 3x3 井字棋板。tk.Tk()
create_board()
tk.Button()
四、初始化变量:
# Initialize variables
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = 1
初始化游戏变量,包括存储游戏板状态的列表和跟踪当前玩家的变量。board
current_player
五、处理按钮点击
# Handle button clicks
def handle_click(row, col):
global current_player
if board[row][col] == 0:
if current_player == 1:
board[row][col] = "X"
current_player = 2
else:
board[row][col] = "O"
current_player = 1
button = window.grid_slaves(row=row, column=col)[0]
button.config(text=board[row][col])
check_for_winner()
每当单击游戏板上的按钮时,都会调用该函数。它使用当前玩家的移动更新游戏板,将当前玩家更改为下一个玩家,并相应地更新按钮文本。handle_click()
用这 5 本必读书籍掌握 Python 编程
从初学者到专家:学习Python的终极指南。
medium.com
六、检查获胜者或平局
def check_for_winner():
winner = None
# Check rows
for row in board:
if row.count(row[0]) == len(row) and row[0] != 0:
winner = row[0]
break
# Check columns
for col in range(len(board)):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != 0:
winner = board[0][col]
break
# Check diagonals
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != 0:
winner = board[0][0]
elif board[0][2] == board[1][1] == board[2][0] and board[0][2] != 0:
winner = board[0][2]
if all([all(row) for row in board]) and winner is None:
winner = "tie"
if winner:
declare_winner(winner)
每次单击按钮后都会调用该函数以检查是否有获胜者或平局。它会检查游戏板上的获胜组合,包括行、列和对角线,如果未找到获胜者,则返回获胜者或“平局”。check_for_winner()
七、宣布获胜者并重新开始游戏:
def declare_winner(winner):
if winner == "tie":
message = "It's a tie!"
else:
message = f"Player {winner} wins!"
answer = messagebox.askyesno("Game Over", message + " Do you want to restart the game?")
if answer:
global board
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(3):
for j in range(3):
button = window.grid_slaves(row=i, column=j)[0]
button.config(text="")
global current_player
current_player = 1
else:
window.quit()
八、运行游戏:
window.mainloop()
调用该函数以启动游戏并运行窗口,直到用户关闭窗口或结束游戏。window.mainloop()
九、完整代码:
import tkinter as tk
from tkinter import messagebox
window = tk.Tk()
window.title("Tic Tac Toe")
# Create board
def create_board():
for i in range(3):
for j in range(3):
button = tk.Button(window, text="", font=("Arial", 50), height=2, width=6, bg="lightblue", command=lambda row=i, col=j: handle_click(row, col))
button.grid(row=i, column=j, sticky="nsew")
create_board()
# Initialize variables
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = 1
# Handle button clicks
def handle_click(row, col):
global current_player
if board[row][col] == 0:
if current_player == 1:
board[row][col] = "X"
current_player = 2
else:
board[row][col] = "O"
current_player = 1
button = window.grid_slaves(row=row, column=col)[0]
button.config(text=board[row][col])
check_for_winner()
# Check for a winner or a tie
def check_for_winner():
winner = None
# Check rows
for row in board:
if row.count(row[0]) == len(row) and row[0] != 0:
winner = row[0]
break
# Check columns
for col in range(len(board)):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != 0:
winner = board[0][col]
break
# Check diagonals
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != 0:
winner = board[0][0]
elif board[0][2] == board[1][1] == board[2][0] and board[0][2] != 0:
winner = board[0][2]
if all([all(row) for row in board]) and winner is None:
winner = "tie"
if winner:
declare_winner(winner)
# Declare the winner and ask to restart the game
def declare_winner(winner):
if winner == "tie":
message = "It's a tie!"
else:
message = f"Player {winner} wins!"
answer = messagebox.askyesno("Game Over", message + " Do you want to restart the game?")
if answer:
global board
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i in range(3):
for j in range(3):
button = window.grid_slaves(row=i, column=j)[0]
button.config(text="")
global current_player
current_player = 1
else:
window.quit()
window.mainloop()
十、结果
十一、结论
总之,我们已经看到了如何使用Python和Tkinter库创建一个简单的井字游戏。我们首先使用 Tkinter 按钮创建游戏板,然后添加处理按钮单击、检查获胜者或平局以及宣布获胜者的功能。在此过程中,我们还学习了一些有用的 Tkinter 函数,例如 grid() 和 grid_slaves()。按照本文中概述的步骤,您可以根据自己的喜好自定义游戏并创建自己的井字游戏版本。请记住尝试不同的颜色、字体和动画,以使游戏更加有趣和引人入胜。阿拉法特伊斯兰教