4

I'm trying to create a "Draw Something" game for at least three participants from different computers.

I have a code that works for two players. Does anyone know how can I make it for three or more?

...............................................................

Here is my server code:

...............................................................

server = ""
port = 5555

games = {}
idCount = 0
currentPlayer = 0

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((server, port))

s.listen()
print("Waiting for a connection, Server Started")

class Game:
    def __init__(self, id):
        self.connected = False

players = [Player(0), Player(1)]

def threaded_client(conn, player, gameId):
    global idCount
    conn.send(pickle.dumps(players[player]))
    reply = ""
    while True:
        try:
            data = pickle.loads(conn.recv(2048))
            players[player] = data
            if gameId in games:
                game = games[gameId]
                if not data:
                    print("Disconnected")
                    break
                else:
                    if player == 1:
                        reply = players[0]
                    else:
                        reply = players[1]

                conn.sendall(pickle.dumps(reply))
        except:
            break

    print("Lost connection")
    try:
        del games[gameId]
        print("Closing game", gameId)
    except:
        pass
    idCount -= 1
    conn.close()

while True:
    conn, addr = s.accept()
    print("Connected to:", addr)

    idCount += 1
    p = 0

    gameId = (idCount - 1) // 2

    if idCount % 2 == 1:
        games[gameId] = Game(gameId)
        players[0].connected = True
        print("Creating a new game")
        print("Waiting for another player")
    else:
        games[gameId].connected = True
        p = 1
        players[1].connected = True
        print("Game is available")



    start_new_thread(threaded_client, (conn, currentPlayer, gameId))
    currentPlayer += 1

...............................................................

thanks :) :) :) :)

...............................................................

1 Answer 1

0

What you can do is when a a game is running with 2 players and a 3rd player joins show them a Waiting for more player screen then when a 4th player joins run a new thread/instance and make the server handle the new instance

But use a powerful pc to run the server because if a lot of people join the server will crash due to overloading+ your pc where you ran the server can lag

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.