gpt4 book ai didi

python - 如何用 Python 构建游戏实例数据

转载 作者:行者123 更新时间:2023-12-01 05:14:02 27 4
gpt4 key购买 nike

我正在用 python 实现一个游戏服务器。 GameServer 类包含多个游戏实例,每个实例包含多个玩家。我正在尝试找出最适合使用的数据结构。有一个函数接收所有传入数据,它需要在游戏中查找玩家并更新信息。

目前,GameServer有一组GameInstance,而GameInstance有一组玩家。这需要我迭代每个游戏和玩家以找到正确的游戏,而且我认为这不是最好的方法,因为它必须每秒运行数百次。

传入数据具有连接(从中接收数据)和消息。这意味着我为类(class)中的每个玩家存储一个连接,以便我可以将消息发送回特定玩家。我无法保存每个玩家连接的字典,因为它们必须按游戏实例分组。请帮助我了解构建此结构的最有效方法。

最佳答案

Currently, GameServer has a set of GameInstances, and GameInstance has a set of players. This requires me to iterate through every game and player to find the correct game, and I don't think this is the best way to do it, because it will have to be run hundreds of times per second.

你说得对!虽然我会回答你的具体问题,但你应该做的是阅读有关数据结构的内容。每个工作程序员至少对最常见的数据结构及其性能特征有基本的了解是至关重要的。

根据您对问题的描述,您需要在标识每个游戏的键与描述它的对象之间维护一个映射(可​​能使用哈希表)。

关于python - 如何用 Python 构建游戏实例数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com