Taula de continguts:
2025 Autora: John Day | [email protected]. Última modificació: 2025-01-13 06:57
joc python tic tac toe
aquest joc està fet en python que és un llenguatge informàtic
He utilitzat un editor de python anomenat: pycharm, també podeu utilitzar un editor de codi Python normal
Pas 1: importació
des de la importació de tkinter *
Pas 2: arrel
arrel = Tk ()
root.title ("3T TIC TAC TOE") # Títol
text = Entrada (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Text
tauler = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Pissarra
Pas 3: Funció després de posar-lo a l'editor Mostra els avisos Feu clic a Warrnigs i doneu-ho a Assambel tot
# Funcions
jugador = botó "X" = stop_game = Fals stop_game_tie = Fals
def Turn (): # Printing Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (reproductor))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech si el joc ha finalitzat Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))
else: Check_Tie ()
if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Comproveu si hi ha un guanyador global stop_game
fila_1 = tauler [0] == tauler [1] == tauler [2]! = "-" fila_2 = tauler [3] == tauler [4] == tauler [5]! = "-" fila_3 = tauler [6] == tauler [7] == tauler [8]! = "-" si fila_1 o fila_2 o fila_3: stop_game = True
columna_1 = tauler [0] == tauler [3] == tauler [6]! = "-" columna_2 = tauler [1] == tauler [4] == tauler [7]! = "-" columna_3 = tauler [2] == tauler [5] == tauler [8]! = "-" si column_1 o column_2 o column_3: stop_game = True
diagonal_1 = tauler [0] == tauler [4] == tauler [8]! = "-" diagonal_2 = tauler [2] == tauler [4] == tauler [6]! = "-" si diagonal_1 o diagonal_2: stop_game = cert
def Check_Tie (): # Comproveu si és un partit d'empat global stop_game_tie
si "-" no és al tauler: stop_game_tie = True
def Hplayer (): # Gestió del jugador global Turn
si jugador == "X": jugador = "O"
else: jugador = "X"
def Add_Text (pos, play): # Botó Afegeix text global
si pos no està en el botó i stop_game == False i stop_game_tie == False i pos! = 9: Turn ()
def_buttons [pos].configure (text = play) tauler [pos] = jugador
button.append (pos) Hplayer () Turn () Game_Over ()
Pas 4: el codi final donarà una mirada final?
# REIXA
def New_Match (): # New Match Button Function Funció tauler global jugador global botó global stop_game global stop_game_tie
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 = "X" button = stop_game = False stop_game_tie = False Turn ()
# Botons
Turn ()
frame = Frame (root) frame.pack (lateral = TOP, ancoratge = NW)
frame1 = Frame (frame) frame1.pack ()
button_1 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'pink') button_1.pack (side = LEFT)
button_2 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'pink') button_2.pack (side = LEFT)
button_3 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (side = ESQUERRA)
frame2 = Frame (frame) frame2.pack ()
button_4 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)
button_5 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)
button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)
frame3 = Frame (frame) frame3.pack ()
button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = ESQUERRA)
button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = ESQUERRA)
button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'taronja') button_9.pack (side = LEFT)
frame4 = Frame (frame) frame4.pack ()
button_clear = Botó (frame4, text = "PARTIT NOU", amplada = 13, altura = 3, ordre = lambda: New_Match (), bg = 'violeta', primer pla = 'vermell') button_clear.pack (lateral = ESQUERRA)
exit_button = Botó (frame4, text = "EXIT", amplada = 12, altura = 3, ordre = lambda: Exit (), bg = 'verd', primer pla = 'taronja') exit_button.pack (lateral = ESQUERRA)
def_buttons = [button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9]
root.mainloop ()