Turinys:

„Python Tic Tac Toe“žaidimas: 4 žingsniai
„Python Tic Tac Toe“žaidimas: 4 žingsniai

Video: „Python Tic Tac Toe“žaidimas: 4 žingsniai

Video: „Python Tic Tac Toe“žaidimas: 4 žingsniai
Video: Tic Tac Toe Gameplay #shorts #chickenartsvideo 2024, Liepa
Anonim
Žaidimas „Python Tic Tac Toe“
Žaidimas „Python Tic Tac Toe“

Python tic tac toe žaidimas

šis žaidimas sukurtas kompiuterine kalba python

Aš naudoju python redaktorių, pavadintą: pycharm, taip pat galite naudoti įprastą python kodo redaktorių

1 veiksmas: importuokite

Importuoti
Importuoti

iš „tkinter“importo *

2 žingsnis: šaknis

Šaknis
Šaknis

šaknis = Tk ()

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

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

lenta = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Lenta

3 žingsnis: Funkcija įdėjus į redaktorių, parodo Warrnings Spustelėkite Warrnigs ir duokite Assambel viską

# Funkcijos

player = "X" mygtukas = stop_game = False stop_game_tie = False

def Turn (): # Spausdinimas Pasukite text.delete (0, END) text.insert (0, "{} 'S TURN".formatas (grotuvas))

def Išėjimas (): root.destroy () exit ()

def Game_Over (): # Patikrinkite, ar žaidimas baigtas Check_Winner ()

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

dar: Check_Tie ()

if stop_game_tie == Tiesa: text.delete (0, END) text.insert (0, „TIE MATCH“)

def Check_Winner (): # Patikrinkite, ar yra nugalėtojas global stop_game

row_1 = lenta [0] == lenta [1] == lenta [2]! = "-" row_2 = lenta [3] == lenta [4] == lenta [5]! = "-" row_3 = lenta [6] == lenta [7] == lenta [8]! = "-", jei eilutė_1 arba eilutė_2 arba eilutė_3: stop_game = Tiesa

column_1 = lenta [0] == lenta [3] == lenta [6]! = "-" column_2 = lenta [1] == lenta [4] == lenta [7]! = "-" column_3 = lenta [2] == lenta [5] == lenta [8]! = "-", jei stulpelis_1 arba stulpelis_2 arba stulpelis_3: stop_game = Tiesa

įstrižainė_1 = lenta [0] == lenta [4] == lenta [8]! = "-" įstrižainė_2 = lenta [2] == lenta [4] == lenta [6]! = "-", jei įstrižainė_1 arba įstrižainė_2: stop_game = Tiesa

def Check_Tie (): # Patikrinkite, ar tai yra lygiosios rungtynės „stop_game_tie“

jei "-" nėra lentoje: stop_game_tie = Tiesa

def Hplayer (): # Valdymas Pasukite pasaulinį žaidėją

jei žaidėjas == "X": žaidėjas = "O"

else: player = "X"

def Add_Text (pos, play): # Pridėti tekstą visuotinis mygtukas

jei poz nėra mygtuke ir stop_game == False ir 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 žingsnis: galutinis kodas suteiks galutinį vaizdą?

Galutinis kodas suteiks galutinį vaizdą?
Galutinis kodas suteiks galutinį vaizdą?
Galutinis kodas suteiks galutinį vaizdą?
Galutinis kodas suteiks galutinį vaizdą?

# TINKLAS

def New_Match (): # „New Match Button“funkcija pasaulinė lenta pasaulinis žaidėjas pasaulinis mygtukas globalus 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 (tekstas = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" button = stop_game = False stop_game_tie = False Turn ()

# Mygtukai

Pasukti ()

rėmas = rėmo (šaknies) rėmas. pakuotė (šonas = viršuje, inkaras = NW)

frame1 = Rėmas (rėmas) frame1.pack ()

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

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

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

frame2 = Rėmas (rėmas) frame2.pack ()

button_4 = Mygtukas (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)

button_5 = Mygtukas (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, grotuvas), bg = 'blue') button_5.pack (side = LEFT)

button_6 = Mygtukas (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)

frame3 = Rėmas (rėmas) frame3.pack ()

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

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

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

frame4 = Rėmas (rėmas) frame4.pack ()

button_clear = Mygtukas (kadras4, tekstas = "NEW MATCH", plotis = 13, aukštis = 3, komanda = lambda: New_Match (), bg = 'violetinė', planas = 'raudona') button_clear.pack (side = LEFT)

exit_button = Mygtukas (kadras4, tekstas = "EXIT", plotis = 12, aukštis = 3, komanda = lambda: Exit (), bg = 'green', foreplan = 'orange') exit_button.pack (side = LEFT)

def_buttons = [mygtukas_1, mygtukas_2, mygtukas_3, mygtukas_4, mygtukas_5, mygtukas_6, mygtukas_7, mygtukas_8, mygtukas_9]

root.mainloop ()

Rekomenduojamas: