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

2.9判断同一颜色的棋子三子成线
现在我们要在棋盘上同一方向上完成同一颜色三子成一线的判断。我们从一个方向开始。下面是我们要做的事情。
1、清楚三子成一线的本质,是位置状态相同。
2、位置状态相同,判断后提示:赢了

实现代码如下:

if event.x>100 and event.x<300 and event.y<500 and event.y>300:
canvas.create_oval(100, 300, 300, 500, fill =colorx )
if event.x>300 and event.x<500 and event.y<500 and event.y>300:
canvas.create_oval(300, 300, 500, 500, fill =colorx )
if event.x>500 and event.x<700 and event.y<500 and event.y>300:
canvas.create_oval(500, 300, 700, 500, fill =colorx )
if event.x>100 and event.x<300 and event.y<700 and event.y>500:
canvas.create_oval(100, 500, 300, 700, fill =colorx )
if event.x>300 and event.x<500 and event.y<700 and event.y>500:
canvas.create_oval(300, 500, 500, 700, fill =colorx )
if event.x>500 and event.x<700 and event.y<700 and event.y>500:
canvas.create_oval(500, 500, 700, 700, fill =colorx )

if position[0]==position[1] and position[1]==position[2] and position[0]==1:
tkinter.messagebox.showinfo(‘提示’,“赢了”)

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

结合上面这段代码,理解position[0]=0、position[0]=1、position[0]=2三者的含义。position[0]=0代表当前位置无棋子,position[0]=1代表当前位置有红色棋子,position[0]=2代表当前位置有绿色棋子

将代码整合起来,整合后完整代码如下:

#-- 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 yesorno():
tkinter.messagebox.showinfo(‘提示’,“提示1”)
if redorgreen==1:
redorgreen=0
else:
redorgreen=1
return

def action(event):
global redorgreen
global colorx

#if redorgreen0 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:
    canvas.create_oval(100, 300, 300, 500, fill =colorx )
if event.x>300 and event.x<500 and event.y<500 and event.y>300:
    canvas.create_oval(300, 300, 500, 500, fill =colorx )
if event.x>500 and event.x<700 and event.y<500 and event.y>300:
    canvas.create_oval(500, 300, 700, 500, fill =colorx )

if event.x>100 and event.x<300 and event.y<700 and event.y>500:
    canvas.create_oval(100, 500, 300, 700, fill =colorx )
if event.x>300 and event.x<500 and event.y<700 and event.y>500:
    canvas.create_oval(300, 500, 500, 700, fill =colorx )
if event.x>500 and event.x<700 and event.y<700 and event.y>500:
    canvas.create_oval(500, 500, 700, 700, fill =colorx )

if position[0]==position[1] and position[1]==position[2] and position[0]==1:
    tkinter.messagebox.showinfo('提示',"赢了")

canvas.bind(’’, action)

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

运行这段代码,3*3方格的第一行,如果都落下红色棋子时,就会提示:赢了。见图2-15
在这里插入图片描述
图2-15

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/88345508