Kazalo:
Video: Igra Python Tic Tac Toe: 4 koraki
2025 Avtor: John Day | [email protected]. Nazadnje spremenjeno: 2025-01-13 06:58
igra python tic tac toe
ta igra je narejena v pythonu, ki je računalniški jezik
Uporabil sem urejevalnik python, imenovan: pycharm, lahko uporabite tudi običajen urejevalnik kod Python
1. korak: Uvoz
iz uvoza tkinter *
Korak: Koren
koren = Tk ()
root.title ("3T TIC TAC TOE") # Naslov
text = Vnos (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Besedilo
board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # deska
3. korak: Funkcija po vnosu v urejevalnik prikaže opozorila Kliknite Warrnigs in dajte Assambel vse
# Funkcije
player = "X" gumb = stop_game = False stop_game_tie = False
def Turn (): # Tiskanje Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (predvajalnik))
def Izhod (): root.destroy () exit ()
def Game_Over (): # Chech, če je igre več Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))
drugo: Check_Tie ()
if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Preverite, ali obstaja zmagovalna globalna stop_game
row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == tabla [7] == tabla [8]! = "-" če vrstica_1 ali vrstica_2 ali vrstica_3: stop_game = True
column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == tabla [5] == tabla [8]! = "-" če je stolpec_1 ali stolpec_2 ali stolpec_3: stop_game = True
diagonal_1 = deska [0] == deska [4] == deska [8]! = "-" diagonala_2 = deska [2] == deska [4] == deska [6]! = "-" če je diagonala_1 ali diagonala_2: stop_game = Res
def Check_Tie (): # Preverite, ali gre za neodločeno ujemanje globalno stop_game_tie
če "-" ni na krovu: stop_game_tie = Res
def Hplayer (): # Upravljanje Turn global player
če je igralec == "X": igralec = "O"
else: player = "X"
def Add_Text (pos, play): # Globalni gumb za dodajanje besedila
če poz ni v gumbu in stop_game == False in stop_game_tie == False in pos! = 9: Turn ()
def_buttons [pos].configure (text = play) board [pos] = player
button.append (pos) Hplayer () Turn () Game_Over ()
4. korak: Končna koda bo dala končni videz?
# MREŽA
def New_Match (): # Nova funkcija gumba za ujemanje globalna plošča globalni igralec globalni gumb globalna stop_game globalna 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 (besedilo = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" gumb = stop_game = False stop_game_tie = False Turn ()
# Gumbi
Obrat()
okvir = okvir (koren) frame.pack (stran = TOP, sidro = SZ)
frame1 = Okvir (okvir) frame1.pack ()
button_1 = Gumb (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'pink') button_1.pack (side = LEFT)
button_2 = Gumb (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'pink') button_2.pack (side = LEFT)
button_3 = Gumb (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (side = LEFT)
frame2 = Okvir (okvir) frame2.pack ()
button_4 = Gumb (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)
button_5 = Gumb (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)
button_6 = Gumb (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)
frame3 = Okvir (okvir) frame3.pack ()
button_7 = Gumb (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)
button_8 = Gumb (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)
button_9 = Gumb (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)
frame4 = Okvir (okvir) frame4.pack ()
button_clear = Button (frame4, text = "NEW MATCH", width = 13, height = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (side = LEFT)
exit_button = Gumb (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 ()