电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

python實現簡單的井字棋游戲(gui界面)

瀏覽:7日期:2022-06-29 13:08:56
項目輸出

python實現簡單的井字棋游戲(gui界面)

項目先決條件

要使用python構建井字游戲,我們需要tkinter模塊和python的基本概念

Tkinter模塊是用于渲染圖形的標準圖形用戶界面。

Tkinter.messagebox用于顯示消息框

要安裝tkinter模塊,我們在命令提示符下使用了pip install命令:

pip install tkinter項目文件結構

這些是使用python構建井字游戲的步驟:

導入模塊 初始化窗口 檢查結果的功能 檢查獲勝者的功能 定義標簽和按鈕1.導入模塊

from tkinter import *import tkinter.messagebox as msg

在此步驟中,我們導入tkinter和messsagebox模塊

2.初始化窗口

root= Tk()root.title(’TIC-TAC-TOE---DataFlair’)digits = [1,2,3,4,5,6,7,8,9]mark = ’’ “count = 0panels = ['panel']*10 Tk()用于初始化窗口 title()用于設置窗口的標題3.檢查結果的功能

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() if digit==2 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button2.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() if digit==3 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button3.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() if digit==4 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button4.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() if digit==5 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button5.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() if digit==6 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button6.config(text = mark) count = count+1 sign 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() if digit==7 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button7.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() if digit==8 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button8.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() if digit==9 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button9.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() if(count>8 and win(panels,’X’)==False and win(panels,’O’)==False): msg.showinfo('Result','Match Tied') root.destroy()

玩家總共有9次點擊以玩游戲。玩家每次單擊時,如果count的值大于8,則通過將count的值增加1來減少機會,則游戲結果為平局

如果count的值為偶數,則玩家1將玩,否則玩家2將玩。 config()用于用適當的文本標記按鈕 messagebox小部件中的showinfo()方法用于顯示一些相關信息 destroy()停止mainloop退出程序 5.定義標簽和按鈕

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()

Label()小部件,用于顯示用戶無法修改的文本。Button()小部件顯示按鈕

root是我們引用的窗口的名稱 文本存儲我們在標簽上顯示的值 文字所使用的字體 單擊按鈕時將調用命令 lambda()函數用于將特定數據發送到回調函數。

要運行程序時,將執行mainloop()方法。

完整代碼

from tkinter import *import tkinter.messagebox as msgroot= Tk()root.title(’TIC-TAC-TOE---Project Gurukul’)#labelsLabel(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)digits = [1,2,3,4,5,6,7,8,9]#for player1 sign = X and for player2 sign= Ymark = ’’#counting the no. of clickcount = 0panels = ['panel']*10def 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))def checker(digit): global count, mark, digits #Check which button clicked if digit==1 and digit in digits: digits.remove(digit)##player1 will play if the value of count is even and for odd player2 will play if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark 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() if digit==2 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button2.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() if digit==3 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button3.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() if digit==4 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button4.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() if digit==5 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button5.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() if digit==6 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button6.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() if digit==7 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button7.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() if digit==8 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button8.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() if digit==9 and digit in digits: digits.remove(digit) if count%2==0: mark =’X’ panels[digit]=mark elif count%2!=0: mark = ’O’ panels[digit]=mark button9.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() ###if count is greater then 8 then the match has been tied if(count>8 and win(panels,’X’)==False and win(panels,’O’)==False): msg.showinfo('Result','Match Tied') root.destroy() ####define buttonsbutton1=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()

以上就是python實現簡單的井字棋游戲的詳細內容,更多關于python 井字棋游戲的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 | 通用磨耗试验机-QUV耐候试验机|久宏实业百科 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 权威废金属|废塑料|废纸|废铜|废钢价格|再生资源回收行情报价中心-中废网 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 制氮设备_PSA制氮机_激光切割制氮机_氮气机生产厂家-苏州西斯气体设备有限公司 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 多物理场仿真软件_电磁仿真软件_EDA多物理场仿真软件 - 裕兴木兰 | 电线电缆厂家|沈阳电缆厂|电线厂|沈阳英联塑力线缆有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 手机游戏_热门软件app下载_好玩的安卓游戏下载基地-吾爱下载站 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 | 高压负荷开关-苏州雷尔沃电器有限公司| 高压直流电源_特种变压器_变压器铁芯-希恩变压器定制厂家 | Q361F全焊接球阀,200X减压稳压阀,ZJHP气动单座调节阀-上海戎钛 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 房在线-免费房产管理系统软件-二手房中介房屋房源管理系统软件 | 水冷式工业冷水机组_风冷式工业冷水机_水冷螺杆冷冻机组-深圳市普威机械设备有限公司 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 代理记账_公司起名核名_公司注册_工商注册-睿婕实业有限公司 | 甲级防雷检测仪-乙级防雷检测仪厂家-上海胜绪电气有限公司 | 耐酸泵,耐腐蚀真空泵,耐酸真空泵-淄博华舜耐腐蚀真空泵有限公司 精密模具-双色注塑模具加工-深圳铭洋宇通 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | 环氧树脂地坪漆_济宁市新天地漆业有限公司 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 影视模板素材_原创专业影视实拍视频素材-8k像素素材网 | 暖气片十大品牌厂家_铜铝复合暖气片厂家_暖气片什么牌子好_欣鑫达散热器 | 骨密度仪-骨密度测定仪-超声骨密度仪-骨龄测定仪-天津开发区圣鸿医疗器械有限公司 | 保温杯,儿童婴童奶瓶,运动水壶「广告礼品杯定制厂家」超朗保温杯壶 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 立刷【微电签pos机】-嘉联支付立刷运营中心 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 |