python实现简单的井字棋游戏(gui界面)
作者:data-flair 发布时间:2023-03-06 23:16:01
标签: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 = 0
panels = ["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 msg
root= Tk()
root.title('TIC-TAC-TOE---Project Gurukul')
#labels
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)
digits = [1,2,3,4,5,6,7,8,9]
#for player1 sign = X and for player2 sign= Y
mark = ''
#counting the no. of click
count = 0
panels = ["panel"]*10
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))
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 buttons
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()
来源:https://data-flair.training/blogs/python-tic-tac-toe/
0
投稿
猜你喜欢
- Pycharm默认可以识别py脚本中的SQL语句,本身很不错,但当SQL拼接时就显示的代码特别难看,找了好久,终于知道怎么关闭SQL识别功能
- python 数据的清理行为实例详解数据清洗主要是指填充缺失数据,消除噪声数据等操作,主要还是通过分析“脏数据”产生的原因和存在形式,利用现
- 前几天写了一个ajax的,总感觉代码比较多,今天晚上又得写了一下,感觉代码还是比较多,但还好的是,比较通用。谁有办法优化一下当然好。&nbs
- 浏览器对于CSS的支持问题落后于CSS的发展,以占有市场绝对份额的Internet Explorer来说,直到其前不久发布的第8个版本才刚刚
- 一、切换到Oracle用户su – oracle二、登录到sys用户sqlplus / as sysdba三、创建临时表空间//查询临时表空
- 一、验证计算机服务列表中是否有MySQL服务(1)右击桌面上的计算机图标出现菜单列表选项,选中管理(有多种方式可以打开计算机本地服务列表,这
- 对于相册来说,大图的浏览非常重要,因为对浏览者来说最重要的就是大图看得爽不爽,因为做项目的需要,我比较了许多相册的大图浏览方式,现在一一评说
- 新上的一台服务器出现了一个现象:ASP程序中,调用函数Now(),显示的时间总是形如“2009-07-12 上午 08:12:56”这样的,
- 如何显示SQL数据库所有表的名称?<%strSQLDSN = "xxxx"strSQLUser
- 本文为大家分享了opencv基于Haar人脸检测和眼睛检测的具体代码,供大家参考,具体内容如下在这里,我们将进行人脸检测。最初,该算法需要大
- 本文实例讲述了Python strip()函数的正则表达式实现方法。分享给大家供大家参考,具体如下:题目如下:写一个函数,它接受一个字符串,
- 1.点算子点算子是两个像素灰度值间的映射关系,属于像素的逐点运算,相邻像素不参与运算。点算子是最简单的图像处理手段,如:亮度调整、对比度调整
- 本文针对Python time模块进行分类学习,希望对大家的学习有所帮助。一.壁挂钟时间1.time()time模块的核心函数time(),
- 实际工作中可能会有这样的场景:两个结构体(可能类型一样), 字段名和类型都一样, 想复制一个结构体的全部或者其中某几个字段的值到另一个(即m
- function utf8_substr($str,$len) { for($i=0;$i<$len;$i++) { $temp_st
- 任何东西只要跟Google有关就总能给我们带来点儿惊喜,2008年9月2日发布的Google Chrome浏览器也不例外。Google Ch
- 本文接上一篇博客python实现的八大排序算法part1,将继续使用python实现八大排序算法中的剩余四个:快速排序、堆排序、归并排序、基
- 如果独自放着jQuery做事,它绝对做得很好,但jQuery充许与其他库共存在,有些事就防不胜防了。看下面代码data :func
- 一个常常令设计师和前端开发人员都头疼的事情就是字体的使用,设计师常常在设计稿中使用某些很漂亮的字体,以实现比较酷炫的界面。但这样常常给编码人
- 本篇文章教大家写一个非常简单的Select组件,想必很多人都写过Select,毕竟它太常用了,但是本篇文章的示例使用到了Vue的自定义指令,