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, Srpanj
Anonim
Python Tic Tac Toe igra
Python Tic Tac Toe igra

igra python tic tac toe

ova igra je napravljena na pythonu koji je računalni jezik

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

Korak 1: Uvoz

Uvoz
Uvoz

iz uvoza tkinter *

Korak 2: Korijen

Korijen
Korijen

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" gumb = stop_game = Netačno stop_game_tie = Netačno

def Turn (): # Ispis 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 == True: text.delete (0, END) text.insert (0, "TIE MATCH")

def Check_Winner (): # Provjerite postoji li pobjednička globalna stop_game

red_1 = ploča [0] == ploča [1] == ploča [2]! = "-" red_2 = ploča [3] == ploča [4] == ploča [5]! = "-" red_3 = ploča [6] == ploča [7] == ploča [8]! = "-" ako je red_1 ili red_2 ili red_3: stop_game = True

stupac_1 = ploča [0] == ploča [3] == ploča [6]! = "-" stupac_2 = ploča [1] == ploča [4] == ploča [7]! = "-" stupac_3 = ploča [2] == ploča [5] == ploča [8]! = "-" ako je stupac_1 ili stupac_2 ili stupac_3: stop_game = True

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 = Istina

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

ako "-" nije na ploči: stop_game_tie = Istina

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

ako je igrač == "X": player = "O"

else: player = "X"

def Add_Text (pos, play): # Globalni gumb Dodaj tekst

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

def_buttons [pos].konfiguracija (tekst = igra) ploča [pos] = player

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

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

Završni kod Dat će konačan izgled?
Završni kod Dat će konačan izgled?
Završni kod Dat će konačan izgled?
Završni kod Dat će konačan izgled?

# MREŽICA

def New_Match (): # Nova funkcija gumba za podudaranje globalna ploča globalni igrač globalni gumb 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" gumb = stop_game = False stop_game_tie = Lažno skretanje ()

# Gumbi

Skretanje()

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

frame1 = Okvir (okvir) frame1.pack ()

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

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

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

frame2 = Okvir (okvir) frame2.pack ()

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

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

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

frame4 = Okvir (okvir) 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čeni: