Obsah:

Hra Python Tic Tac Toe: 4 kroky
Hra Python Tic Tac Toe: 4 kroky

Video: Hra Python Tic Tac Toe: 4 kroky

Video: Hra Python Tic Tac Toe: 4 kroky
Video: How to build TIC TAC TOE game? - Python PyQT5 2024, Listopad
Anonim
Hra Python Tic Tac Toe
Hra Python Tic Tac Toe

python tic tac toe hra

tato hra je vytvořena v Pythonu, což je počítačový jazyk

Použil jsem editor pythonu s názvem: pycharm, můžete také použít normální editor kódu pythonu

Krok 1: Import

Import
Import

z importu tkinter *

Krok 2: Root

Vykořenit
Vykořenit

root = Tk ()

root.title ("3T TIC TAC TOE") # Název

text = Entry (root, font = ("ds-digital", 15)) text.pack (fill = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Text

deska = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Prkno

Krok 3: Funkce po vložení do editoru zobrazí varování, klikněte na Warrnigs a dejte Assambel vše

# Funkce

player = "X" button = stop_game = False stop_game_tie = False

def Turn (): # Printing Turn text.delete (0, END) text.insert (0, "{} 'S TURN".format (player))

def Exit (): root.destroy () exit ()

def Game_Over (): # Chech, pokud hra skončila Check_Winner ()

if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER".format (player))

else: Check_Tie ()

if stop_game_tie == True: text.delete (0, END) text.insert (0, "TIE MATCH")

def Check_Winner (): # Zkontrolujte, zda existuje globální stop_game pro vítěze

row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == deska [7] == deska [8]! = "-" pokud řádek_1 nebo řádek_2 nebo řádek_3: stop_game = pravda

column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == deska [5] == deska [8]! = "-" pokud sloupec_1 nebo sloupec_2 nebo sloupec_3: stop_game = pravda

diagonal_1 = deska [0] == deska [4] == deska [8]! = "-" diagonal_2 = deska [2] == deska [4] == deska [6]! = "-" pokud diagonal_1 nebo diagonal_2: stop_game = Pravda

def Check_Tie (): # Zkontrolujte, zda jde o globální stop_game_tie

pokud "-" není na palubě: stop_game_tie = Pravda

def Hplayer (): # Správa Turn globálního hráče

pokud hráč == "X": hráč = "O"

else: player = "X"

def Add_Text (pos, play): # Globální tlačítko Přidat text

pokud pos není v tlačítku a stop_game == False a stop_game_tie == False a pos! = 9: Turn ()

def_buttons [pos].configure (text = play) deska [pos] = hráč

button.append (pos) Hplayer () Turn () Game_Over ()

Krok 4: Konečný kód Poskytne konečný vzhled?

Konečný kód Poskytne konečný vzhled?
Konečný kód Poskytne konečný vzhled?
Konečný kód Poskytne konečný vzhled?
Konečný kód Poskytne konečný vzhled?

# MŘÍŽKA

def New_Match (): # New Match Button 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 = "") deska = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] hráč = "X" tlačítko = stop_game = False stop_game_tie = False Turn ()

# Tlačítka

Otáčet se()

frame = Rám (root) frame.pack (strana = NAHORU, 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", width = 13, height = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (side = LEFT)

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 ()

Doporučuje: