首页 > 解决方案 > Pygame 循环仅渲染最后一帧/移动

问题描述

我正在参考使用 Tensorflow 和 pygame 的蛇类 AI 游戏的教程。

参考:https ://github.com/tolotrasam/snakegame_ai_tensorflow/tree/master/dist

对于“一些随机游戏优先”和“评估”方法,我想显示整个游戏,而不仅仅是最后一帧。例如:在 player.py 代码中,“一些随机游戏优先”方法只显示最后一步帧。我想渲染每个游戏的每一帧。这将如何完成?

另外,我将如何使用此代码来显示“训练过的”模型,例如生成的 saved.py 文件中的保存数据?

任何帮助,将不胜感激。

参考代码:

LR = 1e-3
goal_steps = 300
score_requirement = 50
initial_games = 5000

def some_random_games_first():
    # Each of these is its own game.
    for episode in range(10):

        env = game()
        env.reset()
        first = True
        for _ in range(goal_steps):

            # action = random.randrange(0, 3)
            action = random.randrange(0, 3)
            # action = 2

            if first:
                first = False
                action = 2

            # do it! render the previous view
            env.render()
            observation, reward, done, info = env.step(action)
            a = 0
            if done: break

蛇游戏可以在 GitHub 链接中找到,不要粘贴整个代码。

渲染方法:


def render(self):
        # score
        d = self.screen.blit(self.bg, SCORE_POS, pygame.Rect(SCORE_POS, (50, 100)))
        f = pygame.font.Font(None, 12)
        scoreimage = f.render(SCORE_PREFIX + str(self.currentscore), True, SCORE_COLOR)
        d2 = self.screen.blit(scoreimage, SCORE_POS)

        # drawing
        dirty = self.all.draw(self.screen)
        dirty.append(d)
        dirty.append(d2)

        # updating screen
        pygame.display.update(dirty)

        # waiting
        self.clock.tick(FPS)

标签: pythonpython-3.xtensorflowpygametflearn

解决方案


推荐阅读