Pygame 和 Open AI 实现 [英] Pygame and Open AI implementation

查看:80
本文介绍了Pygame 和 Open AI 实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的同学决定尝试将 AI 代理实现到我们自己的游戏中.我的朋友根据以前的项目完成了大部分代码,我想知道 PyGame 和 OpenAI 将如何协同工作.曾尝试进行一些研究,但无法真正找到有关此特定主题的任何有用信息.有人说这很难实施,但有人说它有效.无论哪种方式,我都希望您对这个项目提出意见,以及如果是您,您会如何处理.

Me and my classmate have decided to try and implement and AI agent into our own game. My friend have done most of the code, based on previous projects, and I was wondering how PyGame and OpenAI would work together. Have tried to do some research but can't really find any useful information about this specific topic. Some have said that it is hard to implement but some say it works. Either way, I'd like your opinion on this project and how you'd approach this if it was you.

游戏非常基础(只有一个输入)和线性难度.基本上,您只是尝试躲避随着角色(您)以屏幕为中心而产生的绿色方块.绿色方块之间的空间相同,这意味着生成率完全相同.这是代码,但它没有很好地清理或抛光,但我们的第一个示例:

The game is very basic (only has one input) and a linear difficulty. Basically you just try to dodge the green squares that spawn with the character (you) being centered to the screen. The space between the green squares are the same which means the spawn rate is exactly the same. Here's the code but it's not very well cleaned nor polished but our first example:

    import pygame
    import time

    start_time = time.time()
    pygame.init()

    win_dim = 800
    win = pygame.display.set_mode((win_dim, win_dim))


    class Char:

       def __init__(self, x, y, dim, score):

           self.x = x
           self.y = y

           self.dim = dim
           self.score = score

           self.vsp = 0

       def draw_char(self, color):

           pygame.draw.rect(win, (color[0], color[1], color[2]), (self.x, self.y, self.dim, self.dim))

       def jump(self):

           keys = pygame.key.get_pressed()

           if keys[pygame.K_SPACE] and self.y == 400:
               self.vsp = 5

           self.y -= self.vsp

           if self.y < 400:
               self.vsp -= 0.15
           else:
               self.y = 400


    class Cactus:

        def __init__(self, dim):

           self.dim = dim

           self.cac = []
           self.speed = 0

        def startcac(self, x):

           if x % 100 == 0:

               stop_time = time.time()
               self.cac.append(801)


           for i in range(0, len(self.cac)):

               if self.speed < 4:
                   self.speed = 1 + x * 0.0005
               else:
                   self.speed == 10

               self.cac[i] -= self.speed
               pygame.draw.rect(win, (0, 200, 0), (self.cac[i], 400, 20, 20))

           try:
               if self.cac[0] < 0:
                   self.cac.pop(0)
           except IndexError:
               pass


        def collision(self, blob_cords):

           if any(i in [int(i) for i in self.cac] for i in [i for i in range(380, 421)]) and blob_cords[1] >= 380:
               self.cac = []

           dist = 0

           if len(self.cac) >= 2:
               # print(self.cac[-1] - self.cac[-2], blob.x - 1)
               for i in self.cac:
                   if i > 400 + 20:
                       dist = (i - 400 - 20) / (self.cac[-1] - self.cac[-2])

                       break

           if dist <= 1:
               print(dist)
               print(self.speed / 4)




    blob = Char(400, 400, 20, 0)
    # "player"
    cac = Cactus(20)
    # obstacle

    x = 1


    if __name__ == '__main__':

       game = True
       while game:
           pygame.time.delay(1)

           for event in pygame.event.get():
               if event.type == pygame.QUIT:
                   game = False

           # Draws things on screen

           win.fill((0, 0, 0))
           blob.jump()
           blob.draw_char([150, 0, 0])
           cac.startcac(x)
           x += 1
           cac.collision((blob.x, blob.y))

           # Updates movements and events

           # Update display

           pygame.display.update()

       pygame.QUIT()

对于草率的代码真的很抱歉,如果甚至需要的话,但是我们将不胜感激一些关于启动或改造项目的指导.

Really sorry for the sloppy code, if even needed, but some guidance to just start or revamp the project would be much appreciated.

谢谢!

推荐答案

PyGame 和 OpenAI-Gym 配合得很好.我无法评论您发布的游戏代码,这完全取决于您.如果游戏有效,它就有效.

PyGame and OpenAI-Gym work together fine. I can't comment on the game code you posted, that's up to you really. If the game works it works.

但是要使用 PyGame 创建 AI 代理,您需要先将您的环境转换为 Gym 环境.这可以通过遵循此指南来完成.

But to create an AI agent with PyGame you need to first convert your environment into a Gym environment. This can be done by following this guide.

之后,您可以使用 RL 库来实现您的代理.我的建议是 Stable-Baselines 这是 openai-baselines 的一个很好的分支使用起来更容易.

Afterwards you can use an RL library to implement your agent. My recommendation is Stable-Baselines which is a great fork off of openai-baselines that's a lot easier to work with.

如果你想要一些东西作为你作业的参考,可以看看这个小项目 我不久前做过.我创建了 BlockDude 游戏的副本并将其转换为 OpenAI Gym.然后我在这个 colab 中使用了 stable-baselines笔记本.

If you want something as reference for your assignment maybe check out this little project I did a while ago. I created a rendition of the BlockDude game and converted it to OpenAI Gym. I then used stable-baselines on it in this colab notebook.

这篇关于Pygame 和 Open AI 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆