python - 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)
解决方案
推荐阅读
- flutter - Flutter:StatefulWidget 中的范围模型访问
- python - 在 Pytorch 的 Distributions Package 中,Normal 的张量参数应该是什么意思?
- javascript - 如何为图例指定颜色
- javascript - 如何使横幅出现在某个页面/部分?
- python - 在函数内部编写测试
- python - 尝试使用更新函数中的随机值时,matplotlib 中的动画不断崩溃
- html - 对齐导航 html 标记中的文本
- reactjs - Uppy showSelectedFiles 不隐藏文件
- network-protocols - 如何确定自定义通信协议的唯一标头模式?
- task - Snowflake - 使用多个指令(插入、删除、创建视图、创建表等)安排 Script.sql-