为什么使用套接字模块重置这些变量?

时间:2019-11-26 07:36:41

标签: python if-statement while-loop

我正在与本地多人游戏进行战舰游戏。我的服务器出现问题,我需要等待两个船位都进入。在代码示例中,在加载数据期间将player_1和player_2变量重置为None。变量在接收数据之前是相同的,但是在之后它们变为无?这是怎么发生的?谢谢。请注意,此代码在每个客户端的多线程上运行。

    placed = False
    player_1 = None
    player_2 = None

    while not placed:
        place_data = pickle.loads(conn.recv(2048)) # receive data in format: [[placement data],player]
        if place_data[1] == 1: # if it is player 1, save its position data to player_1
            player_1 = place_data[0]
            print('Player 1 positions received: ', player_1)
        if place_data[1] == 2: # if it is player 2, save its position data to player_2
            player_2 = place_data[0]
            print('Player 2 positions received: ', player_2)

        if player_1 != None and player_2 != None: # if player_1 and player_2 have data send 'placed' to client
            print('sending')
            conn.send(pickle.dumps('placed'))
            placed = True

1 个答案:

答案 0 :(得分:0)

这不是在任何计算机上运行它的最低代码,但是您可以通过打印值进行检查,因此您注意到是否正在获取数据。这样检查,

getUsers