首页 > 编程语言 >【汇智学堂】-python系列小游戏(井字游戏之八)

【汇智学堂】-python系列小游戏(井字游戏之八)

时间:2023-04-21 19:01:52浏览次数:45  
标签:messagebox showinfo tkinter python 井字 汇智 else redorgreen position


2.10完成落子各个方向的判断
现在我们要完成同色棋子在不同方向,是否三子连成一线,如果是,提示赢了。结合我们上一节讲的内容,下面是我们要做的事情。
1、判断所有横向,两种颜色棋子是否三子成一线。
2、判断所有竖向,两种颜色棋子是否三子成一线。。
3、判断所有斜向,两种颜色棋子是否三子成一线。。

实现代码如下:

if position[0]==position[1] and position[1]==position[2]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[0]==position[3] and position[3]==position[6]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[0]==position[4] and position[4]==position[8]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[8]==position[7] and position[7]==position[6]:
        if position[8]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[8]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[8]==position[5] and position[5]==position[2]:
        if position[8]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[8]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[6]==position[4] and position[4]==position[2]:
        if position[4]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[4]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[3]==position[4] and position[4]==position[5]:
        if position[3]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[3]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[1]==position[4] and position[4]==position[7]:
        if position[1]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[1]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

总计是8中可能。三横三竖,两斜向。
将代码整合起来,整合后完整代码如下:

#-*- coding:GBK -*-

from tkinter import *
import time

import tkinter.messagebox #messagebox

tk = Tk()
tk.title("雷雷的井字游戏")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=800, height=800, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

redorgreen=0
colorx="green"

position=[0,0,0,0,0,0,0,0,0]

#画棋盘
canvas.create_line(100,100,700,100)
canvas.create_line(100,300,700,300)
canvas.create_line(100,500,700,500)
canvas.create_line(100,700,700,700)

canvas.create_line(100,100,100,700)
canvas.create_line(300,100,300,700)
canvas.create_line(500,100,500,700)
canvas.create_line(700,100,700,700)

def action(event):
    global redorgreen
    global colorx

   #if redorgreen==0 and event.x>100 and event.x<300 and event.y<300 and event.y>100:
    # canvas.create_oval(100, 100, 300, 300, fill =colorx )
    if redorgreen==0:
        redorgreen=1
        colorx="red"
    else :
        redorgreen=0
        colorx="green"  
    #tkinter.messagebox.showinfo('提示',redorgreen)
    
    if event.x>100 and event.x<300 and event.y<300 and event.y>100:
        if position[0]==0:
            canvas.create_oval(100, 100, 300, 300, fill =colorx )
            if colorx=="red":
               position[0]=1
            else:
               position[0]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了!")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
        #winyesorno()
    if event.x>300 and event.x<500 and event.y<300 and event.y>100:
        if position[1]==0:
            canvas.create_oval(300, 100, 500, 300, fill =colorx )
            if colorx=="red":
               position[1]=1
            else:
               position[1]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
    if event.x>500 and event.x<700 and event.y<300 and event.y>100:
        if position[2]==0:
            canvas.create_oval(500, 100, 700, 300, fill =colorx )
            if colorx=="red":
               position[2]=1
            else:
               position[2]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return 
    if event.x>100 and event.x<300 and event.y<500 and event.y>300:
        if position[3]==0:
            canvas.create_oval(100, 300, 300, 500, fill =colorx )
            if colorx=="red":
               position[3]=1
            else:
               position[3]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return 
    if event.x>300 and event.x<500 and event.y<500 and event.y>300:
        if position[4]==0:
            canvas.create_oval(300, 300, 500, 500, fill =colorx )
            if colorx=="red":
               position[4]=1
            else:
               position[4]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return

    if event.x>500 and event.x<700 and event.y<500 and event.y>300:
        if position[5]==0:
            canvas.create_oval(500, 300, 700, 500, fill =colorx )
            if colorx=="red":
               position[5]=1
            else:
               position[5]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
    if event.x>100 and event.x<300 and event.y<700 and event.y>500:
        if position[6]==0:
            canvas.create_oval(100, 500, 300, 700, fill =colorx )
            if colorx=="red":
               position[6]=1
            else:
               position[6]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
    if event.x>300 and event.x<500 and event.y<700 and event.y>500:
        if position[7]==0:
            canvas.create_oval(300, 500, 500, 700, fill =colorx )
            if colorx=="red":
               position[7]=1
            else:
               position[7]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
    if event.x>500 and event.x<700 and event.y<700 and event.y>500:
        if position[8]==0:
            canvas.create_oval(500, 500, 700, 700, fill =colorx )
            if colorx=="red":
               position[8]=1
            else:
               position[8]=2
        else:
            tkinter.messagebox.showinfo('提示',"已有棋子了")
            if redorgreen==1:
               redorgreen=0
            else:
               redorgreen=1
               return
    if position[0]==position[1] and position[1]==position[2]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[0]==position[3] and position[3]==position[6]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[0]==position[4] and position[4]==position[8]:
        if position[0]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[0]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[8]==position[7] and position[7]==position[6]:
        if position[8]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[8]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
    if position[8]==position[5] and position[5]==position[2]:
        if position[8]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[8]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[6]==position[4] and position[4]==position[2]:
        if position[4]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[4]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[3]==position[4] and position[4]==position[5]:
        if position[3]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[3]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")

    if position[1]==position[4] and position[4]==position[7]:
        if position[1]==1:
            tkinter.messagebox.showinfo('提示',"红方获胜")
        if position[1]==2:
            tkinter.messagebox.showinfo('提示',"绿方获胜")
canvas.bind('<Button-1>', action)

while 1:    
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

运行这段代码,我们测试一下各个方向的判断结果(我们以左侧斜上方向为例)。见图2-16

【汇智学堂】-python系列小游戏(井字游戏之八)_python系列小游戏

图2-16


标签:messagebox,showinfo,tkinter,python,井字,汇智,else,redorgreen,position
From: https://blog.51cto.com/u_15545753/6213992

相关文章

  • Python如何建立多层字典
    使用字典的get方法使用字典的底层方法get设置默认值为一个空字典,即可创建下层字典:a={}a["testkey"]=a.get("testkey",{}) 优点是不需要导入其他包;缺点是拓展性太差,几乎只能用在二层字典的创建上,层数越多,代码量越大。 使用collections.defaultdict创建Python自带......
  • 经典的Python小游戏值得收藏
    最近在github上发现用Python开发的多款经典小游戏。这个必须要推荐给大家!可以针对Python2和Python3(到Python的3.7)项目地址:grantjenks/free-python-games​github.com/grantjenks/free-python-games绘画板在屏幕上画线和形状。单击以标记形状的开始,然后再次单击以标记其结束。......
  • python调用imgkit将html转图片pdf问题实例wkhtmltox
    wkhtmltox的下载地址:https://wkhtmltopdf.org/downloads.html或者:https://github.com/wkhtmltopdf/wkhtmltopdf0.12.6版本按网络上的教程会出现一个错误:IOError:wkhtmltopdfexitedwithnon-zerocode1.error:[blank]解决方法参见:https://stackoverflow.com/questions/......
  • Python用哈希算法查找相似图片(包括不同分辨率,不同大小,不同格式的图片)
    #-*-coding:utf-8-*-'''Python用哈希算法查找相似图片并放入[_df]的文件夹中相似图片包括不同分辨率,不同大小,不同格式,只要图片相似就会算重复文件安装cv2pipinstallopencv-python'''importosimportcv2importnumpyasnpimportshutilimportrandomclas......
  • 【汇智学堂】JSTL标签库-循环标签(forTokens)
    <c:forTokens>标签与JAVA语言中StringTokenizer类的作用相似,可以用指定的分隔符分离一个字符串,根据分隔的数量确定循环的次数。<%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c&......
  • 【四二学堂】Python数据可视化-饼形图
    #用饼形图来统计学生成绩等级占比importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltlabel=['A','B','C','D']percent=[25,54,16,5]#explode=[0,0.2,0,0]explode=[0,0,0,0.1]plt.axes(aspect=1)plt.pie(x=percent,la......
  • 【四二学堂】python四子连珠游戏-4(落下棋子后位置记录下来。保证每个棋子能够落在准确
    代码:fromtkinterimport*importtime#画布#棋盘#鼠标左键绑定事件#落下棋子后位置记录下来。保证每个棋子能够落在准确的位置上。classGame:def__init__(self):#self.ball=ballself.clsposition=Clsposition()self.tk=Tk()......
  • 【四二学堂】Python数据可视化-线性图
    importmatplotlib.pyplotaspltimportnumpyasnpplt.rcParams['font.sans-serif']=['MicrosoftYaHei']#识别汉字x=np.linspace(0,2,50)#代表当前网线上有的点的number(0,2)代表x方向的起止位置分为50份plt.plot(x,x,label='北京')#Plotsomedat......
  • 【汇智学堂】JSTL标签库-c标签中的out
    afterrun,likethis:basenolastarticle,out.jsp:<%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%><!DOCTYPEHTMLPU......
  • 【汇智学堂】JSTL标签库-c标签中的remove
    remove.jsp:<%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%><html><head><title><c:remove>......