Tabla de contenido:
Video: Juego Python Tic Tac Toe: 4 pasos
2025 Autor: John Day | [email protected]. Última modificación: 2025-01-13 06:57
juego de tic tac toe de pitón
este juego está hecho en python que es un lenguaje de computadora
He usado un editor de Python llamado: pycharm, también puede usar el editor de código Python normal.
Paso 1: Importar
desde tkinter import *
Paso 2: raíz
raíz = Tk ()
root.title ("3T TIC TAC TOE") # Título
text = Entry (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Texto
tablero = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Tablero
Paso 3: función después de poner en el editor Muestra advertencias Haga clic en las ventanas y entréguele todo a Assambel
# Funciones
jugador = botón "X" = stop_game = False stop_game_tie = False
def Turn (): # Imprimiendo Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (jugador))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech si el juego ha terminado Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (jugador))
más: Check_Tie ()
if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Comprueba si hay un ganador global stop_game
fila_1 = tablero [0] == tablero [1] == tablero [2]! = "-" fila_2 = tablero [3] == tablero [4] == tablero [5]! = "-" fila_3 = tablero [6] == tablero [7] == tablero [8]! = "-" si fila_1 o fila_2 o fila_3: stop_game = True
column_1 = tablero [0] == tablero [3] == tablero [6]! = "-" columna_2 = tablero [1] == tablero [4] == tablero [7]! = "-" columna_3 = tablero [2] == tablero [5] == tablero [8]! = "-" si column_1 o column_2 o column_3: stop_game = True
diagonal_1 = tablero [0] == tablero [4] == tablero [8]! = "-" diagonal_2 = tablero [2] == tablero [4] == tablero [6]! = "-" si diagonal_1 o diagonal_2: stop_game = Verdadero
def Check_Tie (): # Comprueba si es un empate coincide con global stop_game_tie
si "-" no está en el tablero: stop_game_tie = True
def Hplayer (): # Gestión del jugador global de Turno
si jugador == "X": jugador = "O"
más: jugador = "X"
def Add_Text (pos, play): # Botón global Agregar texto
if pos no en button y stop_game == False y stop_game_tie == False y pos! = 9: Turn ()
def_buttons [pos].configure (texto = reproducir) tablero [pos] = jugador
button.append (pos) Hplayer () Turn () Game_Over ()
Paso 4: Código final ¿Dará un vistazo final?
# RED
def New_Match (): # Función de botón de nueva partida global board global player global button 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 (texto = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") tablero = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] jugador = botón "X" = stop_game = False stop_game_tie = False Turn ()
# Botones
Girar()
frame = Frame (raíz) frame.pack (lateral = SUPERIOR, ancla = NW)
frame1 = Frame (marco) 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 (lateral = IZQUIERDA)
button_3 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (lateral = IZQUIERDA)
frame2 = Frame (marco) frame2.pack ()
button_4 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (lateral = IZQUIERDA)
button_5 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (lateral = IZQUIERDA)
button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (lateral = IZQUIERDA)
frame3 = Frame (marco) frame3.pack ()
button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (lateral = IZQUIERDA)
button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (lateral = IZQUIERDA)
button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (lateral = IZQUIERDA)
frame4 = Frame (marco) frame4.pack ()
button_clear = Button (frame4, text = "NEW MATCH", width = 13, height = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (lado = IZQUIERDA)
exit_button = Button (frame4, text = "EXIT", width = 12, height = 3, command = lambda: Exit (), bg = 'green', foreground = 'orange') exit_button.pack (lateral = IZQUIERDA)
def_buttons = [button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9]
root.mainloop ()