#from re import A
import tkinter as tk
from tkinter import *
#from zipfile import ZIP_BZIP2
import tkinter.messagebox
#from matplotlib.pyplot import text
# 创建窗口
window =tk.Tk()
# 设置回调函数
window.config(background ="black")
bt=tk.Label(text="五子棋",width=102,height=2)
bt.grid(row=0,column=1)
window.resizable(0,0)
#重新加载界面
#删除棋子函数
def scc():
global n
cv.delete("r")
bqzb.clear()
hqzb.clear()
n=0
#撤销函数
def dele(event):
#删除图形
global dele_coor,n
zbb=(cv.coords('current')[0]+25,cv.coords('current')[1]+25)
if zbb in hqzb:
hqzb.remove(zbb)
n=0
if zbb in bqzb:
bqzb.remove(zbb)
n=1
print(cv.coords('current'))
dele_coor.append(cv.coords('current')) #获取当前active的item的坐标
cv.delete('current') #删除当前active对象
b = Button(window,text='重新开始',command=scc,width=10,height=2)
b.grid(row=0,column=2)
b1 = Label(window,text='游戏规则\n左键放置棋子\n右键撤回棋子\n黑棋先手',height=42)
b1.grid(row=1,column=2)
dele_coor=[]
#定义判定四个方向是否有五连珠
l1=[]
l2=[]
l3=[]
l4=[]
def pdhs(x,l):
num1=0
num2=0
num3=0
num4=0
num5=0
num6=0
num7=0
num8=0
n1=0
n2=0
n3=0
n4=0
#纵向向上
if (x[0],x[1]-50*1) in l :
num1=num1+1
if (x[0],x[1]-50*2) in l:
num1=num1+1
if (x[0],x[1]-50*3) in l :
num1=num1+1
if (x[0],x[1]-50*4) in l :
num1=num1+1
#纵向向下
if (x[0],x[1]+50*1) in l :
num2=num2+1
if (x[0],x[1]+50*2) in l:
num2=num2+1
if (x[0],x[1]+50*3) in l :
num2=num2+1
if (x[0],x[1]+50*4) in l :
num2=num2+1
#横向向左
if (x[0]-50,x[1]) in l :
num3=num3+1
if (x[0]-100,x[1]) in l:
num3=num3+1
if (x[0]-150,x[1]) in l :
num3=num3+1
if (x[0]-200,x[1]) in l :
num3=num3+1
#横向向右
if (x[0]+50,x[1]) in l :
num4=num4+1
if (x[0]+100,x[1]) in l:
num4=num4+1
if (x[0]+150,x[1]) in l :
num4=num4+1
if (x[0]+200,x[1]) in l :
num4=num4+1
#左上
if (x[0]-50,x[1]-50*1) in l :
num5=num5+1
if (x[0]-100,x[1]-50*2) in l:
num5=num5+1
if (x[0]-150,x[1]-50*3) in l :
num5=num5+1
if (x[0]-200,x[1]-50*4) in l :
num5=num5+1
#右下
if (x[0]+50,x[1]+50*1) in l :
num6=num6+1
if (x[0]+100,x[1]+50*2) in l:
num6=num6+1
if (x[0]+150,x[1]+50*3) in l :
num6=num6+1
if (x[0]+200,x[1]+50*4) in l :
num6=num6+1
#右上
if (x[0]+50,x[1]-50*1) in l :
num7=num7+1
if (x[0]+100,x[1]-50*2) in l:
num7=num7+1
if (x[0]+150,x[1]-50*3) in l :
num7=num7+1
if (x[0]+200,x[1]-50*4) in l :
num7=num7+1
#左下
if (x[0]-50,x[1]+50*1) in l :
num8=num8+1
if (x[0]-100,x[1]+50*2) in l:
num8=num8+1
if (x[0]-150,x[1]+50*3) in l :
num8=num8+1
if (x[0]-200,x[1]+50*4) in l :
num8=num8+1
#return print("num1,num2",num1,num2)
#for zb in l:
#横向连珠
#if zb[1]==x[1] and zb[0]!=x[0]:
#return print("num3,num4",num3,num4)
#for zb in l:
#if zb[0]!=x[0] and zb[1]!=x[1] :
#左上到右下连珠
#for i in range(1,5):
#return print("num5,num6",num5,num6)
#左下到右上连珠
#for i in range(1,5):
n1=num1+num2
n2=num3+num4
n3=num5+num6
n4=num7+num8
#return print("num7,num8",num7,num8)
return max(n1,n2,n3,n4)
#return print("n1,n2,n3,n4",n1,n2,n3,n4)
# 设置画布的背景
cv=Canvas(window,bg="#B8860B",width =720, height =720,cursor="hand2")#指针arrow手hand2
# 将控件放置在主窗口中
cv.grid(row=1,column=1)
point1=[(10,10),(10,710),(710,710),(710,10),(10,10)]
line1=cv.create_line(point1,fill="black",width=5)
#鼠标样式
'''
def sbys(event):
xz=event.x
yz=event.y
if (xz-10)%50==0 and (yz-10)%50==0:
cv["cursor"]="hand2"
else:
cv["cursor"]="arrow"
cv.bind('<Motion>',sbys)
'''
#棋盘
#竖向线
for i in range(1,15):
point2=[]
x=10+50*i
y=710
y1=10
point2.append(tuple([x,y1]))
point2.append(tuple([x,y]))
line2=cv.create_line(point2,fill="black",width=5)
#横向线
for i in range(1,15):
point2=[]
x=710
y=10+50*i
x1=10
point2.append(tuple([x1,y]))
point2.append(tuple([x,y]))
line2=cv.create_line(point2,fill="black",width=5)
#中心点
xx=360
yy=360
cv.create_oval(xx-7,yy-7,xx+7,yy+7,fill ="black",outline=None,width=1)
#棋子
n=0
#黑棋坐标列表
hqzb=[]
#白棋坐标列表
bqzb=[]
#所有棋子的坐标
qz=[]
##
def hqxy(event):
global n
xz=event.x
yz=event.y
for q in range(0,15):
xzbz=10+50*q
if 25>=abs(xz-xzbz)>=0:
for w in range(0,15):
yzbz=10+50*w
if 25>=abs(yz-yzbz)>=0:
x1=xzbz+25
y1=yzbz+25
x2=xzbz-25
y2=yzbz-25
#画圆
def hqxy(event):
global n
xz=event.x
yz=event.y
for q in range(0,15):
xzbz=10+50*q
if 25>=abs(xz-xzbz)>=0:
for w in range(0,15):
yzbz=10+50*w
if 25>=abs(yz-yzbz)>=0:
x1=xzbz+25
y1=yzbz+25
x2=xzbz-25
y2=yzbz-25
if n==1:
#避免重复
if (xzbz,yzbz) in hqzb or (xzbz,yzbz) in bqzb:
break
else:
cv.create_oval(x1,y1,x2,y2,fill ="white",outline=None,width=1,tag=("r"))
n=n-1
if pdhs((xzbz,yzbz),bqzb)!=4:
bqzb.append((xzbz,yzbz))
else:
n=0
print("白棋坐标",bqzb)
print("白棋胜利")
tkinter.messagebox.showinfo(title='游戏结束', message='白棋胜利!')
print(pdhs((xzbz,yzbz),bqzb))
elif n==0:
#避免重复
if (xzbz,yzbz) in hqzb or (xzbz,yzbz) in bqzb:
break
else:
cv.create_oval(x1,y1,x2,y2,fill ="black",outline=None,width=1,tag=("r"))
n=n+1
if pdhs((xzbz,yzbz),hqzb) != 4:
hqzb.append((xzbz,yzbz))
else:
n=0
print("黑棋胜利")
tkinter.messagebox.showinfo(title='游戏结束', message='黑棋胜利!')
print("黑棋坐标",hqzb)
print(pdhs((xzbz,yzbz),hqzb))
#鼠标光标变化
cv.bind('<ButtonPress-1>',hqxy)
cv.tag_bind('r','<Button-3>',dele)
#im = tk.PhotoImage(file="P__34QO1R2J8HGW_SSA%6RJ.gif")
#def b2cmd(event):
# c["bg"]="#FAEBD7"
#c=Button(cv,image=im,bg="black")
#c.place(x=35,y=35,height=50,width=50,bordermode="outside")
#c.bind('<ButtonPress-1>',b2cmd)
tk.mainloop()
五子棋
发布于 2023-12-10 109 次阅读
Comments NOTHING