Sommario:

Gioco Tic Tac Toe in pitone: 4 passaggi
Gioco Tic Tac Toe in pitone: 4 passaggi

Video: Gioco Tic Tac Toe in pitone: 4 passaggi

Video: Gioco Tic Tac Toe in pitone: 4 passaggi
Video: Learn Python with ChatGPT | Tic-Tac-Toe Game 2024, Luglio
Anonim
Pitone Tic Tac Toe gioco
Pitone Tic Tac Toe gioco

pitone tic tac toe gioco

questo gioco è realizzato in Python che è un linguaggio informatico

ho usato un editor python chiamato: pycharm puoi anche usare il normale editor di codice python anche

Passaggio 1: importare

Importare
Importare

dall'importazione di tkinter *

Passaggio 2: radice

Radice
Radice

radice = Tk()

root.title(" 3T TIC TAC TOE") # Titolo

text = Entry(root, font=("ds-digital", 15)) text.pack(fill=X, padx=5, pady=5, ipadx=5, ipady=5) # Text

tavola = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Tavola

Passaggio 3: funzione dopo averlo inserito nell'editor Mostra avvisi Fare clic sugli avvisi e assegnare ad Assambel tutto

# Funzioni

player = pulsante "X" = stop_game = False stop_game_tie = False

def Turn(): # Stampa Turn text.delete(0, END) text.insert(0, "{}'S TURN".format(player))

def Exit(): root.destroy() exit()

def Game_Over(): # Verifica se il gioco è finito Check_Winner()

if stop_game == True: Hplayer() text.delete(0, END) text.insert(0, "'{}' WINNER".format(player))

altro: Check_Tie()

if stop_game_tie == True: text.delete(0, END) text.insert(0, "TIE MATCH")

def Check_Winner(): # Controlla se c'è un vincitore globale stop_game

riga_1 = tavola[0] == tavola[1] == tavola[2] != "-" riga_2 = tavola[3] == tavola[4] == tavola[5] != "-" riga_3 = tavola[6] == board[7] == board[8] != "-" if row_1 o row_2 o row_3: stop_game = True

column_1 = board[0] == board[3] == board[6] != "-" column_2 = board[1] == board[4] == board[7] != "-" column_3 = board[2] == board[5] == board[8] != "-" se column_1 o column_2 o column_3: stop_game = True

diagonal_1 = board[0] == board[4] == board[8] != "-" diagonal_2 = board[2] == board[4] == board[6] != "-" se diagonal_1 o diagonal_2: stop_game = True

def Check_Tie(): # Controlla se è un pareggio globale stop_game_tie

se "-" non è presente nel tabellone: stop_game_tie = True

def Hplayer(): # Gestire il giocatore globale di Turn

se giocatore == "X": giocatore = "O"

altrimenti: giocatore = "X"

def Add_Text(pos, play): # Pulsante globale Aggiungi testo

if pos not in button e stop_game == False e stop_game_tie == False e pos != 9: Turn()

def_buttons[pos].configure(text=play) board[pos] = player

button.append(pos) Hplayer() Turn() Game_Over()

Passaggio 4: il codice finale darà un'occhiata finale?

Codice finale darà un'ultima occhiata?
Codice finale darà un'ultima occhiata?
Codice finale darà un'ultima occhiata?
Codice finale darà un'ultima occhiata?

# GRIGLIA

def New_Match(): # Nuova funzione pulsante Match tabellone globale giocatore globale pulsante globale stop_game globale stop_game_tie globale

button_1.configure(text="") button_2.configure(text="") button_3.configure(text="") button_4.configure(text="") button_5.configure(text="") button_6.configure(text ="") button_7.configure(text="") button_8.configure(text="") button_9.configure(text="") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = pulsante "X" = stop_game = False stop_game_tie = False Turn()

# Pulsanti

Giro()

frame = Frame(root) frame.pack(side=TOP, anchor=NW)

frame1 = Frame(frame) frame1.pack()

button_1 = Button(frame1, text="", command=lambda: Add_Text(0, player), bg = 'pink') button_1.pack(side=LEFT)

button_2 = Button(frame1, text="", command=lambda: Add_Text(1, player), bg = 'pink') button_2.pack(side=LEFT)

button_3 = Button(frame1, text="", command=lambda: Add_Text(2, player), bg = 'pink') button_3.pack(side=LEFT)

frame2 = Frame(frame) frame2.pack()

button_4 = Button(frame2, text="", command=lambda: Add_Text(3, player), bg = 'blue') button_4.pack(side=LEFT)

button_5 = Button(frame2, text="", command=lambda: Add_Text(4, player), bg = 'blue') button_5.pack(side=LEFT)

button_6 = Button(frame2, text="", command=lambda: Add_Text(5, player), bg = 'blue') button_6.pack(side=LEFT)

frame3 = Frame(frame) frame3.pack()

button_7 = Button(frame3, text="", command=lambda: Add_Text(6, player), bg = 'orange') button_7.pack(side=LEFT)

button_8 = Button(frame3, text="", command=lambda: Add_Text(7, player), bg = 'orange') button_8.pack(side=LEFT)

button_9 = Button(frame3, text="", command=lambda: Add_Text(8, player), bg = 'orange') button_9.pack(side=LEFT)

frame4 = Frame(frame) frame4.pack()

button_clear = Button(frame4, text="NEW MATCH", command=lambda: New_Match(), bg = 'violet', foreground = 'red') button_clear.pack(side=LEFT)

exit_button = Button(frame4, text="EXIT", command=lambda: Exit(), bg = 'green', foreground = 'orange') exit_button.pack(side=LEFT)

def_buttons = [pulsante_1, pulsante_2, pulsante_3, pulsante_4, pulsante_5, pulsante_6, pulsante_7, pulsante_8, pulsante_9]

root.mainloop()

Consigliato: