Learn to code a Game AI in Python in under 10 minutes – Part I

Today I will show you how you can create create a game AI in under 10 minutes. All you have to do is bear with me for 10 minutes.

This tutorial assumes that you have only basic knowledge of programming and are familiar with at least one procedural language (because we are going to use Python). If not, no worries. You can learn it later too. Let’s focus on the aim of this tiny endeavor.

Open AI

Open AI is an Open Source project on Artificial Intelligence providing a workspace for programmers (such as yourself) to experiment with AIs without much hassle on your behalf. Designing a Game AI required you to design a Game first. Well, we don’t have time for that… What we are interested in is the AI and not the game. The OpenAI Gym project provides us a set of utilities to incorporate our AI into any supported Games. At the moment, let’s just use a Retro-style Arcade game. Gym comes prepackaged with a set of environments based on Arcade Games but you can install fancier environments later.

Installing Open AI Gym

On Linux (tested on Ubuntu 17.10)

  1. Installing Python: We will be using Python 3 for this project. So, even though most Linux environments ship with Python 2.7 installed, we have to do a clean installation of Python 3. Open Terminal and type –
    sudo apt-get install python3
  2. Making sure pip is installed: Pip is the python command line package management tool which should be installed by default but it’s safe to check.
    sudo apt-get install python3-pip
  3. Installing OpenAI Gym: There are several instructions for installing Gym for several supported systems. Head over to Gym’s Github Page to see the specific instruction for your system. If required you might have to install linuxbrew too.
  4. Installing Python Idle: IDLE is a Python IDE developed by it’s creators. This enables us to run an interactive shell for Python. Let’s install this to check out our cool new playground. Open Terminal (if not already open) and run this code:
    sudo apt-get install idle

On Windows (tested on Windows 10)

  1. Installing Python: This is easy as pie for Windows users. Python now provides GUI Installations for your system. Go to their Releases Page and download the correct executable installer for your System Architecture (x86 if 32-bit and x86-64 if 64-bit) and install. While installing be sure to check the Set PATH variable box.
  2. Installing Gym: Open a command prompt window and type in
    pip install gym

Let’s get started

Now you should be locked and loaded to fire up your first OpenAI environment. Open a new Terminal on Linux and type in “`idle“` and hit Enter. It should open up an IDLE prompt.

On Windows, open up a Command Prompt window and type in “`python“` to open an interactive shell.

Put the following code. (Note: Python  IDLE might show a multiline detected error if you copy and paste the code so you have to type it in manually, no shortcuts 😛 ).

import gym

env = gym.make('MountainCar-v0')

state = env.reset()

env.render()

You should see a screen like this:

Screenshot from 2017-12-05 21-46-21

Environment running on Linux

slide

Same one on windows

The goal in this game is to get this car to the top of that mountain with the Yellow Flag on it. The rules of the game are:

  1. You can only push the car to the left (state 0), not at all (state 1) or to the right (state 2).
  2. There is no penalty for going left
  3. Each step you make gives you a -1 point. So, you have to complete it in the minimum number of steps.

The variables for any environment in OpenAI Gym can be categorised into 2 sections:

  1. Observation Space: The data you get from the state of the environment (e.g.,  current position and velocity)
  2. Action Space: The data you put into the environment (e.g., push direction)

For this game the observation space returns you a list of 2 values.

  1. The first one is a measure of your displacement.
  2. The second one is a measure of your velocity.

You can perform an action by specifying a single variable having a Discrete value 0,1 or 2. A value of 0 means a push to the left, 1 means no push and 2 means a push to the right. By learning to co-ordinate the pushes, your AI must overcome the hurdle of climbing the hill. First let’s use a dummy method to achieve our goal. Open a new file named test.py and paste the following code into it and save:

<pre>import gym

env = gym.make('MountainCar-v0')

env.reset()

state, reward, done, info = env.step(2)

vel = state[1]

accl = vel

for _ in range(1000):

 env.render()

  if(accl >= 0 or state[0] > 0):

    state, reward, done, info = env.step(2)

  else:

    state, reward, done, info = env.step(0)

  if(state[0] >= 0.5):

    break

  accl = state[1] - vel

  vel = state[1]



print('Solved')</pre>

This will do the job. Open a Terminal and type in “`python3 test.py“` and you can see the car in action. But this is just a fix for a particular game. There are several paths you can take from here. For instance, you can apply a Neural Network to simulate more than one episodes and at each state, update the nodes until your NN figures out the problem. Or you can implement a Reinforcement Learning Algorithm like I’ll be showing in the next tutorial to improve this AI. If you are familiar with the basics of AI algorithms, implementing one should not be a problem.

Further Reading

Here is a list of resources for learning AI algorithms:

  1. Reinforcement Learning – Wikipedia

  2. Neural Network – Wikipedia

  3. PyBrain

  4. General Game AIs in Industry

P.S., if you have not already seen it, now might be a good time to check out my earlier post about the several Optimization Algorithms. You can also see my Python implementation of such algorithms and try to implement it here. Stay tuned for the next Part to learn how to implement a proper algorithm for such games. Good luck exploring !

If you like this post please do share it and subscribe. If you have a query or would like to add to this post, please comment down below. Thanks for reading.

Advertisements

Top 5 Presentation Creation Tools for your website

Developers, type no more! There are tools on the market that you probably didn’t know about that let you create awe-striking slides and carousels with just a few clicks. While Bootstrap Carousels would be in every designer’s toolbox, these plugins and apps seem to work just as well and maybe even better. This list of the Top 5 Slide Creation Tools will give you a real headstart into your app development.

RevealJS

Reveal JS Home

Being innovative has never been easier on the web. With this presentation framework, you can build One-Page-Wonders. Forget about the tradition carousels and slider- this gives you a whole new level of smooth. Don’t take my word for it. See it to believe it. It also features a bunch of other capabilities like exporting to PDF, sharing on slides.com and using Markdown contents. Read more on Github

 

CSS Slider

CSS Slider App Demo

Create beautiful sliders with the CSS Slider app

Are you very picky about your plugins? Or simply want a purely CSS-based library? Either ways, this is the right choice. This helps you create seamless CSS Slides without a pint of coding (Yes, you heard me right!). Downloadable for Windows and Mac Platforms, this lets you customize your slider bottom up using a WYSIWYG interface. And to add icing to the cake, it also supports Retina-display for better resolutions. So fret no more. Download today!

Slicebox

Slicebox demo

Slicebox 3D rotate effect

While 2D gets the job done quite well, sometimes you need a 3D plugin to build a great design. Slicebox JQuery Plugin is the way to go. It’s small, it’s free and it’s Open Source! Read their docs over a Github and download the code.

MaterializeCSS Slider

MaterializeCSS Slider Demo

Right-aligned MaterializeCSS Fullscreen slider

MaterializeCSS is a beautiful CSS/JQuery framework for material design popularized by Google. Are you a fan of material design? Do you want a carousel tailored to your site’s material blocks? MaterializeCSS understands. It provides you one of the richest material 3D carousel experience. They also have a plain slide feature in case you wanna keep it clean. You are missing out if you haven’t tried it yet.

SlidesJS

SlidesJS Standard Demo

The standard SlidesJS demo

Want something simple but elegant? You are bound to fall for this eye-catchingly simple slideshow plugin for JQuery. Download the code at www.slidesjs.com and start creating beautiful sliders.

If you find this post helpful please dont forget to like, share and comment. Thanks for reading.