Following is an example of a simple game which could be used to train agents.
import pygame as pg from pygame.locals import * import sys import random def new_rect_after_action(newrct, act): if act == 'right': if newrct.right + newrct.width > windowWidth: return newrct else: return pg.Rect(newrct.left + newrct.width, newrct.top, newrct.width, newrct.height) # Rect(left, top, width, height) else: # action is left if newrct.left - newrct.width < 0: return newrct else: return pg.Rect(newrct.left - newrct.width, newrct.top, newrct.width, newrct.height) # Rect(left, top, width, height) def circle_falling(crclradius): newx = random.randint(0 + crclradius, 200 - crclradius) multiplier = random.randint(1, 4) newx *= multiplier return newx windowWidth = 800 windowHeight = 400 FPS = 3 # frames per second setting fpsClock = pg.time.Clock() pg.init() window = pg.display.set_mode((windowWidth, windowHeight)) # width, height pg.display.set_caption('Catch the ball!') # setup colors RED = (255, 0, 0) GREEN = (0, 255, 0) WHITE = (255, 255, 255) BLACK = (0, 0, 0) # specify circle properties crclCentreX = 50 crclCentreY = 50 crclRadius = 20 # specify rectangle properties rctLeft = 400 rctTop = 350 rctWidth = 200 rctHeight = 50 rct = pg.Rect(rctLeft, rctTop, rctWidth, rctHeight) # Rect(left, top, width, height) action = 'left' score = 0 font = pg.font.Font(None, 30) while True: for event in pg.event.get(): if event.type == QUIT: pg.quit() sys.exit() elif event.type == pg.KEYDOWN: if event.key == pg.K_LEFT: action = 'left' rct = new_rect_after_action(rct, action) elif event.key == pg.K_RIGHT: action = 'right' rct = new_rect_after_action(rct, action) window.fill(BLACK) # at this position, the rectangle should be here. else loses if crclCentreY >= windowHeight - rctHeight - crclRadius: if rct.left <= crclCentreX <= rct.right: score += 1 else: score -= 1 crclCentreX = circle_falling(crclRadius) crclCentreY = 50 else: crclCentreY += 20 pg.draw.circle(window, RED, (crclCentreX, crclCentreY), crclRadius) # circle(Surface, color, pos(x, y), radius, width=0) pg.draw.rect(window, GREEN, rct) # rect(Surface, color, Rect, width=0) text = font.render('score: ' + str(score), True, (238, 58, 140)) window.blit(text, (windowWidth - 100, 10)) pg.display.update() fpsClock.tick(FPS)This will create a game window that looks like the following screenshot.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHLDueeWZCb9e-E3ZqgRAP2yt0l1HLvhuwnxajw89ycI9dvDnLSMBi-XaB3856NgH_7OfkdDKGCfxG6YoLqkWcLKKhsiX76EBQLlOWp44dIqD410o4CeAr7CZ0CJfLsADapup5S-yyDTSa/s320/prfct.png)
Update: Second part is now available: http://anixtech.blogspot.com/2016/12/reinforcement-learning-with-pygame.html
No comments:
Post a Comment