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.
Thanks!
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.
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.
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.
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.