O'Reilly logo

Hands-On Reinforcement Learning with Python by Sudharsan Ravichandiran

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a video game bot

Let's learn how to build a video game bot which plays a car racing game. Our objective is that the car has to move forward without getting stuck on any obstacles or hitting other cars.

First, we import the necessary libraries:

import gymimport universe # register universe environmentimport random

Then we simulate our car racing environment using the make function:

env = gym.make('flashgames.NeonRace-v0')env.configure(remotes=1) #automatically creates a local docker container

Let's create the variables for moving the car:

# Move leftleft = [('KeyEvent', 'ArrowUp', True), ('KeyEvent', 'ArrowLeft', True),        ('KeyEvent', 'ArrowRight', False)]#Move rightright = [('KeyEvent', 'ArrowUp', True), ('KeyEvent', 'ArrowLeft', False), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required