import tkinter as tk
from tkinter import messagebox
# Initialize the main window
root = tk.Tk()
root.title("Tic-Tac-Toe")
root.configure(bg='lightblue') # Set background color
# Variables to store the state of the game
board = [' ' for _ in range(9)]
player = 'X'
scores = {'X': 0, 'O': 0}
# Function to update the button text and board state
def update_board(index, button):
global player
if board[index] == ' ':
board[index] = player
button.config(text=player, fg='blue' if player == 'X' else 'green', bg='white')
if check_win():
messagebox.showinfo("Tic-Tac-Toe", f"Player {player} wins!")
scores[player] += 1
reset_board()
elif ' ' not in board:
messagebox.showinfo("Tic-Tac-Toe", "No winner!")
reset_board()
else:
player = 'O' if player == 'X' else 'X'
# Function to check for a win
def check_win():
win_conditions = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # Rows
(0, 3, 6), (1, 4, 7), (2, 5, 8), # Columns
(0, 4, 8), (2, 4, 6) # Diagonals
]
for i, j, k in win_conditions:
if board[i] == board[j] == board[k] != ' ':
return True
return False
# Function to reset the board
def reset_board():
global board, player
board = [' ' for _ in range(9)]
player = 'X'
for button in buttons:
button.config(text='', bg='lightgrey')
# Function to show the scores when closing the game
def on_closing():
messagebox.showinfo("Scores", f"Player X: {scores['X']} wins\nPlayer O: {scores['O']} wins")
root.destroy()
# Create buttons for the Tic-Tac-Toe board
buttons = []
for i in range(9):
button = tk.Button(root, text=' ', width=10, height=5, bg='lightgrey',
command=lambda i=i: update_board(i, buttons[i]))
button.grid(row=i//3, column=i%3, padx=5, pady=5) # Add padding for better spacing
buttons.append(button)
# Bind the closing event to the on_closing function
root.protocol("WM_DELETE_WINDOW", on_closing)
# Run the main loop
root.mainloop()
#tikinter | #game | آموزش پایتون
📸Taking photos using your system's webcam in Python
📸گرفتن عکس در پایتون با وبکم سیستم
#opencv_python | #cv2
Write colored text in Python
متن هاتو در پایتون رنگی بنویس😍
#termcolor | #colorama
تحلیل کد بالا
با اجرای کد، فقط خروجی False در کنسول چاپ خواهد شد، زیرا رشته txt با حرف بزرگ "H" شروع نمیشود. تابع capitalize() در اینجا به دلیل عدم ذخیره خروجی، هیچ تأثیری در متغیر txt نداشته است.
بنابراین، اگر بخواهید خروجی True باشد باید capitalize را به یک متغیر اختصاص دهید:
txt = txt.capitalize()
در این حالت، txt مقدار "Hello, welcome to my world." را خواهد داشت.
آموزش پایتون
🙄خروجی قطعه کد بالا کدام است؟ #تست_تایم | آموزش پایتون
پاسخ صحیح: گزینه B
makinggames.pdf
حجم:
4.6M
📚«Making Games with Python & pygame»
📚کتاب "ساخت بازی با پایتون"
✅ساخت بازی و دوست دارید؟؟
این کتاب طور خاص به مباحث مربوط به استفاده از کتابخانه Pygame میپردازد.
✅آشنایی با اصول اولیه برنامهنویسی و توسعه بازی و ساخت بازیهای سرگرمکننده.
#معرفی_کتاب | #بوک_تایم | آموزش پایتون
1.6M حجم رسانه بالاست
مشاهده در ایتا
Morse Code Converter
🧑💻تبدیل کنندهٔ کد مورس
#tikinter | #morse_code