在 while 循环中从服务器接收数据显示 window 没有响应
Receiving data from server in while loop Shows window not responding
我正在制作一个初学者在线多人游戏 Tic Tac Toe 游戏,它在本地服务器上运行。我的 code.I 有 2 个问题已将我的游戏文件夹分成 6 个 python 文件:
Client1.py:- 这个文件是输出文件,客户端 have.I 有一些这个文件中的问题。当玩家正在等待其他玩家移动时处于 while 循环中,如果此客户端已播放,它将发送 PLAYED 到服务器,如果服务器正在等待其他玩家移动,它将发送 W 给该客户端,否则它将发送玩家移动 number.While 这个 .py 文件正在等待回复它从服务器获取的消息是 W 然后 window 显示 Not Responding。两个客户端都参考了 Clientsupport.py 和 Network.py。这两个文件仅用于支持客户
import pygame
from Network import network
from clientSupport import GameHelp
n=network()
pt=n.getpos()
g=GameHelp()
g.Display_Start_Up(pt)
if pt==0:
run1=True
run2=False
elif pt==1:
run1 = False
run2 = True
while True:
while run1:
num = None
num=g.Input()
out=n.send(num)
print(out,"hrl")
if out =='Y':
print(num,pt,'hey')
g.Placing(num,pt)
run2=True
break
else:
pass
while run2:
opp=n.send('PLAYED')
print(opp,'loop2')
if opp=='W':
pygame.time.delay(500)
else:
print('here is',opp)
if pt==0:
op=1
elif pt==1:
op=0
g.Placing(opp, op)
print(opp,op,'hello')
run1=True
break
Client2.py:-这个文件是 Client1.py 的精确副本。但这两个文件都是有同样的问题。这是 第二个问题 当两个客户端都连接时,X 和 O 图像仅在加入服务器的第一个客户端上被 blit。对于第二个客户端,它的空白 window 只有板,但输入和其他一切都正常。
Server.py:- 这从另一个文件 Game.py 获取参考。服务器使用此文件来检查谁赢得了什么分数和其他参数。 Pt 服务器中的变量基本上是playertag 发送0 并且1.the 先加入的玩家获得0 并且是X 玩家否则玩家是O 玩家。
import socket
from _thread import *
import sys
import pickle
import game
server = "192.168.1.107"
port = 34262
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server,port))
except socket.error as e:
print(str(e),'hey')
brain=game.Brain()
setup=game.Setup()
s.listen(2)
print("waiting for connection,Server connected")
p=0
setup.p2played=True
lastplayed=[0,0]
def thread_client(conn,playertag):
conn.send(pickle.dumps(playertag))
reply = ""
Run=True
while Run:
try:
try:
data = pickle.loads(conn.recv(2048))
except EOFError as e:
print(e)
pass
if not data:
print('disconnect')
break
elif data=='PLAYED':
if playertag==0:
if lastplayed[1]==0:
reply='W'
else:
reply=lastplayed[1]
lastplayed[1]=0
elif playertag==1:
if lastplayed[0]==0:
reply='W'
else:
reply=lastplayed[0]
lastplayed[0]=0
else:
if playertag==0 and setup.p2played==True:
if brain.Player_move(playertag,data):
reply='Y'
lastplayed[0]=int(data)
setup.p2played = False
setup.p1played = True
else:
reply='N'
if brain.Board_full_check():
reply='T'
Run=False
if brain.Winner_check(1):
reply='X'
Run=False
elif brain.Winner_check(-1):
reply='O'
Run=False
print(f'Recieved: {data}')
print(f'Sending {reply}')
elif playertag==1 and setup.p1played==True:
if brain.Player_move(playertag, data):
reply = 'y'
lastplayed[1] = int(data)
setup.p2played = True
setup.p1played = False
else:
reply = 'N'
if brain.Board_full_check():
reply = 'T'
Run = False
if brain.Winner_check(1):
reply = 'X'
Run = False
elif brain.Winner_check(-1):
reply = 'O'
Run = False
print(f'Recieved: {data}')
print(f'Sending {reply}')
else:
reply='W'
conn.sendall(pickle.dumps(reply))
except socket.error as e:
print(e,'ji')
break
print(f'Disconnected from {addr}')
while True:
print(game.BoardList)
conn, addr= s.accept()
print('connected to ', addr)
start_new_thread(thread_client ,(conn,p,))
p+=1
Clientsupport.py:-所以这个文件提供了所有的客户端支持,比如 pygame 显示和 blitting X和 O.
import pygame
class GameHelp():
def __init__(self):
self.screen=pygame.display.set_mode((600, 600))
xplayer=pygame.image.load('x.png')
self.xplayer = pygame.transform.scale(xplayer, (50, 50))
oplayer = pygame.image.load('o.png')
self.oplayer = pygame.transform.scale(oplayer, (50, 50))
def Display_Start_Up(self,playertag):
caption='Tic Tac Toe '+str(playertag)
pygame.display.set_caption(caption)
self.BOARD(self.screen)
pygame.display.update()
def BOARD(self,sc):
sc.fill((225, 200, 225))
board = pygame.image.load('TransparentImage.png')
board = pygame.transform.scale(board, (375, 375))
sc.blit(board, (100, 200))
def Input(self,):
run=True
while run:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print('1')
run=False
return 1
elif event.key == pygame.K_2:
print('2')
run=False
return 2
elif event.key == pygame.K_3:
print('3')
run=False
return 3
elif event.key == pygame.K_4:
print('4')
run=False
return 4
elif event.key == pygame.K_5:
print('5')
run=False
return 5
elif event.key == pygame.K_6:
print('6')
run=False
return 6
elif event.key == pygame.K_7:
print('7')
run=False
return 7
elif event.key == pygame.K_8:
print('8')
run=False
return 8
elif event.key == pygame.K_9:
print('9')
run=False
return 9
elif event.key == pygame.K_ESCAPE or event.type==pygame.QUIT:
pygame.quit()
def Placing(self,pos,playertag):
if playertag==0:
if pos == 1:
self.screen.blit(self.xplayer, (108, 215))
elif pos == 2:
self.screen.blit(self.xplayer, (249, 222))
elif pos == 3:
self.screen.blit(self.xplayer, (376, 213))
elif pos == 4:
self.screen.blit(self.xplayer, (122, 360))
elif pos == 5:
self.screen.blit(self.xplayer, (253, 346))
elif pos == 6:
self.screen.blit(self.xplayer, (379, 346))
elif pos == 7:
self.screen.blit(self.xplayer, (118, 486))
elif pos == 8:
self.screen.blit(self.xplayer, (261, 492))
elif pos == 9:
self.screen.blit(self.xplayer, (375, 476))
elif playertag==1:
if pos == 1:
self.screen.blit(self.oplayer, (108, 215))
elif pos == 2:
self.screen.blit(self.oplayer, (249, 222))
elif pos == 3:
self.screen.blit(self.oplayer, (376, 213))
elif pos == 4:
self.screen.blit(self.oplayer, (122, 360))
elif pos == 5:
self.screen.blit(self.oplayer, (253, 346))
elif pos == 6:
self.screen.blit(self.oplayer, (379, 346))
elif pos == 7:
self.screen.blit(self.oplayer, (118, 486))
elif pos == 8:
self.screen.blit(self.oplayer, (261, 492))
elif pos == 9:
self.screen.blit(self.oplayer, (375, 476))
pygame.display.update()
Network.py:-关于这个文件没什么好说的,这个文件只连接并向服务器发送消息并工作非常好。
最终调试帮助:- 有 2 个问题:
- 等待对手移动时 Windows 显示无响应
- 总是作为玩家 O 的客户端没有得到图像 blit 认为它接受输入并发送它
到服务器。
A Pygame window 期望它的事件队列总是被处理。典型的 PyGame 程序如下所示:
### Do some initialisation
[...]
### Main Loop
while not exiting:
### handle every event in the queue
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True
elif ( event.type == [ ... ] ):
### Update all the game logic, sprites, network comms, whatever
if ( networkDataArrived( server_socket, server_rx_buffer ) ):
handleNetworkData( server_rx_buffer )
game_sprites.update()
### paint the screen
window.fill( WHITE )
paintGameBoard( window )
game_sprites.draw( window )
### Hopefully do nothing for the rest of the frame-time
clock.tick( FPS )
不正常只是停止超过一秒钟。如果您正在等待套接字数据到达,则需要检查是否有数据到达,如果没有立即继续。如果您正在等待键盘输入,则不能仅调用 input()
... 它 破坏 事件模型。
要处理网络通信,您必须进行非阻塞读取。 一种方法 是使用 select.select()
function。这允许您在套接字缓冲区 "peek" 查看是否有任何内容到达。如果数据已经到达,您可以读取确切的数量而不会阻塞。否则你就回到你的主循环。请注意,还有其他方法可以做到这一点,但是 select()
在所有操作系统(至少对于套接字)上都可用,并且在大多数编程语言中都可用,因此了解它是一个很好的功能。
请考虑 networkDataArrived()
的准~伪代码:
def networkDataArrived( server_socket, socket_buffer ):
""" Read from the socket, stuffing data into the buffer.
Returns True when a full packet has been read into the buffer """
result = False
socks = [ server_socket ]
input,output,excep = select.select( socks, [], [], 0.01 ) # tiny read-timeout
### has any data arrived?
if ( input.count( server_socket ) > 0 ):
socket_buffer.append( server_socket.recv( MAX_PACKET_SIZE ) )
### do we have a full packet?
if ( len( socket_buffer ) >= MAX_PACKET_SIZE ):
result = True
return result
我正在制作一个初学者在线多人游戏 Tic Tac Toe 游戏,它在本地服务器上运行。我的 code.I 有 2 个问题已将我的游戏文件夹分成 6 个 python 文件:
Client1.py:- 这个文件是输出文件,客户端 have.I 有一些这个文件中的问题。当玩家正在等待其他玩家移动时处于 while 循环中,如果此客户端已播放,它将发送 PLAYED 到服务器,如果服务器正在等待其他玩家移动,它将发送 W 给该客户端,否则它将发送玩家移动 number.While 这个 .py 文件正在等待回复它从服务器获取的消息是 W 然后 window 显示 Not Responding。两个客户端都参考了 Clientsupport.py 和 Network.py。这两个文件仅用于支持客户
import pygame
from Network import network
from clientSupport import GameHelp
n=network()
pt=n.getpos()
g=GameHelp()
g.Display_Start_Up(pt)
if pt==0:
run1=True
run2=False
elif pt==1:
run1 = False
run2 = True
while True:
while run1:
num = None
num=g.Input()
out=n.send(num)
print(out,"hrl")
if out =='Y':
print(num,pt,'hey')
g.Placing(num,pt)
run2=True
break
else:
pass
while run2:
opp=n.send('PLAYED')
print(opp,'loop2')
if opp=='W':
pygame.time.delay(500)
else:
print('here is',opp)
if pt==0:
op=1
elif pt==1:
op=0
g.Placing(opp, op)
print(opp,op,'hello')
run1=True
break
Client2.py:-这个文件是 Client1.py 的精确副本。但这两个文件都是有同样的问题。这是 第二个问题 当两个客户端都连接时,X 和 O 图像仅在加入服务器的第一个客户端上被 blit。对于第二个客户端,它的空白 window 只有板,但输入和其他一切都正常。
Server.py:- 这从另一个文件 Game.py 获取参考。服务器使用此文件来检查谁赢得了什么分数和其他参数。 Pt 服务器中的变量基本上是playertag 发送0 并且1.the 先加入的玩家获得0 并且是X 玩家否则玩家是O 玩家。
import socket
from _thread import *
import sys
import pickle
import game
server = "192.168.1.107"
port = 34262
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server,port))
except socket.error as e:
print(str(e),'hey')
brain=game.Brain()
setup=game.Setup()
s.listen(2)
print("waiting for connection,Server connected")
p=0
setup.p2played=True
lastplayed=[0,0]
def thread_client(conn,playertag):
conn.send(pickle.dumps(playertag))
reply = ""
Run=True
while Run:
try:
try:
data = pickle.loads(conn.recv(2048))
except EOFError as e:
print(e)
pass
if not data:
print('disconnect')
break
elif data=='PLAYED':
if playertag==0:
if lastplayed[1]==0:
reply='W'
else:
reply=lastplayed[1]
lastplayed[1]=0
elif playertag==1:
if lastplayed[0]==0:
reply='W'
else:
reply=lastplayed[0]
lastplayed[0]=0
else:
if playertag==0 and setup.p2played==True:
if brain.Player_move(playertag,data):
reply='Y'
lastplayed[0]=int(data)
setup.p2played = False
setup.p1played = True
else:
reply='N'
if brain.Board_full_check():
reply='T'
Run=False
if brain.Winner_check(1):
reply='X'
Run=False
elif brain.Winner_check(-1):
reply='O'
Run=False
print(f'Recieved: {data}')
print(f'Sending {reply}')
elif playertag==1 and setup.p1played==True:
if brain.Player_move(playertag, data):
reply = 'y'
lastplayed[1] = int(data)
setup.p2played = True
setup.p1played = False
else:
reply = 'N'
if brain.Board_full_check():
reply = 'T'
Run = False
if brain.Winner_check(1):
reply = 'X'
Run = False
elif brain.Winner_check(-1):
reply = 'O'
Run = False
print(f'Recieved: {data}')
print(f'Sending {reply}')
else:
reply='W'
conn.sendall(pickle.dumps(reply))
except socket.error as e:
print(e,'ji')
break
print(f'Disconnected from {addr}')
while True:
print(game.BoardList)
conn, addr= s.accept()
print('connected to ', addr)
start_new_thread(thread_client ,(conn,p,))
p+=1
Clientsupport.py:-所以这个文件提供了所有的客户端支持,比如 pygame 显示和 blitting X和 O.
import pygame
class GameHelp():
def __init__(self):
self.screen=pygame.display.set_mode((600, 600))
xplayer=pygame.image.load('x.png')
self.xplayer = pygame.transform.scale(xplayer, (50, 50))
oplayer = pygame.image.load('o.png')
self.oplayer = pygame.transform.scale(oplayer, (50, 50))
def Display_Start_Up(self,playertag):
caption='Tic Tac Toe '+str(playertag)
pygame.display.set_caption(caption)
self.BOARD(self.screen)
pygame.display.update()
def BOARD(self,sc):
sc.fill((225, 200, 225))
board = pygame.image.load('TransparentImage.png')
board = pygame.transform.scale(board, (375, 375))
sc.blit(board, (100, 200))
def Input(self,):
run=True
while run:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
print('1')
run=False
return 1
elif event.key == pygame.K_2:
print('2')
run=False
return 2
elif event.key == pygame.K_3:
print('3')
run=False
return 3
elif event.key == pygame.K_4:
print('4')
run=False
return 4
elif event.key == pygame.K_5:
print('5')
run=False
return 5
elif event.key == pygame.K_6:
print('6')
run=False
return 6
elif event.key == pygame.K_7:
print('7')
run=False
return 7
elif event.key == pygame.K_8:
print('8')
run=False
return 8
elif event.key == pygame.K_9:
print('9')
run=False
return 9
elif event.key == pygame.K_ESCAPE or event.type==pygame.QUIT:
pygame.quit()
def Placing(self,pos,playertag):
if playertag==0:
if pos == 1:
self.screen.blit(self.xplayer, (108, 215))
elif pos == 2:
self.screen.blit(self.xplayer, (249, 222))
elif pos == 3:
self.screen.blit(self.xplayer, (376, 213))
elif pos == 4:
self.screen.blit(self.xplayer, (122, 360))
elif pos == 5:
self.screen.blit(self.xplayer, (253, 346))
elif pos == 6:
self.screen.blit(self.xplayer, (379, 346))
elif pos == 7:
self.screen.blit(self.xplayer, (118, 486))
elif pos == 8:
self.screen.blit(self.xplayer, (261, 492))
elif pos == 9:
self.screen.blit(self.xplayer, (375, 476))
elif playertag==1:
if pos == 1:
self.screen.blit(self.oplayer, (108, 215))
elif pos == 2:
self.screen.blit(self.oplayer, (249, 222))
elif pos == 3:
self.screen.blit(self.oplayer, (376, 213))
elif pos == 4:
self.screen.blit(self.oplayer, (122, 360))
elif pos == 5:
self.screen.blit(self.oplayer, (253, 346))
elif pos == 6:
self.screen.blit(self.oplayer, (379, 346))
elif pos == 7:
self.screen.blit(self.oplayer, (118, 486))
elif pos == 8:
self.screen.blit(self.oplayer, (261, 492))
elif pos == 9:
self.screen.blit(self.oplayer, (375, 476))
pygame.display.update()
Network.py:-关于这个文件没什么好说的,这个文件只连接并向服务器发送消息并工作非常好。
最终调试帮助:- 有 2 个问题:
- 等待对手移动时 Windows 显示无响应
- 总是作为玩家 O 的客户端没有得到图像 blit 认为它接受输入并发送它 到服务器。
A Pygame window 期望它的事件队列总是被处理。典型的 PyGame 程序如下所示:
### Do some initialisation
[...]
### Main Loop
while not exiting:
### handle every event in the queue
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True
elif ( event.type == [ ... ] ):
### Update all the game logic, sprites, network comms, whatever
if ( networkDataArrived( server_socket, server_rx_buffer ) ):
handleNetworkData( server_rx_buffer )
game_sprites.update()
### paint the screen
window.fill( WHITE )
paintGameBoard( window )
game_sprites.draw( window )
### Hopefully do nothing for the rest of the frame-time
clock.tick( FPS )
不正常只是停止超过一秒钟。如果您正在等待套接字数据到达,则需要检查是否有数据到达,如果没有立即继续。如果您正在等待键盘输入,则不能仅调用 input()
... 它 破坏 事件模型。
要处理网络通信,您必须进行非阻塞读取。 一种方法 是使用 select.select()
function。这允许您在套接字缓冲区 "peek" 查看是否有任何内容到达。如果数据已经到达,您可以读取确切的数量而不会阻塞。否则你就回到你的主循环。请注意,还有其他方法可以做到这一点,但是 select()
在所有操作系统(至少对于套接字)上都可用,并且在大多数编程语言中都可用,因此了解它是一个很好的功能。
请考虑 networkDataArrived()
的准~伪代码:
def networkDataArrived( server_socket, socket_buffer ):
""" Read from the socket, stuffing data into the buffer.
Returns True when a full packet has been read into the buffer """
result = False
socks = [ server_socket ]
input,output,excep = select.select( socks, [], [], 0.01 ) # tiny read-timeout
### has any data arrived?
if ( input.count( server_socket ) > 0 ):
socket_buffer.append( server_socket.recv( MAX_PACKET_SIZE ) )
### do we have a full packet?
if ( len( socket_buffer ) >= MAX_PACKET_SIZE ):
result = True
return result