Основная идея проста: я запускаю серверный код, а затем клиентский код. чтобы он мог подключиться к серверу (если я хочу играть с несколькими клиентами, я просто создаю еще один файл py с тем же кодом, что и другой клиент, и запускаю его).
Это основной функция сервера:
Код: Выделить всё
def handle_client(conn, addr):
global cells, players # Declare cells as a global variable
print(f"[CONNECTION] Connected to {addr}")
# Assign a player ID based on connection order
player_id = uuid.uuid4()
players[player_id] = Player(player_id, 0, 0)
# Send player ID to client
conn.sendall(pickle.dumps(player_id))
# Send cells data to client
conn.sendall(pickle.dumps(cells))
while True:
try:
# Receive player position from client
player_data = conn.recv(4096)
player = pickle.loads(player_data)
# Update player position in the list
players[player_id] = player
conn.sendall(pickle.dumps(players))
cell_data = conn.recv(4096)
cells = pickle.loads(cell_data)
print(cells)
Подробнее здесь: [url]https://stackoverflow.com/questions/78384814/weird-behavior-in-my-python-server-client-code-with-the-game-mechanics-working-o[/url]