Inhoudsopgave:
Video: Python Tic Tac Toe-spel - Ajarnpa
2025 Auteur: John Day | [email protected]. Laatst gewijzigd: 2025-01-13 06:57
python tic tac toe spel
dit spel is gemaakt in python, dat is een computertaal
ik heb een python-editor gebruikt met de naam: pycharm je kunt ook de normale python-code-editor gebruiken
Stap 1: Importeren
van tkinter import *
Stap 2: Wortel
wortel = 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) # Tekst
bord = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Bord
Stap 3: Functie Nadat u deze in de editor hebt gezet, worden waarschuwingen weergegeven Klik op de waarschuwingen en geef Assambel alles
# Functies
player = "X" button = stop_game = False stop_game_tie = False
def Turn(): # Afdrukken Turn text.delete(0, END) text.insert(0, "{}'S TURN".format(player))
def Exit(): root.destroy() exit()
def Game_Over(): # Controleer of het spel voorbij is Check_Winner()
if stop_game == True: Hplayer() text.delete(0, END) text.insert(0, "'{}' WINNER".format(player))
anders: Check_Tie()
if stop_game_tie == True: text.delete(0, END) text.insert(0, "TIE MATCH")
def Check_Winner(): # Controleer of er een winnaar is global stop_game
rij_1 = bord[0] == bord[1] == bord[2] != "-" rij_2 = bord[3] == bord[4] == bord[5] != "-" rij_3 = bord[6] == bord[7] == bord[8] != "-" if row_1 of row_2 of row_3: stop_game = True
column_1 = bord[0] == bord[3] == bord[6] != "-" column_2 = bord[1] == bord[4] == bord[7] != "-" column_3 = bord[2] == bord[5] == bord[8] != "-" if column_1 of column_2 of column_3: stop_game = True
diagonal_1 = bord[0] == bord[4] == bord[8] != "-" diagonal_2 = bord[2] == bord[4] == bord[6] != "-" indien diagonaal_1 of diagonal_2: stop_game = True
def Check_Tie(): # Controleer of het een gelijkspel is global stop_game_tie
als "-" niet in het bord staat: stop_game_tie = True
def Hplayer(): # Managing Turn global player
if speler == "X": speler = "O"
anders: speler = "X"
def Add_Text(pos, play): # Tekst toevoegen globale knop
if pos not in button en stop_game == False en stop_game_tie == False en pos != 9: Turn()
def_buttons[pos].configure(text=play) bord[pos] = speler
button.append(pos) Hplayer() Turn() Game_Over()
Stap 4: Laatste code Zal het een laatste blik geven?
# ROOSTER
def New_Match(): # New Match Button Functie 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="") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" button = stop_game = False stop_game_tie = False Turn()
# Toetsen
Draai()
frame = Frame (root) frame.pack (zijde = BOVEN, anker = NW)
frame1 = Frame(frame) frame1.pack()
button_1 = Button(frame1, text="", command=lambda: Add_Text(0, player), bg = 'pink') button_1.pack(side=LEFT)
button_2 = Button(frame1, text="", command=lambda: Add_Text(1, player), bg = 'pink') button_2.pack(side=LEFT)
button_3 = Button(frame1, text="", command=lambda: Add_Text(2, player), bg = 'pink') button_3.pack(side=LEFT)
frame2 = Frame(frame) frame2.pack()
button_4 = Button(frame2, text="", command=lambda: Add_Text(3, player), bg = 'blue') button_4.pack(side=LEFT)
button_5 = Button(frame2, text="", command=lambda: Add_Text(4, player), bg = 'blue') button_5.pack(side=LEFT)
button_6 = Button(frame2, text="", command=lambda: Add_Text(5, player), bg = 'blue') button_6.pack(side=LEFT)
frame3 = Frame(frame) frame3.pack()
button_7 = Button(frame3, text="", command=lambda: Add_Text(6, player), bg = 'oranje') button_7.pack(side=LEFT)
button_8 = Button(frame3, text="", command=lambda: Add_Text(7, player), bg = 'oranje') button_8.pack(side=LEFT)
button_9 = Button(frame3, text="", command=lambda: Add_Text(8, player), bg = 'oranje') button_9.pack(side=LEFT)
frame4 = Frame(frame) frame4.pack()
button_clear = Button(frame4, text="NEW MATCH", command=lambda: New_Match(), bg = 'violet', voorgrond = 'red') button_clear.pack(side=LEFT)
exit_button = Button(frame4, text="EXIT", command=lambda: Exit(), bg = 'green', voorgrond = 'oranje') 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()