1. Description
Do you remember playing tic tac toe as a kid? This is a simple game that can be played on a piece of paper with just a pen or pencil. But did you know you can also create a tic tac toe game using Python's Tkinter library? In this article, we'll walk through the process of creating a tic tac toe game using Tkinter. By the end of this article, you'll have a basic understanding of how to create a simple game using a GUI framework, and you'll have a fun game to play with your friends and family!
2. Import libraries and modules:
import tkinter as tk
from tkinter import messagebox
The code starts by importing the required modules and libraries, ie and modules, which are required to create the user interface and message prompts.tkinter
messagebox
3. Create windows and boards:
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()
This function is used to create the main window of the game. This function is used to create a 3x3 tic-tac-toe board using this method.tk.Tk()
create_board()
tk.Button()
Fourth, initialize variables:
# Initialize variables
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
current_player = 1
Initializes game variables, including a list to store the state of the game board and a variable to track the current player.board
current_player
5. Handle button clicks
# 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()
This function is called whenever a button on the gamepad is clicked. It updates the game board with the current player's movement, changes the current player to the next player, and updates the button text accordingly.handle_click()
6. Check Winner or 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)
The function is called after every button click to check if there is a winner or a tie. It checks the game board for winning combinations, including rows, columns, and diagonals, and returns the winner or "tie" if no winner is found.check_for_winner()
7. Announce the winner and 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()
8. Run the game:
window.mainloop()
Call this function to start the game and run the window until the user closes the window or ends the game.window.mainloop()
Nine, complete code:
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()
10. Results
11. Conclusion
In summary, we've seen how to create a simple tic tac toe game using Python and the Tkinter library. We start by creating the game board with Tkinter buttons, then add functionality to handle button clicks, check for a winner or tie, and announce the winner. Along the way, we also learned some useful Tkinter functions such as grid() and grid_slaves(). By following the steps outlined in this article, you can customize the game to your liking and create your own version of Tic Tac Toe. Remember to experiment with different colors, fonts and animations to make the game more fun and engaging. arafat islam