无法访问你试图使用的功能所在的网络位置。是什么原因?
741
2022-05-29
服务器端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import socket
import threading
from tkinterimport *
from tkinter.messageboximport *
def drawQiPan():
for iin range(0,15):
cv.create_line(20,20 + 40 * i,580,20 + 40 * i, width=2)
for iin range(0,15):
cv.create_line(20 + 40 * i,20,20 + 40 * i,580, width=2)
cv.pack()
# 走棋函数
def callPos(event):
global turn
global MyTurn
if MyTurn== -1: # 第一次确认自己的角色
MyTurn= turn
else:
if MyTurn != turn:
showinfo(title="提示", message="还没轮到自己下棋")
return
# print("clicked at",event.x,event.y,true)
x= event.x// 40
y= event.y// 40
print("clicked at", x, y, turn)
if maps[x][y] != " ":
showinfo(title="提示", message="已有棋子")
else:
img1= images[turn]
cv.create_image((x* 40 + 20, y* 40 + 20), image=img1)
cv.pack()
maps[x][y]= str(turn)
pos= str(x)+ "," + str(y)
sendMessage("move|" + pos)
print("服务器走的位置", pos)
label1["text"]= "服务器走的位置" + pos
# 输出输赢信息
if win_lose():
if turn== 0:
showinfo(title="提示", message="黑方你赢了")
sendMessage("over|黑方你赢了")
else:
showinfo(title="提示", message="白方你赢了")
sendMessage("over|白方你赢了")
# 换下一方走棋
if turn== 0:
turn= 1
else:
turn= 0
# 发送消息
def sendMessage(pos):
global s
global addr
s.sendto(pos.encode(), addr)
# 退出函数
def callExit(event):
pos= "exit|"
sendMessage(pos)
os.exit()
# 画对方棋子
def drawOtherChess(x, y):
global turn
img1= images[turn]
cv.create_image((x* 40 + 20, y* 40 + 20), image=img1)
cv.pack()
maps[x][y]= str(turn)
# 换下一方走棋
if turn== 0:
turn= 1
else:
turn= 0
# 判断整个棋盘的输赢
def win_lose():
a= str(turn)
print("a=", a)
for iin range(0,11):
for jin range(0,11):
if maps[i][j]== aand maps[i+ 1][j+ 1]== aand maps[i+ 2][j+ 2]== aand maps[i+ 3][j+ 3]== aand \
maps[i+ 4][j+ 4]== a:
print("x=y轴上形成五子连珠")
return True
for iin range(4,15):
for jin range(0,11):
if maps[i][j]== aand maps[i- 1][j+ 1]== aand maps[i- 2][j+ 2]== aand maps[i- 3][j+ 3]== aand \
maps[i- 4][j+ 4]== a:
print("x=-y轴上形成五子连珠")
return True
for iin range(0,15):
for jin range(4,15):
if maps[i][j]== aand maps[i][j- 1]== aand maps[i][j- 2]== aand maps[i][j- 2]== aand maps[i][
j- 4]== a:
print("Y轴上形成了五子连珠")
return True
for iin range(0,11):
for jin range(0,15):
if maps[i][j]== aand maps[i+ 1][j]== aand maps[i+ 2][j]== aand maps[i+ 3][j]== aand maps[i+ 4][
j]== a:
print("X轴形成五子连珠")
return True
return False
# 输出map地图
def print_map():
for jin range(0,15):
for iin range(0,15):
print(maps[i][j], end=' ')
print('w')
# 接受消息
def receiveMessage():
global s
while True: # 接受客户端发送的消息
global addr
data, addr= s.recvfrom(1024)
data= data.decode('utf-8')
a= data.split("|")
if not data:
print('client has exited!')
break
elif a[0]== 'join': # 连接服务器的请求
print('client 连接服务器!')
label1["text"]= 'client连接服务器成功,请你走棋!'
elif a[0]== 'exit':
print('client对方退出!')
label1["text"]= 'client对方退出,游戏结束!'
elif a[0]== 'over':
print('对方赢信息!')
label1["text"]= data.split("|")[0]
showinfo(title="提示", message=data.split("1")[1])
elif a[0]== 'move':
print('received:', data,'from', addr)
p= a[1].split(",")
x= int(p[0])
y= int(p[1])
print(p[0], p[1])
label1["text"]= "客户端走的位置" + p[0]+ p[1]
drawOtherChess(x, y)
s.close()
def startNewThread(): # 启动新线程来接受客户端消息
thread= threading.Thread(target=receiveMessage, args=())
thread.setDaemon(True)
thread.start()
if __name__== '__main__':
root= Tk()
root.title("网络五子棋v2.0-服务器端")
images= [PhotoImage(file='./images/BlackStone.png'), PhotoImage(file='./images/WhiteStone.png')]
turn= 0
MyTurn= -1
maps= [[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]for yin range(15)]
cv= Canvas(root, bg='green', width=610, height=610)
drawQiPan()
cv.bind("
cv.pack()
label1= Label(root, text="服务器端...")
label1.pack()
button1= Button(root, text="退出游戏")
button1.bind("
button1.pack()
# 创建UDP SOCKET
s= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('localhost',8000))
addr= ('localhost',8000)
startNewThread()
root.mainloop()
Python 网络
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。