人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

python實(shí)現(xiàn)帶界面的井字棋小游戲

發(fā)布日期:2022-01-01 13:17 | 文章來源:腳本之家

今天我們用python+tkinter安裝帶界面的井字棋,效果如圖所示。

Tkinter Python 的標(biāo)準(zhǔn) GUI 庫(kù)。Python 使用 Tkinter 可以快速的創(chuàng)建 GUI 應(yīng)用程序。由于 Tkinter 是內(nèi)置到 python 的安裝包中、只要安裝好 Python 之后就能 import Tkinter 庫(kù)、而且 IDLE 也是用 Tkinter 編寫而成、對(duì)于簡(jiǎn)單的圖形界面 Tkinter 還是能應(yīng)付自如。

pip install tkinter

1、首先安裝tkinter

root= Tk()
root.title('井字棋')
digits = [1,2,3,4,5,6,7,8,9]
mark = '' “
count = 0
panels = ["panel"]*10

2、初始化窗口

Label(root,text="player1 : X",font="times 15").grid(row=0,column=1)
Label(root,text="player2 : O",font="times 15").grid(row=0,column=2)
button1=Button(root,width=15,font=('Times 16 bold'),height=7,command=lambda:checker(1))
button1.grid(row=1,column=1)
button2=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda:checker(2))
button2.grid(row=1,column=2)
button3=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(3))
button3.grid(row=1,column=3)
button4=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(4))
button4.grid(row=2,column=1)
button5=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(5))
button5.grid(row=2,column=2)
button6=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(6))
button6.grid(row=2,column=3)
button7=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(7))
button7.grid(row=3,column=1)
button8=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(8))
button8.grid(row=3,column=2)
button9=Button(root,width=15,height=7,font=('Times 16 bold'),command=lambda: checker(9))
button9.grid(row=3,column=3)

root.mainloop()

3、定義按鈕

很明顯上面的井字棋就是9個(gè)按鈕。這里定義9個(gè)按鈕。

def win(panels,sign):
 return ((panels[1] == panels[2] == panels [3] == sign)
or (panels[1] == panels[4] == panels [7] == sign)
or (panels[1] == panels[5] == panels [9] == sign)
or (panels[2] == panels[5] == panels [8] == sign)
or (panels[3] == panels[6] == panels [9] == sign)
or (panels[3] == panels[5] == panels [7] == sign)
or (panels[4] == panels[5] == panels [6] == sign) 
or (panels[7] == panels[8] == panels [9] == sign))

4、檢查獲勝的條件

檢查獲勝的條件,上面其中一種情況都是獲勝的。

def checker(digit):
 global count, mark, digits
 if digit==1 and digit in digits:
  digits.remove(digit)
  if count%2==0:
mark ='X'
panels[digit]=mark
  elif count%2!=0:
mark = 'O'
panels[digit]=mar
  button1.config(text = mark)
  count = count+1
  sign = mark
  if(win(panels,sign) and sign=='X'):
msg.showinfo("Result","Player1 wins")
root.destroy()
  elif(win(panels,sign) and sign=='O'):
msg.showinfo("Result","Player2 wins")
root.destroy()

最后這里是點(diǎn)擊下棋,然后用win函數(shù)判斷是否結(jié)束比賽贏得棋局。

到此這篇關(guān)于python實(shí)現(xiàn)帶界面的井字棋的小游戲的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)帶界面的井字棋內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部