手把手教你做一个强化学习的环境,用pygame搭建一个可视化训练游戏环境(一)

可视化训练环境

强化学习基本上用来学习的开源环境比较多,
新手刚入门可以用gym就够了
gym是基于pyglet搭建的,但是做不出比较高大上的效果,而且很多地方不如pygame

pygame

Pygame是一组跨平台的Python模块, 用于创建视频游戏
安装直接conda 里pip install pygame即可

搭建流程(静态部分)

先说一下常用的方法,基本上掌握了就可以组一个完善的小游戏

首先在项目中导入pygame

import pygame
from pygame.locals import *

注:和pyglet有些许不同,这里导入即初始化游戏,刷新画面(render)操作需要套在主循环中并且抓住event,不然会卡住

然后做一些初始化

#注释掉的部分完整版中会放开,这里只介绍基础搭建
class Myenv():
    def __init__(self) -> None:
        #self.gen = Generator()
        #self.player = Player()
        #初始化游戏
        pygame.init()
        #设定游戏屏幕大小,500宽,450高,这个自己定义
        self.screen = pygame.display.set_mode((500, 450))
        #这个是小框框标题,可以不设
        pygame.display.set_caption("Kousei's game")
        #这个是自己导入图片,插到游戏中显示
        self.line = pygame.image.load(r'E:\RLpaz\data\line.png')
        self.agent = pygame.image.load(r'E:\RLpaz\data\agent.png')
        #把图片压缩到指定大小
        self.line = pygame.transform.scale(self.line, (图片宽, 图片高))
        self.agent = pygame.transform.scale(self.agent, (图片宽, 图片高))
        #这个fcclock可以限制游戏刷新率,如果不限制可以不设
        self.fcclock = pygame.time.Clock()
        #设定刷新fps
        self.fcclock.tick(100)
        #self.record = []
        #self.pre_render(400)

接着环境初始化就弄好了,接着我们需要有个刷新功能,每一帧去把图片刷到游戏里

def step(self,auto = False,a=1):
		#done是强化学习用来判断回合是否结束
        #done = False
        #背景填充白色,如果你拿图片当背景可以不用设
        self.screen.fill
  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,我可以给您提供一个使用 Pygame 制作的简单游戏的示例代码,希望对您有所帮助。 游戏简介:玩家需要通过控制一个小球躲避障碍物,随着时间的推移,障碍物的速度会逐渐加快,玩家需要尽可能地躲避更多的障碍物。 首先,您需要安装 Pygame 模块,可以使用以下命令进行安装: ``` pip install pygame ``` 接下来,就可以开始编写游戏代码了。 ```python import pygame import random # 初始化 Pygame pygame.init() # 窗口大小 screen_width = 600 screen_height = 400 # 颜色定义 white = (255, 255, 255) black = (0, 0, 0) # 创建窗口 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("小球躲避游戏") # 设置字体 font = pygame.font.SysFont(None, 25) # 小球类 class Ball(pygame.sprite.Sprite): def __init__(self, color, radius, speed): super().__init__() self.color = color self.radius = radius self.speed = speed self.image = pygame.Surface([radius * 2, radius * 2]) self.image.fill(white) pygame.draw.circle(self.image, color, (radius, radius), radius) self.rect = self.image.get_rect() # 移动小球 def move(self): self.rect.x += self.speed # 障碍物类 class Obstacle(pygame.sprite.Sprite): def __init__(self, color, width, height, speed): super().__init__() self.color = color self.width = width self.height = height self.speed = speed self.image = pygame.Surface([width, height]) self.image.fill(color) self.rect = self.image.get_rect() # 移动障碍物 def move(self): self.rect.x -= self.speed # 创建小球和障碍物组 ball_group = pygame.sprite.Group() obstacle_group = pygame.sprite.Group() # 创建小球 ball = Ball(black, 10, 5) ball.rect.x = 50 ball.rect.y = 200 ball_group.add(ball) # 障碍物数量 num_obstacles = 5 # 创建障碍物 for i in range(num_obstacles): obstacle = Obstacle(black, 50, 50, random.randint(3, 8)) obstacle.rect.x = random.randint(screen_width, screen_width + 1000) obstacle.rect.y = random.randint(0, screen_height - obstacle.height) obstacle_group.add(obstacle) # 游戏循环 game_over = False clock = pygame.time.Clock() score = 0 while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True # 检测小球和障碍物是否碰撞 if pygame.sprite.spritecollide(ball, obstacle_group, False): game_over = True # 移动小球和障碍物 ball.move() obstacle_group.update() # 检测障碍物是否超出屏幕,如果是,则移除障碍物并创建新的障碍物 for obstacle in obstacle_group: if obstacle.rect.right < 0: obstacle_group.remove(obstacle) new_obstacle = Obstacle(black, 50, 50, random.randint(3, 8)) new_obstacle.rect.x = random.randint(screen_width, screen_width + 1000) new_obstacle.rect.y = random.randint(0, screen_height - new_obstacle.height) obstacle_group.add(new_obstacle) score += 1 # 绘制屏幕和得分 screen.fill(white) ball_group.draw(screen) obstacle_group.draw(screen) score_text = font.render("得分:" + str(score), True, black) screen.blit(score_text, [10, 10]) # 更新屏幕 pygame.display.update() # 控制游戏帧率 clock.tick(60) # 退出游戏 pygame.quit() ``` 以上就是一个简单的 Pygame 游戏的示例代码。您可以根据自己的需要对其进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值