Sadržaj:

Python Tic Tac Toe igra: 4 koraka
Python Tic Tac Toe igra: 4 koraka

Video: Python Tic Tac Toe igra: 4 koraka

Video: Python Tic Tac Toe igra: 4 koraka
Video: Пишем игру Крестики Нолики на python и pygame. Tic tac toe game using python and pygame 2024, Juli
Anonim
Python Tic Tac Toe igra
Python Tic Tac Toe igra

python tic tac toe igra

ova igra je napravljena na pythonu koji je kompjuterski jezik

Koristio sam uređivač pythona pod nazivom: pycharm, možete koristiti i normalni uređivač koda pythona

Korak 1: Uvoz

Uvoz
Uvoz

iz tkinter uvoza *

Korak 2: Korijen

Root
Root

korijen = Tk ()

root.title ("3T TIC TAC TOE") # Naslov

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

board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Ploča

Korak 3: Funkcija nakon stavljanja u uređivač Prikazuje upozorenja Pritisnite Warrnigs i dajte Assambel -u sve

# Funkcije

player = "X" dugme = stop_game = Netačno stop_game_tie = Netačno

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

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

def Game_Over (): # Chech ako je igra gotova Check_Winner ()

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

drugo: Check_Tie ()

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

def Check_Winner (): # Provjerite postoji li pobjednik globalna stop_game

row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == ploča [7] == ploča [8]! = "-" ako je red_1 ili red_2 ili red_3: stop_game = Tačno

column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == tabla [5] == tabla [8]! = "-" ako je kolona_1 ili kolona_2 ili kolona_3: stop_game = Tačno

diagonal_1 = ploča [0] == ploča [4] == ploča [8]! = "-" dijagonala_2 = ploča [2] == ploča [4] == ploča [6]! = "-" ako je dijagonala_1 ili dijagonala_2: stop_game = Tačno

def Check_Tie (): # Provjerite radi li se o izjednačenju globalno stop_game_tie

ako "-" nije na ploči: stop_game_tie = Tačno

def Hplayer (): # Upravljanje Uključite globalnog igrača

if player == "X": player = "O"

else: player = "X"

def Add_Text (pos, play): # Globalno dugme za dodavanje teksta

ako poz nije u button and stop_game == False and stop_game_tie == False and pos! = 9: Turn ()

def_buttons [pos].configure (text = play) board [pos] = player

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

Korak 4: Završni kod će dati konačan izgled?

Konačni kod koji će dati konačan izgled?
Konačni kod koji će dati konačan izgled?
Konačni kod koji će dati konačan izgled?
Konačni kod koji će dati konačan izgled?

# GRID

def New_Match (): # Nova funkcija dugmeta za povezivanje globalna ploča globalni igrač globalni taster 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 (text = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" dugme = stop_game = False stop_game_tie = Lažno skretanje ()

# Dugmad

Skreni ()

frame = Frame (root) frame.pack (strana = TOP, sidro = SZ)

frame1 = Frame (frame) frame1.pack ()

button_1 = Dugme (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'pink') button_1.pack (side = LEFT)

button_2 = Dugme (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'pink') button_2.pack (side = LEFT)

button_3 = Dugme (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'pink') button_3.pack (side = LEFT)

frame2 = Frame (frame) 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 = Dugme (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 = Dugme (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)

button_8 = Dugme (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)

button_9 = Dugme (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)

frame4 = Frame (frame) 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 ()

Preporučuje se: