INHOUDSOPGAWE:
Video: Python Tic Tac Toe -spel: 4 stappe
2025 Outeur: John Day | [email protected]. Laas verander: 2025-01-13 06:56
spel met python tic tac toe
hierdie speletjie is gemaak in python wat 'n rekenaartaal is
Ek het 'n python -redakteur genaamd: pycharm gebruik, u kan ook 'n normale python -kode -redakteur gebruik
Stap 1: Invoer
vanaf tkinter invoer *
Stap 2: Wortel
root = Tk ()
root.title ("3T TIC TAC TOE") # Titel
text = Entry (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Teks
bord = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Bord
Stap 3: Funksie nadat dit in die redakteur geplaas is. Dit wys waarborge. Klik op die Warrnigs en gee Assambel alles
# Funksies
speler = "X" -knoppie = stop_spel = Valse stop_game_tie = Onwaar
def Turn (): # Printing Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (speler))
def Exit (): root.destroy () exit ()
def Game_Over (): # Chech as die spel verby is Check_Winner ()
if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (speler))
anders: Check_Tie ()
if stop_game_tie == Waar: text.delete (0, END) text.insert (0, "TIE MATCH")
def Check_Winner (): # Kyk of daar 'n wêreldwye stopspel is
row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == bord [7] == bord [8]! = "-" as ry_1 of ry_2 of ry_3: stop_spel = Waar
column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == board [5] == board [8]! = "-" as column_1 of column_2 of 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 = Waar
def Check_Tie (): # Kyk of dit 'n gelykop wedstryd is, globale stop_game_tie
as "-" nie aan boord is nie: stop_game_tie = Waar
def Hplayer (): # Bestuur Turn global player
as speler == "X": speler = "O"
anders: speler = "X"
def Add_Text (pos, play): # Voeg globale teksknoppie by
as pos nie in knoppie is nie en stop_game == Onwaar en stop_spel_tie == Onwaar en pos! = 9: Draai ()
def_buttons [pos].configure (text = play) board [pos] = speler
button.append (pos) Hplayer () Turn () Game_Over ()
Stap 4: Finale kode Dit gee 'n finale kyk?
# GRID
def New_Match (): # New Match Button Function globale bord globale speler globale knoppie 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 (teks = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") bord = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] speler = "X" -knoppie = stop_spel = Valse stop_game_tie = Valse draai ()
# Knoppies
Draai ()
raam = raam (wortel) raam. pak (kant = bo, anker = NW)
raam1 = raam (raam) raam1.pak ()
button_1 = Knoppie (raam1, teks = "", breedte = 8, hoogte = 3, opdrag = lambda: Add_Text (0, speler), bg = 'pienk') knoppie_1.pak (kant = LINKS)
button_2 = Knoppie (raam1, teks = "", breedte = 8, hoogte = 3, opdrag = lambda: Add_Text (1, speler), bg = 'pienk') knoppie_2.pak (sy = LINKS)
button_3 = Knoppie (raam1, teks = "", breedte = 8, hoogte = 3, opdrag = lambda: Add_Text (2, speler), bg = 'pienk') button_3.pack (sy = LEFT)
raam2 = raam (raam) raam2.pak ()
button_4 = Knoppie (frame2, text = "", breedte = 8, hoogte = 3, command = lambda: Add_Text (3, speler), bg = 'blou') button_4.pack (sy = LEFT)
button_5 = Knoppie (frame2, text = "", breedte = 8, hoogte = 3, command = lambda: Add_Text (4, speler), bg = 'blou') button_5.pack (sy = LEFT)
button_6 = Knoppie (frame2, text = "", breedte = 8, hoogte = 3, command = lambda: Add_Text (5, speler), bg = 'blou') button_6.pack (sy = LEFT)
raam3 = raam (raam) raam3.pak ()
button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)
button_8 = Knoppie (frame3, text = "", breedte = 8, hoogte = 3, command = lambda: Add_Text (7, speler), bg = 'oranje') button_8.pack (sy = LEFT)
button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)
raam4 = raam (raam) raam4.pak ()
button_clear = Knoppie (frame4, text = "NEW MATCH", breedte = 13, hoogte = 3, command = lambda: New_Match (), bg = 'violet', voorgrond = 'rooi') button_clear.pack (sy = LINKS)
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 ()