import tkinter as tk
from tkinter import *
import tkinter.messagebox
import socket, _thread
import json
import winsound
width = 800
height = 900
matrix = [[0 for i in range(19)]for j in range(19)]
Bool_ = True
adress = ('139.199.61.165', 8712)
match_num = None
color = None
sendID = None
def set_color_white():
"""
设置当前颜色为白棋
"""
global color
window.button_color["fg"] = "black"
window.button_color["bg"] = "white"
window.button_color["text"] = "白棋"
window.button_color.place(x=610, y=40)
color = 1
def set_color_black():
"""
说着当前颜色为黑棋
"""
global color
window.button_color["fg"] = "white"
window.button_color["bg"] = "black"
window.button_color["text"] = "黑棋"
window.button_color.place(x=610, y=40)
color = 2
def send_point(point, color_code):
"""
向服务器发送坐标
"""
global sendID, match_num, MyID
print(match_num)
send_dict = {"match_num": match_num, "point": point, "sendID": sendID, "color_code": color_code,"MyID":MyID}
s.sendall(json.dumps(send_dict).encode("utf8"))
def send_isShowconnect():
"""
向服务发送获取闲置连接请求
"""
global MyID, match_num
s.sendall(json.dumps({"isShowConnect": True, "MyId": MyID, "match_num": match_num, "ID": None}).encode("utf8"))
def start_match():
"""
开始配对
"""
global color, MyID,sendID
if MyID == sendID:
return 0
send_dict = {"ID": sendID, "MyID": MyID, "point": [0, 0], "color_code": color, "match_num": None}
s.sendall(json.dumps(send_dict).encode("utf8"))
# match_num = json.loads(s.recv(1024).decode("utf8"))
def server_init():
"""
初始化连接
"""
global s, MyID
s = socket.socket() # 创建 socket 对象
s.connect(adress)
MyID = json.loads(s.recv(1024).decode(encoding='utf8'))["adress"]
print(MyID)
def receive_info():
"""
消息接收处理
"""
global matrix, table, Bool_, match_num, c, sendID, color
while True:
dict_str = s.recv(10240).decode("utf8")
print(dict_str)
receive_dict = json.loads(dict_str)
print(receive_dict)
# if "heart" in receive_dict:
# s.sendall(json.dumps({"heart": True}).encode("utf8"))
if "restart" in receive_dict:
print("收到restart %s" % receive_dict)
_init()
if receive_dict["color"] == 1:
set_color_white()
else:
set_color_black()
Bool_ = True
window.butoon_restart["state"] = DISABLED
elif "drop_successful" in receive_dict:
tkinter.messagebox.showinfo(message="已经断开连接")
match_num = None
Bool_ = True
_init()
window.dis_button.destroy()
window.butoon_restart["state"] = DISABLED
elif receive_dict["ConnectPool"]is not None:
create_find_connect(receive_dict)
elif "successful_connect" in receive_dict:
if "sendID" in receive_dict:
sendID = receive_dict["sendID"]
match_num = receive_dict["match_num"]
tkinter.messagebox.showinfo(title="successful", message="连接成功!")
if receive_dict["color"] == 1:
set_color_white()
Bool_ = False
else:
set_color_black()
Bool_ = True
window.butoon_restart["state"] = DISABLED
window.disconnect()
elif receive_dict["point"] is not None:
Bool_ = True
point = receive_dict["point"]
if receive_dict["color_code"] == 1:
table.create_white(point[0], point[1])
elif receive_dict["color_code"] == 2:
table.create_black(point[0], point[1])
if receive_dict["isFalse"] is True:
tkinter.messagebox.showinfo(title="game over", message="游戏结束")
window.butoon_restart["state"] = NORMAL
Bool_ = False
def _init():
"""
GUI界面初始化
"""
global table, matrix
if table is not None:
del table
matrix = [[0 for i in range(19 )]for j in range(19)]
table = GameTable(window.root)
table.table.bind("<Button-1>", table.point)
def restart():
"""
向服务器发送
重新开始请求
"""
global MyID
s.sendall(json.dumps({"restart": True, "sendID": sendID, "match_num": match_num, "MyID": MyID}).encode("utf8"))
print("发送restart")
def create_find_connect(dict_):
"""创建连接选择窗口"""
c = ConnectWindows(dict_["ConnectPool"])
c.choice()
class GameWindow:
"""
游戏主体窗口
"""
def __init__(self):
self.root = tk.Tk()
self.root.title("五子棋(%s)" % MyID)
self.root.iconbitmap(r"F:\test\gobang.ico")
self.root.resizable(width=False, height=False)
self.root.geometry("%sx%s+600+100" % (width, height))
def create_frame(self):
self.frame = tk.Frame(self.root, width=width, height=100, bg='green')
self.butoon_start = tk.Button(self.frame, bg="blue", text="开始游戏", font=("微软雅黑", 14))
self.butoon_start.place(x=10, y=40)
self.butoon_restart = tk.Button(self.frame, bg="blue", text="重新开始", font=("微软雅黑", 14), command=restart, state=DISABLED)
self.butoon_restart.place(x=120, y=40)
self.butoon_stop = tk.Button(self.frame, bg="blue", text="悔棋", font=("微软雅黑", 14))
self.butoon_stop.place(x=230, y=40)
self.button_color = tk.Button(self.frame, fg="black", bg="white", text=" ", font=("微软雅黑", 14),state=DISABLED)
button_showpool = tk.Button(self.frame, fg="black", bg="white", text="显示连接池", font=("微软雅黑", 14),
command=send_isShowconnect)
button_showpool.place(x=680, y=40)
self.frame.place(x=0, y=0)
def disconnect(self):
self.dis_button = tk.Button(self.frame, fg="black", bg="white", text="断开连接", font=("微软雅黑", 14),command=self.dis_buttonclean)
self.dis_button.place(x=300,y=40)
def dis_buttonclean(self):
global s, match_num
s.sendall(json.dumps({"match_num": match_num, "disconnect": True}).encode("utf8"))
class GameTable:
"""
游戏桌区域
"""
def __init__(self, frame):
self.table = tk.Canvas(frame, width=width, height=height, bd=0, highlightthickness=0)
self.table.place(x=0, y=100)
self.table.create_rectangle(0, 0, width, height - 100, fill="#FFB90F")
self.table.create_line(20, 20, 780, 20)
self.table.create_line(20, 20, 20, 780)
self.table.create_line(780, 780, 20, 780)
self.table.create_line(780, 780, 780, 20)
for i in range(20):
self.table.create_line(40, i * 40, 760, i * 40)
self.table.create_line(i * 40, 40, i * 40, 760)
self.table.create_oval(10 * 40 - 5, 10 * 40 - 5, 10 * 40 + 5, 10 * 40 + 5, fill="black")
self.table.create_oval(4 * 40 - 5, 4 * 40 - 5, 4 * 40 + 5, 4 * 40 + 5, fill="black")
self.table.create_oval(16 * 40 - 5, 16 * 40 - 5, 16 * 40 + 5, 16 * 40 + 5, fill="black")
self.table.create_oval(16 * 40 - 5, 4 * 40 - 5, 16 * 40 + 5, 4 * 40 + 5, fill="black")
self.table.create_oval(4 * 40 - 5, 16 * 40 - 5, 4 * 40 + 5, 16 * 40 + 5, fill="black")
def create_black(self, x, y):
matrix[y][x] = 2
x += 1
y += 1
self.table.create_oval(x * 40 - 15, y * 40 - 15, x * 40 + 15, y * 40 + 15, fill="black")
# _thread.start_
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python联机五子棋(包含客户端和服务端程序).zip (2个子文件)
python联机五子棋(包含客户端和服务端程序)
五子棋_联机_客户端.py 12KB
五子棋_联机_服务端.py 7KB
共 2 条
- 1
资源评论
尼克不可
- 粉丝: 81
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功