Sisukord:
Video: Python Tic Tac Toe mäng: 4 sammu
2025 Autor: John Day | [email protected]. Viimati modifitseeritud: 2025-01-13 06:57
python tic tac toe mäng
see mäng on tehtud pythonis, mis on arvutikeel
Olen kasutanud pythoni redaktorit nimega: pycharm, saate kasutada ka tavalist pythoni koodiredaktorit
Samm: importige
tkinteri impordist *
2. samm: juur
juur = Tk ()
root.title ("3T TIC TAC TOE") # Pealkiri
text = kirje (juur, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # tekst
board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Juhatus
Samm: funktsioon pärast redaktorisse sisestamist näitab see sõnu
# Funktsioonid
mängija = nupp "X" = stop_game = vale stop_game_tie = vale
def Turn (): # Printimine Pöörake teksti. kustutage (0, END) text.insert (0, "{} 'S TURN".vorming (mängija))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech, kui mäng on läbi Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))
muu: Check_Tie ()
if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Kontrollige, kas on olemas võitja globaalne stop_game
rida_1 = laud [0] == laud [1] == laud [2]! = "-" rida_2 = laud [3] == laud [4] == laud [5]! = "-" rida_3 = laud [6] == laud [7] == laud [8]! = "-" kui rida_1 või rida_2 või rida_3: stop_game = True
column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == tahvel [5] == laud [8]! = "-" kui veerg_1 või veerg_2 või veerg_3: stop_game = Tõsi
diagonaal_1 = laud [0] == laud [4] == laud [8]! = "-" diagonaal_2 = laud [2] == laud [4] == laud [6]! = "-" kui diagonaal_1 või diagonaal_2: stop_game = Tõsi
def Check_Tie (): # Kontrollige, kas see on tasavägine globaalne stop_game_tie
kui "-" pole pardal: stop_game_tie = Tõsi
def Hplayer (): # Managing Muutke globaalne mängija
kui mängija == "X": mängija = "O"
muu: mängija = "X"
def Add_Text (pos, play): # Lisa tekst globaalne nupp
kui pos pole nupus ja stop_game == Vale ja stop_game_tie == Vale ja pos! = 9: Turn ()
def_buttons [pos].configure (text = play) board [pos] = player
button.append (pos) Hplayer () Turn () Game_Over ()
Samm 4: Lõplik kood, mis annab lõpliku ilme?
# GRID
def New_Match (): # New Match Button Funktsioon globaalne laud globaalne mängija globaalne nupp globaalne stop_game globaalne 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 (tekst = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" button = stop_game = False stop_game_tie = False Turn ()
# Nupud
Pööra ()
raam = raam (juur) raam. pakk (külg = ülaosa, ankur = NW)
frame1 = raam (raam) frame1.pack ()
button_1 = Nupp (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'roosa') button_1.pack (side = LEFT)
button_2 = Nupp (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'roosa') button_2.pack (side = LEFT)
button_3 = Nupp (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'roosa') button_3.pack (side = LEFT)
frame2 = raam (raam) frame2.pack ()
button_4 = Nupp (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)
button_5 = Nupp (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)
button_6 = Nupp (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)
frame3 = raam (raam) frame3.pack ()
button_7 = Nupp (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)
button_8 = Nupp (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)
button_9 = Nupp (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)
frame4 = Raam (raam) frame4.pack ()
button_clear = Nupp (frame4, text = "NEW MATCH", laius = 13, kõrgus = 3, käsk = lambda: New_Match (), bg = 'violetne', esiplaan = 'punane') button_clear.pack (side = LEFT)
exit_button = Nupp (raam4, tekst = "VÄLJU", laius = 12, kõrgus = 3, käsk = lambda: Välju (), bg = 'roheline', esiplaan = 'oranž') exit_button.pack (külg = VASAK)
def_buttons = [nupp_1, nupp_2, nupp_3, nupp_4, nupp_5, nupp_6, nupp_7, nupp_8, nupp_9]
root.mainloop ()