Obsah:
Video: Hra Python Tic Tac Toe: 4 kroky
2025 Autor: John Day | [email protected]. Naposledy zmenené: 2025-01-13 06:58
hra python tic tac toe
táto hra je vytvorená v Pythone, ktorý je počítačovým jazykom
Použil som editor pythonu s názvom: pycharm, môžete použiť aj normálny editor kódu pythonu
Krok 1: Import
z importu z tkinter *
Krok 2: Root
koreň = Tk ()
root.title ("3T TIC TAC TOE") # Názov
text = Entry (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Text
doska = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Rada
Krok 3: Funkcia po vložení do editora zobrazuje upozornenia, kliknite na varovania a dajte Assambel všetko
# Funkcie
hráč = tlačidlo "X" = stop_game = False stop_game_tie = Nepravda
def Turn (): # Printing Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (player))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech, ak sa hra skončila Check_Winner ()
ak stop_game == Pravda: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))
else: Check_Tie ()
ak stop_game_tie == True: text.delete (0, END) text.insert (0, „TIE MATCH“)
def Check_Winner (): # Skontrolujte, či existuje víťazná globálna stop_game
row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == board [7] == board [8]! = "-" if row_1 or row_2 or 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] == doska [5] == doska [8]! = "-" ak stĺpec_1 alebo stĺpec_2 alebo stĺpec_3: stop_game = True
diagonal_1 = doska [0] == doska [4] == doska [8]! = "-" diagonal_2 = doska [2] == doska [4] == doska [6]! = "-" ak je diagonal_1 alebo diagonal_2: stop_game = Pravda
def Check_Tie (): # Skontrolujte, či ide o globálnu stop_game_tie
ak "-" nie je na palube: stop_game_tie = Pravda
def Hplayer (): # Správa Turn globálneho hráča
ak hráč == "X": hráč = "O"
else: player = "X"
def Add_Text (pos, play): # Globálne tlačidlo Pridať text
ak nie je v tlačidle a stop_game == nepravda a stop_game_tie == nepravda a pos! = 9: otočte ()
def_buttons [pos].configure (text = play) doska [pos] = hráč
button.append (poz) Hplayer () Turn () Game_Over ()
Krok 4: Konečný kód Poskytne konečný vzhľad?
# GRID
def New_Match (): # New Button Match Function 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 (text = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") doska = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] hráč = tlačidlo "X" = stop_game = False stop_game_tie = False Turn ()
# Tlačidlá
Odbočiť ()
rám = rám (koreň) rám. balík (strana = HORNÁ, kotva = SZ)
frame1 = Rám (rám) 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 = LEFT)
frame2 = Rám (rám) 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 = Rám (rám) frame3.pack ()
button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)
button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)
button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)
frame4 = Rám (rám) frame4.pack ()
button_clear = Button (frame4, text = "NEW MATCH", šírka = 13, výška = 3, príkaz = lambda: New_Match (), bg = 'fialová', popredie = 'červená') button_clear.pack (strana = VĽAVO)
exit_button = Button (frame4, text = "EXIT", width = 12, height = 3, command = lambda: Exit (), bg = 'green', foreground = 'orange') exit_button.pack (side = LEFT)
def_buttons = [button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9]
root.mainloop ()