python - 勾选 Python 中的 Python 实例

标签 python

我对使用 python 做一个编程游戏很感兴趣,并且我想以 GunTactyx ( http://apocalyx.sourceforge.net/guntactyx/index.php ) 的风格来做。只是简单得多,因为我主要感兴趣的是从 python 并行执行 python 脚本。

Gun Tactyx 挑战玩家编写一个程序来控制各个单元在团队中协同工作,其中每条指令都带有时间限制。每个程序都在自己的 protected 环境中执行,通过可以与游戏世界交互的函数与游戏世界进行通信。

我想知道是否有一种Python方法可以达到类似的效果。

游戏引擎的伪代码结构如下:

Instantiate units with individual programs
while 1
   Update game world
   for unit in units:
      unit.tick()

模拟将运行直到超时或达到某个目标条件。

亲切的问候

/税

最佳答案

也许你应该研究一下Python的fork:stackless ,它允许同时运行数千个微线程,而不会造成太多性能损失 - 每个“线程”(这些不是真正的操作系统线程)都可以是一个单元。

而且用 stackless 实现 Actor 模型也非常容易:

In the actor model, everything is an actor (duh!). Actors are objects (in the generic sense, not necessarily the OO sense) that can: Receive messages from other actors. Process the received messages as they see fit. Send messages to other actors. Create new Actors.

Actors do not have any direct access to other actors. All communication is accomplished via message passing. This provides a rich model to simulate real-world objects that are loosely-coupled and have limited knowledge of each others internals.

from Introduction to concurrent programming with stackless

或者,您也可以通过实现协同例程来模拟此行为 - 使用 python 生成器,如 here 所示。但我猜你使用 stackless 会更好,因为它已经全部存在了。

关于python - 勾选 Python 中的 Python 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1333016/

相关文章:

python - 删除边界线上方的图像顶部以检测文本文档

python - 树莓派开机时运行Python SimpleHTTPServer

python - Django TypeError get_object_or_404() 至少需要 1 个参数(给定 0 个)

for循环中的Python列表初始化

python appengine unicodeencodeerror on search api snippeted results

python - 是什么阻止我在 web.py 框架中使用 CSS 样式表?

python - 将指数曲线拟合到python中的数值数据

python - 在ubuntu中安装ujson的问题

python - 获取所有可用时区

python - 如何将 re.split 中的分隔符保留在返回列表的同一索引上