python - 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
解决方案
推荐阅读
- amazon-web-services - 跨账户资源访问策略规则是否有效(非承担角色)
- sql - 在 Oracle 中将数字显示为日期
- java - MapStruct 为实现 Iterable 的对象生成文件
界面 - ios - 使用 Swift 创建 Gameboy 游戏
- reactjs - Twilio 可编程 SMS npm 包不从 Safari 发送 SMS
- node.js - 创建对象时调用猫鼬模式方法
- azure - 用于在现有虚拟网络中创建 VM 的 ARM 模板
- graphql - 从 graphql-codegen 获取类似于 apollo codegen 生成的类型
- java - JAVA GAE:进程终止,因为机器被强制关闭
- python - 网络抓取价格和手机名称?