Satura rādītājs:

Python Tic Tac Toe spēle: 4 soļi
Python Tic Tac Toe spēle: 4 soļi

Video: Python Tic Tac Toe spēle: 4 soļi

Video: Python Tic Tac Toe spēle: 4 soļi
Video: Tic Tac Toe Game In Python | Python Tic Tac Toe Tutorial | Python Projects | Simplilearn 2024, Novembris
Anonim
Spēle Python Tic Tac Toe
Spēle Python Tic Tac Toe

spēle python tic tac toe

šī spēle ir veidota python datorvalodā

Esmu izmantojis python redaktoru ar nosaukumu: pycharm, varat izmantot arī parasto python koda redaktoru

1. darbība. Importēšana

Importēt
Importēt

no tkinter importa *

2. solis: sakne

Sakne
Sakne

sakne = Tk ()

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

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

tāfele = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Dēlis

3. darbība. Funkcija pēc redaktorā ievietošanas parāda karnevālus Noklikšķiniet uz Warrnigs un dodiet Assambel

# Funkcijas

player = "X" poga = stop_game = False stop_game_tie = False

def Turn (): # Drukāšana Pagrieziet text.delete (0, END) text.insert (0, "{} 'S TURN".formāts (atskaņotājs))

def Iziet (): root.destroy () izeja ()

def Game_Over (): # Chech, ja spēle ir beigusies Check_Winner ()

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

cits: Check_Tie ()

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

def Check_Winner (): # Pārbaudiet, vai ir uzvarētāja globālā stop_game

rinda_1 = tāfele [0] == tāfele [1] == tāfele [2]! = "-" rinda_2 = tāfele [3] == tāfele [4] == tāfele [5]! = "-" rinda_3 = tāfele [6] == dēlis [7] == dēlis [8]! = "-", ja rinda_1 vai rinda_2 vai rinda_3: stop_game = True

column_1 = tāfele [0] == tāfele [3] == tāfele [6]! = "-" column_2 = tāfele [1] == tāfele [4] == tāfele [7]! = "-" column_3 = tāfele [2] == tāfele [5] == tāfele [8]! = "-", ja kolonna_1 vai kolonna_2 vai kolonna_3: stop_game = True

diagonal_1 = board [0] == board [4] == board [8]! = "-" diagonal_2 = board [2] == board [4] == board [6]! = "-" if diagonal_1 or diagonal_2: stop_game = Taisnība

def Check_Tie (): # Pārbaudiet, vai tas ir neizšķirts spēles globālais stop_game_tie

ja "-" nav iekļauts: stop_game_tie = True

def Hplayer (): # Managing Turn global player

ja spēlētājs == "X": spēlētājs = "O"

cits: spēlētājs = "X"

def Add_Text (poz, play): # Pievienot tekstu globālā poga

ja poz nav pogā un 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 ()

4. solis: galīgais kods tam piešķirs galīgo izskatu?

Galīgais kods tam piešķirs galīgo izskatu?
Galīgais kods tam piešķirs galīgo izskatu?
Galīgais kods tam piešķirs galīgo izskatu?
Galīgais kods tam piešķirs galīgo izskatu?

# GRID

def New_Match (): # New Match Button Function globālais dēlis globālais spēlētājs globālā poga globālā stop_game globālā 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 (teksts = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" button = stop_game = False stop_game_tie = False Turn ()

# Pogas

Pagriezieties ()

rāmis = rāmis (saknes) rāmis. iepakojums (sāns = augšā, enkurs = ZR)

frame1 = rāmis (rāmis) frame1.pack ()

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

button_2 = Poga (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āmis (rāmis) 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 = Poga (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, atskaņotājs), bg = 'zils') button_5.pack (side = LEFT)

button_6 = Poga (kadrs2, teksts = "", platums = 8, augstums = 3, komanda = lambda: Pievienot_tekstu (5, spēlētājs), bg = 'zils') button_6.pack (sāns = LEFT)

frame3 = rāmis (rāmis) frame3.pack ()

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

button_8 = Poga (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āmis (rāmis) frame4.pack ()

button_clear = Poga (frame4, text = "NEW MATCH", platums = 13, augstums = 3, komanda = lambda: New_Match (), bg = 'violeta', priekšplāna = 'sarkana') button_clear.pack (side = LEFT)

exit_button = Poga (frame4, text = "EXIT", platums = 12, augstums = 3, komanda = lambda: Exit (), bg = 'green', foreplan = 'orange') exit_button.pack (side = LEFT)

def_buttons = [poga_1, poga_2, poga_3, poga_4, poga_5, poga_6, poga_7, poga_8, poga_9]

root.mainloop ()

Ieteicams: