首页 > 解决方案 > Pygame - 从外部类生成多个游戏实例

问题描述

目前我正在创建一个用于玩俄罗斯方块的遗传算法,我正在破解的俄罗斯方块应用程序使用 Pygame 作为其主要引擎。

当通过启动单个线程运行生成单个游戏的遗传算法时,它运行良好。一旦我运行该类的副本以创建多个线程,我就会收到分段错误错误。

我将如何解决这个问题?我以前从未使用过 Pygame,但我非常需要制定线程,其中每个线程都有自己的 Pygame 运行。

随时询问更多信息,这是我到目前为止要展示的内容。

get_fitness 方法

def get_fitness(participant, pool, index):
    import tetris
    app = tetris.TetrisApp()
    app.run()
    participant.Fitness = 0
    pool[index] = participant

create_children

def create_children(players, evaluation_length, geneset):
participants = [Chromosome([0 for x in range(evaluation_length)], 0) for i in range(players)]
threads = [None] * players
pool = [None] * players
for index, participant in enumerate(participants):
    genes = participant.Genes
    for gene in range(len(genes)):
        genes[gene] = geneset[random.randrange(0, len(geneset))]
    participant.Genes = genes
    threads[index] = Thread(target=get_fitness, args=(participant, pool, index))
    threads[index].start()
for thread in threads:
    thread.join()
return pool

Python 版本 3.5.2

标签: pythonpygame

解决方案


推荐阅读