Cuprins:

Joc Python Tic Tac Toe: 4 pași
Joc Python Tic Tac Toe: 4 pași

Video: Joc Python Tic Tac Toe: 4 pași

Video: Joc Python Tic Tac Toe: 4 pași
Video: Beginner Python Project From Scratch: Tic Tac Toe 2024, Iulie
Anonim
Joc Python Tic Tac Toe
Joc Python Tic Tac Toe

joc python tic tac toe

acest joc este realizat în python, care este un limbaj pentru computer

Am folosit un editor Python numit: Pycharm, de asemenea, puteți utiliza un editor normal de cod Python

Pasul 1: Importați

Import
Import

din importul tkinter *

Pasul 2: rădăcină

Rădăcină
Rădăcină

rădăcină = Tk ()

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

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

bord = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Bord

Pasul 3: Funcție după ce ați introdus editorul Afișați avarici Faceți clic pe Warrnigs și dați-i lui Assambel totul

# Funcții

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 dacă jocul s-a terminat Check_Winner ()

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

else: Check_Tie ()

dacă stop_game_tie == Adevărat: text.delete (0, END) text.insert (0, "TIE MATCH")

def Check_Winner (): # Verifică dacă există un câștigător global stop_game

row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == bord [7] == bord [8]! = "-" dacă rândul_1 sau rândul_2 sau rândul_3: stop_game = True

column_1 = board [0] == board [3] == board [6]! = "-" column_2 = board [1] == board [4] == board [7]! = "-" column_3 = board [2] == bord [5] == bord [8]! = "-" dacă coloană_1 sau coloană_2 sau coloană_3: stop_game = Adevărat

diagonal_1 = board [0] == board [4] == board [8]! = "-" diagonal_2 = board [2] == board [4] == board [6]! = "-" if diagonal_1 or diagonal_2: stop_game = Adevărat

def Check_Tie (): # Verifică dacă este o egalitate globală stop_game_tie

dacă „-” nu este în tablou: stop_game_tie = True

def Hplayer (): # Managing Turn player global

dacă jucător == "X": jucător = "O"

else: player = "X"

def Add_Text (pos, play): # Adăugați butonul global Text

if pos not in 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 ()

Pasul 4: Cod final Va da o privire finală?

Cod final Va da o privire finală?
Cod final Va da o privire finală?
Cod final Va da o privire finală?
Cod final Va da o privire finală?

# GRILĂ

def New_Match (): # New Match Button Function Funcție bord global player global buton 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 ()

# Butoane

Turn ()

frame = Frame (rădăcină) frame.pack (lateral = TOP, ancoră = NW)

frame1 = Frame (frame) frame1.pack ()

button_1 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (0, player), bg = 'roz') button_1.pack (side = STÂNGA)

button_2 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (1, player), bg = 'roz') button_2.pack (lateral = STÂNGA)

button_3 = Button (frame1, text = "", width = 8, height = 3, command = lambda: Add_Text (2, player), bg = 'roz') button_3.pack (lateral = STÂNGA)

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 (lateral = STÂNGA)

button_5 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (lateral = STÂNGA)

button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (lateral = STÂNGA)

frame3 = Frame (frame) frame3.pack ()

button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (lateral = STÂNGA)

button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (lateral = STÂNGA)

button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (lateral = STÂNGA)

frame4 = Frame (frame) frame4.pack ()

button_clear = Buton (cadru4, text = "MATCH NOU", lățime = 13, înălțime = 3, comandă = lambda: New_Match (), bg = 'violet', prim-plan = 'roșu') button_clear.pack (lateral = STÂNGA)

exit_button = Buton (cadru4, text = "EXIT", lățime = 12, înălțime = 3, comandă = lambda: Exit (), bg = 'verde', prim-plan = 'portocaliu') exit_button.pack (lateral = STÂNGA)

def_buttons = [button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8, button_9]

root.mainloop ()

Recomandat: