mardi 26 novembre 2019

Why do these variables get reset with socket module?

I am making a battleships game with local multiplayer. I am having a problem with the server where I need to wait for both ship placements to come in. On the code example, the player_1 and player_2 variables are reset to None during loading data. The variables are the same before receiving data but they get changed to None after? How does this happen? Thanks. Please note that this code is running on multithreading for each client.

    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

Aucun commentaire:

Enregistrer un commentaire