
Master OpenAI Gym Environments with Python Tutorial
Introduction
Learning how to build a custom environment in OpenAI Gym with Python is a great way to understand reinforcement learning in action. This tutorial walks you through creating ChopperScape, an environment where an AI-controlled helicopter navigates obstacles, collects fuel, and earns rewards. You’ll explore how observation and action spaces work, define environment elements, and implement key Gym functions like reset, step, and render. By the end, you’ll know how to design interactive AI simulations that form the basis for more advanced machine learning experiments.
What is ChopperScape?
ChopperScape is a simple game-like environment created using OpenAI Gym where a helicopter, controlled by an AI agent, must fly through the air while avoiding birds and collecting fuel to keep going. It helps demonstrate how to build custom environments that let AI systems learn from trial and error in a fun, interactive way. This environment teaches the basics of designing and programming a space where an agent can take actions, receive rewards, and improve its performance over time.
Prerequisites
Alright, before we jump into the fun stuff, let’s make sure your setup is ready to go. You’ll need a machine with Python installed, nothing complicated, just enough to get you through some light coding. If you’ve already played around with Python basics like variables, loops, and functions, you’re in a good spot. Think of this as your base, because you wouldn’t start building a house before laying down a solid foundation, right?
Now, let’s chat about OpenAI Gym. You can think of it as a playground for your artificial intelligence projects. It gives you a bunch of different environments where your AI agents can learn through trial and error, just like when a kid learns to ride a bike by wobbling and maybe tipping over a few times (though don’t worry, this version is pain-free). You’ll run your agent in this digital space and watch as it gets better over time. OpenAI Gym and Python work perfectly together, letting you create, train, and test your AI models in a setup that feels structured but still fun and creative.
Dependencies/Imports
Alright, tool time! Before you can watch your helicopter agent zip around the sky, you’ll need to install a few key tools. These will take care of things like showing images, handling data, and helping you see what’s going on behind the scenes.
$ pip install opencv-python
$ pip install pillow
Once you’ve got those installed, let’s bring in all the imports that make this project run:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import PIL.Image as Image
import gym
import random
from gym import Env, spaces
import timefont = cv2.FONT_HERSHEY_COMPLEX_SMALL
Each one plays a special role. For example, NumPy is like the math whiz—it deals with numbers and calculations. OpenCV is your artist—it draws visuals and helps process images. Matplotlib is your showcase—it helps you see what your environment looks like. When you put them all together, your OpenAI Gym and Python setup feels lively and interactive, almost like watching your code come to life.
Description of the Environment
Imagine this: your internet suddenly cuts out, and boom, that little Dino Run game pops up on Chrome. You know the one, where the tiny dinosaur runs, jumps over cacti, and dodges flying birds. It’s simple but addictive. That’s the idea behind our environment. Only this time, instead of a dinosaur, your main character is a Chopper, a helicopter that soars through the air, dodging obstacles and collecting rewards.
The mission? Pretty straightforward but still exciting—fly as far as possible without crashing. The longer you survive, the more points you earn. But here’s where it gets interesting: you also have to manage your fuel. If your Chopper runs out, that’s the end of the round. Fortunately, you’ll find floating fuel tanks scattered around the sky (yes, floating tanks—let’s not question the physics here) that refill your tank up to 1000 liters.
Before you dive into coding, there’s something you need to understand first: observation space and action space. The observation space defines what your agent can “see” in its world. The action space defines what your agent can do. Understanding both helps balance realism and complexity so your AI can learn effectively.
ChopperScape Class
Now here’s where the fun really starts, building your world! The ChopperScape class is the master plan for everything that happens in your game. Here’s what you’ll be setting up inside the __init__ method:
- Canvas: The visual area where all the action unfolds.
- x_min, y_min, x_max, y_max: Boundaries of the world.
- Elements: A list of everything in the environment—Chopper, birds, and fuel tanks.
- max_fuel: The Chopper’s fuel capacity.
class ChopperScape(Env):
def __init__(self):
super(ChopperScape, self).__init__()
# Define a 2-D observation space
self.observation_shape = (600, 800, 3)
self.observation_space = spaces.Box(low=np.zeros(self.observation_shape), high=np.ones(self.observation_shape), dtype=np.float16)
# Define an action space ranging from 0 to 4
self.action_space = spaces.Discrete(6,)
# Create a canvas to render the environment images upon
self.canvas = np.ones(self.observation_shape) * 1
# Define elements present inside the environment
self.elements = []
# Maximum fuel the chopper can take at once
self.max_fuel = 1000
# Permissible area of the helicopter to be
self.y_min = int(self.observation_shape[0] * 0.1)
self.x_min = 0
self.y_max = int(self.observation_shape[0] * 0.9)
self.x_max = self.observation_shape[1]
Elements of the Environment
Now that your world has structure, it’s time to fill it with life. You’ll need:
- The Chopper — main hero.
- The Flying Birds — obstacles.
- The Floating Fuel Stations — refueling points.
Each is a class derived from a parent Point class, sharing common functionality.
Point Base Class
The Point class defines the core attributes—location, boundaries, and name—for every object in the environment.
class Point(object):
def __init__(self, name, x_max, x_min, y_max, y_min):
self.x = 0
self.y = 0
self.x_min = x_min
self.x_max = x_max
self.y_min = y_min
self.y_max = y_max
self.name = name def set_position(self, x, y):
self.x = self.clamp(x, self.x_min, self.x_max – self.icon_w)
self.y = self.clamp(y, self.y_min, self.y_max – self.icon_h) def get_position(self):
return (self.x, self.y) def move(self, del_x, del_y):
self.x += del_x
self.y += del_y
self.x = self.clamp(self.x, self.x_min, self.x_max – self.icon_w)
self.y = self.clamp(self.y, self.y_min, self.y_max – self.icon_h) def clamp(self, n, minn, maxn):
return max(min(maxn, n), minn)
Chopper, Bird, and Fuel Classes
The Chopper, Bird, and Fuel classes inherit from Point and define their own appearances and sizes.
class Chopper(Point):
def __init__(self, name, x_max, x_min, y_max, y_min):
super(Chopper, self).__init__(name, x_max, x_min, y_max, y_min)
self.icon = cv2.imread(“chopper.png”) / 255.0
self.icon_w = 64
self.icon_h = 64
self.icon = cv2.resize(self.icon, (self.icon_h, self.icon_w))class Bird(Point):
def __init__(self, name, x_max, x_min, y_max, y_min):
super(Bird, self).__init__(name, x_max, x_min, y_max, y_min)
self.icon = cv2.imread(“bird.png”) / 255.0
self.icon_w = 32
self.icon_h = 32
self.icon = cv2.resize(self.icon, (self.icon_h, self.icon_w))class Fuel(Point):
def __init__(self, name, x_max, x_min, y_max, y_min):
super(Fuel, self).__init__(name, x_max, x_min, y_max, y_min)
self.icon = cv2.imread(“fuel.png”) / 255.0
self.icon_w = 32
self.icon_h = 32
self.icon = cv2.resize(self.icon, (self.icon_h, self.icon_w))
Back to the ChopperScape Class
Every world needs rules. In reinforcement learning, those rules come from two key functions:
- reset() – The restart button for the environment.
- step() – Moves the simulation forward one step at a time.
Reset Function
The reset function is like starting a new level. It wipes the slate clean, refills the Chopper’s fuel, and places it in a fresh position each time. A helper called draw_elements_on_canvas handles drawing the Chopper, birds, and fuel tanks, as well as the stats on your screen.
(From here, the process continues with rendering, game steps, and collisio
Conclusion
Mastering OpenAI Gym with Python gives you the tools to create dynamic reinforcement learning environments that are both educational and fun. In this tutorial, you learned how to build ChopperScape, define observation and action spaces, and implement key Gym functions like reset, step, and render. These concepts form the backbone of interactive AI simulations and help you understand how agents learn through trial and error.By experimenting with OpenAI Gym and Python, you’re not just coding—you’re shaping intelligent behaviors in virtual worlds. As reinforcement learning continues to evolve, expect to see more advanced, real-world applications built on similar frameworks. Keep exploring, keep training, and let your AI agents take flight in even more complex environments.
Create Custom OpenAI Gym Environments: Build Chopper Game with Coding (2025)