Daftar Isi:
Video: Permainan Python Tic Tac Toe: 4 Langkah
2025 Pengarang: John Day | [email protected]. Terakhir diubah: 2025-01-13 06:57
permainan python tic tac toe
game ini dibuat dengan python itu bahasa komputer
saya telah menggunakan editor python yang disebut: pycharm Anda juga dapat menggunakan editor kode python normal juga
Langkah 1: Impor
dari impor tkinter *
Langkah 2: Root
akar = Tk()
root.title("3T TIC TAC TOE") # Judul
teks = Entri(root, font=("ds-digital", 15)) text.pack(fill=X, padx=5, pady=5, ipadx=5, ipady=5) # Teks
papan = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # Papan
Langkah 3: Fungsi Setelah Dimasukkan Ke Editor Ini Menunjukkan Peringatan Klik Warrnigs dan Berikan Assambel Semua
# Fungsi
player = tombol "X" = stop_game = Salah stop_game_tie = Salah
def Turn(): # Printing Turn text.delete(0, END) text.insert(0, "{}'S TURN".format(player))
def Keluar(): root.destroy() keluar()
def Game_Over(): # Periksa apakah game sudah selesai Check_Winner()
if stop_game == Benar: Hplayer() text.delete(0, END) text.insert(0, "'{}' WINNER".format(player))
lain: Periksa_Tie()
if stop_game_tie == Benar: text.delete(0, END) text.insert(0, "TIE MATCH")
def Check_Winner(): # Periksa apakah ada pemenang global 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] != "-" jika row_1 atau row_2 atau row_3: stop_game = True
column_1 = board[0] == board[3] == board[6] != "-" column_2 = board[1] == board[4] == board[7] != "-" column_3 = board[2] == board[5] == board[8] != "-" jika column_1 atau column_2 atau column_3: stop_game = True
diagonal_1 = papan[0] == papan[4] == papan[8] != "-" diagonal_2 = papan[2] == papan[4] == papan[6] != "-" jika diagonal_1 atau diagonal_2: stop_game = Benar
def Check_Tie(): # Periksa apakah ini adalah pertandingan global stop_game_tie
jika "-" tidak ada di papan: stop_game_tie = Benar
def Hplayer(): # Mengelola Putar pemain global
jika pemain == "X": pemain = "O"
lain: pemain = "X"
def Add_Text(pos, play): # Tambahkan teks tombol global
jika pos tidak ada di tombol dan stop_game == Salah dan stop_game_tie == Salah dan pos != 9: Putar()
def_buttons[pos].configure(text=play) board[pos] = pemain
button.append(pos) Hplayer() Putar() Game_Over()
Langkah 4: Kode Akhir Ini Akan Memberikan Tampilan Terakhir?
# GRID
def New_Match(): # Fungsi Tombol Pertandingan Baru papan global pemain global tombol 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(teks ="") button_7.configure(text="") button_8.configure(text="") button_9.configure(text="") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] player = "X" button = stop_game = Salah stop_game_tie = Putaran Salah()
# Tombol
Berbelok()
frame = Frame(root) frame.pack(side=TOP, anchor=NW)
frame1 = Bingkai(bingkai) frame1.pack()
button_1 = Button(frame1, text="", command=lambda: Add_Text(0, player), bg = 'pink') button_1.pack(side=LEFT)
button_2 = Button(frame1, text="", command=lambda: Add_Text(1, player), bg = 'pink') button_2.pack(side=LEFT)
button_3 = Button(frame1, text="", command=lambda: Add_Text(2, player), bg = 'pink') button_3.pack(side=LEFT)
frame2 = Bingkai(bingkai) frame2.pack()
button_4 = Button(frame2, text="", command=lambda: Add_Text(3, player), bg = 'blue') button_4.pack(side=LEFT)
button_5 = Button(frame2, text="", command=lambda: Add_Text(4, player), bg = 'blue') button_5.pack(side=LEFT)
button_6 = Button(frame2, text="", command=lambda: Add_Text(5, player), bg = 'blue') button_6.pack(side=LEFT)
frame3 = Bingkai(bingkai) frame3.pack()
button_7 = Button(frame3, text="", command=lambda: Add_Text(6, player), bg = 'orange') button_7.pack(side=LEFT)
button_8 = Button(frame3, text="", command=lambda: Add_Text(7, player), bg = 'orange') button_8.pack(side=LEFT)
button_9 = Button(frame3, text="", command=lambda: Add_Text(8, player), bg = 'orange') button_9.pack(side=LEFT)
frame4 = Bingkai(bingkai) frame4.pack()
button_clear = Button(frame4, text="NEW MATCH", command=lambda: New_Match(), bg = 'violet', foreground = 'red') button_clear.pack(side=LEFT)
exit_button = Tombol(frame4, teks="EXIT", lebar=12, tinggi=3, perintah=lambda: Keluar(), bg = 'hijau', latar depan = 'oranye') 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()