gpt4 book ai didi

python - 在while循环中从服务器接收数据显示窗口没有响应

转载 作者:行者123 更新时间:2023-12-03 12:02:36 25 4
gpt4 key购买 nike

我正在做一个初学者在线多人游戏井字游戏 在本地服务器上运行的游戏。我的代码有 2 个问题。我已将游戏文件夹分成 6 个 python 文件:

Client1.py :- 此文件是客户端将拥有的输出文件。我在此文件中遇到了一些问题。当玩家正在等待其他玩家移动时,如果该客户端已经播放,它将发送 PLAYED 到服务器,如果服务器正在等待其他玩家移动,它将向该客户端发送 W 否则它将发送玩家移动号码。当这个 .py 文件等待回复时,它从服务器获得的消息是 W,然后窗口显示 没有回应 .两个客户端都引用了 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。对于第二个客户,它的空白窗口只有板,但输入和其他一切工作正常。

服务器.py :- 这从另一个文件 Game.py 中获取引用。服务器使用此文件来检查谁赢得了分数和其他参数。服务器中的 Pt 变量基本上是 playertag 发送 0 和 1。首先加入的玩家获得 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

客户端支持.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()

网络.py :-关于这个文件没什么好说的,这个文件只连接并将消息发送到服务器并且工作得很好。

最终调试帮助 :- 有两个问题:
  • 当等待对手移动时,Windows 显示 Not Responding
  • 总是作为玩家 O 的客户端没有得到图像 blit 认为它需要输入并发送它
    到服务器。
  • 最佳答案

    Pygame 窗口期望它的事件队列总是被处理。一个典型的 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 .这使您可以“窥视”套接字缓冲区以查看是否有任何东西到达。如果数据已经到达,您可以在不阻塞的情况下读取确切的持有量。否则你只是回到你的主循环。请注意,还有其他方法可以做到这一点,但 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

    关于python - 在while循环中从服务器接收数据显示窗口没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62405329/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com