首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python游戏-多人游戏

python游戏-多人游戏
EN

Stack Overflow用户
提问于 2020-04-13 13:09:44
回答 1查看 3K关注 0票数 4

我正试图为来自不同计算机的至少三个参与者创建一个“画图”游戏。

我有一个对两个人有效的密码。有人知道我怎么能做到三人或更多人吗?

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

以下是我的服务器代码:

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

代码语言:javascript
复制
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

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

(谢谢:) :)

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

EN

回答 1

Stack Overflow用户

发布于 2021-11-17 04:32:00

您可以做的是,当一个游戏与两个玩家一起运行,而第三个玩家加入时,向他们展示等待更多玩家的屏幕,然后当第四个玩家加入时,运行一个新的线程/实例,并让服务器处理新的实例。

,但使用功能强大的pc来运行服务器,因为如果很多人加入服务器,由于overloading+,您运行的pc会导致服务器崩溃,服务器可能会滞后

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61188623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档