Sisällysluettelo:
Video: Python Tic Tac Toe -peli: 4 vaihetta
2025 Kirjoittaja: John Day | [email protected]. Viimeksi muokattu: 2025-01-13 06:57
python tic tac toe peli
Tämä peli on tehty pythonilla, joka on tietokoneen kieli
Olen käyttänyt python -editoria nimeltä: pycharm, voit käyttää myös tavallista python -koodieditoria
Vaihe 1: Tuo
tkinterin tuonnista *
Vaihe 2: Juuri
root = Tk ()
root.title ("3T TIC TAC TOE") # Otsikko
text = Entry (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Teksti
board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Hallitus
Vaihe 3: Toiminto sen jälkeen, kun olet asettanut sen editoriin, se näyttää varoitukset Napsauta Warrnigit ja anna Assambelille kaikki
# Toiminnot
player = "X" -painike = stop_game = False stop_game_tie = False
def Turn (): # Tulostus Käännä teksti.poista (0, END) text.insert (0, "{} 'S TURN".muoto (soitin))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech jos peli on ohi Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (pelaaja))
else: Check_Tie ()
if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Tarkista onko olemassa voittaja maailmanlaajuinen 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] == board [5] == board [8]! = "-" if column_1 or column_2 or column_3: stop_game = True
diagonal_1 = board [0] == board [4] == board [8]! = "-" diagonal_2 = board [2] == board [4] == board [6]! = "-" if diagonal_1 or diagonal_2: stop_game = Totta
def Check_Tie (): # Tarkista, onko se tasapeli maailmanlaajuinen stop_game_tie
jos "-" ei ole mukana: stop_game_tie = Totta
def Hplayer (): # Managing Käännä maailmanlaajuinen pelaaja
jos pelaaja == "X": pelaaja = "O"
else: player = "X"
def Add_Text (pos, play): # Lisää teksti maailmanlaajuinen -painike
jos pos ei ole painikkeessa ja stop_game == False and stop_game_tie == False and pos! = 9: Turn ()
def_buttons [pos].configure (text = play) board [pos] = pelaaja
button.append (pos) Hplayer () Turn () Game_Over ()
Vaihe 4: Lopullinen koodi antaa lopullisen ilmeen?
# GRID
def New_Match (): # New Match Button -toiminto globaali hallitus globaali pelaaja globaali painike 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 (teksti = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" -painike = stop_game = False stop_game_tie = False Turn ()
# Painikkeet
Vuoro()
runko = Kehys (pää) kehys. pakkaus (sivu = YLÖS, ankkuri = NW)
frame1 = Runko (kehys) frame1.pack ()
button_1 = Painike (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 = VASEN)
button_3 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (side = VASEN)
frame2 = Runko (kehys) frame2.pack ()
button_4 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = VASEN)
button_5 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, pelaaja), bg = 'sininen') button_5.pack (side = VASEN)
button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, pelaaja), bg = 'sininen') button_6.pack (side = LEFT)
frame3 = Runko (kehys) frame3.pack ()
button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = VASEN)
button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = VASEN)
button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, pelaaja), bg = 'oranssi') button_9.pack (side = LEFT)
frame4 = Runko (kehys) 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 = VASEN)
exit_button = Painike (frame4, text = "EXIT", leveys = 12, korkeus = 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 ()