Talaan ng mga Nilalaman:

Laro ng Python Tic Tac Toe: 4 Hakbang
Laro ng Python Tic Tac Toe: 4 Hakbang

Video: Laro ng Python Tic Tac Toe: 4 Hakbang

Video: Laro ng Python Tic Tac Toe: 4 Hakbang
Video: Making a Tic Tac Toe Game - Introduction and Setup (Lesson 1-2) 2024, Nobyembre
Anonim
Python Tic Tac Toe Game
Python Tic Tac Toe Game

python tic tac toe game

ang larong ito ay ginawa sa sawa iyon ay isang wika sa computer

Gumamit ako ng isang python editor na tinatawag na: pycharm maaari mo ring gamitin ang normal na editor ng python code din

Hakbang 1: Mag-import

Angkat
Angkat

mula sa tkinter import *

Hakbang 2: Root

Ugat
Ugat

ugat = Tk ()

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

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

board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Lupon

Hakbang 3: Pag-andar Matapos Maglagay sa Editor Ipinapakita nito ang Mga Babala I-click ang Warrnigs at Bigyan ang Lahat ng Assambel

# Mga pagpapaandar

player = "X" button = stop_game = False stop_game_tie = Mali

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

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

def Game_Over (): # Chech kung ang laro ay higit sa Check_Winner ()

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

iba pa: Check_Tie ()

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

def Check_Winner (): # Suriin kung mayroong isang nagwagi pandaigdigang stop_game

row_1 = board [0] == board [1] == board [2]! = "-" row_2 = board [3] == board [4] == board [5]! = "-" row_3 = board [6] == board [7] == board [8]! = "-" kung row_1 o row_2 o row_3: stop_game = True

haligi_1 = board [0] == board [3] == board [6]! = "-" haligi_2 = board [1] == board [4] == board [7]! = "-" haligi_3 = board [2] == board [5] == board [8]! = "-" kung haligi_1 o haligi_2 o haligi_3: stop_game = True

diagonal_1 = board [0] == board [4] == board [8]! = "-" diagonal_2 = board [2] == board [4] == board [6]! = "-" kung diagonal_1 o diagonal_2: stop_game = Tama

def Check_Tie (): # Suriin kung ito ay isang tugma na pandaigdigan stop_game_tie

kung "-" wala sa board: stop_game_tie = True

def Hplayer (): # Pamamahala sa pandaigdigang manlalaro

kung player == "X": player = "O"

iba pa: player = "X"

def Add_Text (pos, play): # Magdagdag ng Tekstong pandaigdigang pindutan

kung ang pos ay hindi sa pindutan at stop_game == Mali at stop_game_tie == Mali at pos! = 9: Lumiko ()

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

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

Hakbang 4: Pangwakas na Code na Magbibigay Ito ng Pangwakas na Pagtingin?

Pangwakas na Code na Magbibigay Ito ng Pangwakas na Pagtingin?
Pangwakas na Code na Magbibigay Ito ng Pangwakas na Pagtingin?
Pangwakas na Code na Magbibigay Ito ng Pangwakas na Pagtingin?
Pangwakas na Code na Magbibigay Ito ng Pangwakas na Pagtingin?

# GRID

def New_Match (): # Bagong Tugma ng Button Function pandaigdigang board global player pandaiguan button 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 ()

# Mga Pindutan

Lumiko()

frame = Frame (root) frame.pack (gilid = TOP, anchor = NW)

frame1 = Frame (frame) frame1.pack ()

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

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

button_3 = Button (frame1, text = "", width = 8, taas = 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, taas = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)

button_5 = Button (frame2, text = "", width = 8, taas = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)

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

frame3 = Frame (frame) frame3.pack ()

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

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

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

frame4 = Frame (frame) frame4.pack ()

button_clear = Button (frame4, text = "BAGONG MATCH", lapad = 13, taas = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (side = LEFT)

exit_button = Button (frame4, text = "EXIT", lapad = 12, taas = 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 ()

Inirerekumendang: