#192.168.1.124
import pygame
import sys
import socket
import tkinter
player=0
def row(my,x,y):
j=-1
for i in range(5):
if (x-i,y) in my:
j=j+1
else:
break
for i in range(5):
if (x+i,y) in my:
j=j+1
else:
break
print(j)
return j
def col(my,x,y):
j=-1
for i in range(5):
if (x,y-i) in my:
j=j+1
else:
break
for i in range(5):
if (x,y+i) in my:
j=j+1
else:
break
print(j)
return j
def right(my,x,y):
j=-1
for i in range(5):
if (x-i,y-i) in my:
j=j+1
else:
break
for i in range(5):
if (x+i,y+i) in my:
j=j+1
else:
break
print("right",j)
return j
def left(my,x,y):
j=-1
for i in range(5):
if (x+i,y-i) in my:
j=j+1
else:
break
for i in range(5):
if (x-i,y+i) in my:
j=j+1
else:
break
print("left",j)
return j
def win(my,x,y):
if row(my,x,y)==5 or col(my,x,y)==5 or right(my,x,y)==5 or left(my,x,y)==5:
global a
a=True
return True
else:
return False
a=False
my=set()
he=set()
pygame.init()
root = pygame.display.set_mode((500,500))
pygame.display.set_caption("五子棋")
bg = pygame.image.load("bg.png")
root.blit(bg,(0,0))
for i in range(100,420,20):
pygame.draw.line(root,(0,0,0),(i,100),(i,400),width=1)
pygame.draw.line(root, (0, 0, 0), (100,i), (400, i), width=1)
bt1 = pygame.image.load("./bt120.png")
bt2 = pygame.image.load("./bt220.png")
root.blit(bt1,(110,50))
root.blit(bt2,(260,50))
#pygame.draw.rect(root,(255,0,0),(10,110,60,30))
#pygame.draw.rect(root,(0,255,0),(10,160,60,30))
pygame.display.update()
connect=False
msg2=[0,0]
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def cli(ip):
s.connect((ip, 12345))
global connect
global player
connect = True
player=1
global msg2
# msg2=s.recv(1024).decode().split(",")
# print(msg2,"hhhhhhhh")
# print(str[0], str[1])
# data = input()
# s.send(data.encode('utf-8'))
return s
def main():
s.bind(("",12345))
s.listen(1)
nc,np= s.accept()
global connect
connect=True
player=0
if connect:
pygame.display.set_caption("主机状态联机成功")
return nc
def close():
global ip
ip = a1.get()
tkroot.destroy()
while 1:
#修复白子不更新问题开头加刷新
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if a==False and player==0 and event.type==pygame.MOUSEBUTTONUP:
x,y=pygame.mouse.get_pos()
if 110 < x < 230 and 50 < y < 100:
print(x,y)
pygame.display.set_caption("等待副机连接")
nc=main()
if 260 < x < 430 and 50 < y < 90:
tkroot = tkinter.Tk()
ip="192.168.1.124"
tkroot.geometry("300x50")
a1 = tkinter.Entry(tkroot, width=90)
a1.pack()
a2 = tkinter.Button(tkroot,width=60,text="确认",command=close)
a2.pack()
tkroot.mainloop()
print(ip)
nc=cli(ip)
player=1
pygame.display.set_caption("主机连接成功")
if 100<x<420 and 100<y<420:
x = (x + 10) // 20 - 5
y = (y + 10) // 20 - 5
pygame.draw.circle(root,(0,0,0),((x+5)*20,(y+5)*20),8,width=0)
pygame.display.update()
print(x,y)
my.add((x,y))
a=win(my,x, y)
msg = (str(x) + ',' + str(y)).encode()
nc.send(msg)
print(a)
if a:
f = pygame.font.Font(None, 100)
ff = f.render("WIN", True, (0, 255, 255)) # surface
root.blit(ff, (180, 200))
pygame.display.update()
else:
pass
player = (player + 1) % 2
if connect and player==1 and a==False:
# if (getattr(nc, '_closed') == True):
# print("hhhhhhhhhhhhhhhhhhhhhhhhhhh")
# msg=(str(x)+','+str(y)).encode()
# nc.send(msg)
print(player,"------------------")
msg2=nc.recv(1024).decode().split(",")
x=int(msg2[0])
y=int(msg2[1])
pygame.draw.circle(root, (255, 255, 255), ((x + 5) * 20, (y + 5) * 20), 8, width=0)
print(msg2[0],msg2[1],"kkkkk")
if msg2!=[0,0]:
he.add((int(msg2[0]),int(msg2[1])))
b=win(he,int(msg2[0]),int(msg2[1]))
if b:
f = pygame.font.Font(None, 100)
ff = f.render("LOOSE", True, (0, 255, 255)) # surface
root.blit(ff, (180, 200))
pygame.display.update()
print('okkkkkk')
player=(player+1)%2
msg2=[0,0]
pygame.display.update()