为了设置你的机器人开发环境,你必须下载游戏本身以及进行计算所需的库。

首先为这个项目创建一个名为 AtariBot 的工作区:

Bash
mkdir ~/AtariBot

导航到新的 AtariBot 目录:

Bash
cd ~/AtariBot

然后为项目创建一个新的虚拟环境。你可以随意命名这个虚拟环境;这里,我们将其命名为 ataribot

Bash
python3 -m venv ataribot

激活你的环境:

Bash
source ataribot/bin/activate

在 Ubuntu 16.04 及更高版本上,OpenCV 需要安装一些额外的包才能正常运行。这包括 CMake(一个管理软件构建过程的应用程序)以及会话管理器、各种扩展和数字图像合成工具。运行以下命令安装这些包:

Bash
sudo apt-get install -y cmake libsm6 libxext6 libxrender-dev libz-dev

注意: 如果你是在运行 macOS 的本地机器上按照本指南操作,唯一需要额外安装的软件是 CMake。使用 Homebrew(如果你遵循了先决条件中的 macOS 教程,应该已经安装了 Homebrew)输入以下命令进行安装:

Bash
brew install cmake

接下来,使用 pip 安装 wheel 包,它是 wheel 打包标准的参考实现。作为一个 Python 库,这个包作为构建 wheel 的扩展,并包含一个用于处理 .whl 文件的命令行工具:

Bash
python -m pip install wheel

除了 wheel,你还需要安装以下包:

  • Gym:一个 Python 库,提供各种游戏供研究使用,以及 Atari 游戏的所有依赖项。由 OpenAI开发,Gym 为每款游戏提供公共基准,以便可以统一评估各种智能体和算法的性能。
  • TensorFlow:一个深度学习库。这个库使我们能够更高效地运行计算。具体来说,它通过使用 TensorFlow 的抽象构建数学函数,这些函数专门在你的 GPU 上运行。
  • OpenCV:前面提到的计算机视觉库。
  • SciPy:一个科学计算库,提供高效的优化算法。
  • NumPy:一个线性代数库。

使用以下命令安装每个包。请注意,此命令指定了每个包要安装的版本:

Bash
python -m pip install gym==0.9.5 tensorflow==1.5.0 tensorpack==0.8.0 numpy==1.14.0 scipy==1.1.0 opencv-python==3.4.1.15

在此之后,再次使用 pip 安装 Gym 的 Atari 环境,其中包含各种 Atari 视频游戏,包括《太空入侵者》:

Bash
python -m pip install gym[atari]

如果你的 gym[atari] 包安装成功,你的输出将以以下内容结束:

Output
Installing collected packages: atari-py, Pillow, PyOpenGL
Successfully installed Pillow-5.4.1 PyOpenGL-3.1.0 atari-py-0.1.7

安装好这些依赖项后,你就可以继续构建一个随机玩游戏的智能体,作为你的比较基准。


你准备好构建第一个随机游戏智能体了吗?


Step 1 — Creating the Project and Installing Dependencies
In order to set up the development environment for your bots, you must
download the game itself and the libraries needed for computation.
Begin by creating a workspace for this project named AtariBot:
mkdir ~/AtariBot
Navigate to the new AtariBot directory:
cd ~/AtariBot
Then create a new virtual environment for the project. You can name
this virtual environment anything you’d like; here, we will name it
ataribot:
python3 -m venv ataribot
Activate your environment:
source ataribot/bin/activate
On Ubuntu, as of version 16.04, OpenCV requires a few more packages
t o be installed in order to function. These include CMake — an
application that manages software build processes — as well as a session
manager, miscellaneous extensions, and digital image composition. Run
the following command to install these packages:
sudo apt-get install -y cmake libsm6 libxext6 libxrender-dev libz-dev
NOTE: If you’re following this guide on a local machine running
MacOS, the only additional software you need to install is CMake. Install
it using Homebrew (which you will have installed if you followed the
prerequisite MacOS tutorial) by typing:
brew install cmake
Next,
 use pip
 to
 install
 the wheel
 package, the reference
implementation of the wheel packaging standard. A Python library, this
package serves as an extension for building wheels and includes a
command line tool for working with .whl files:
python -m pip install wheel
In addition to wheel, you’ll need to install the following packages:
Gym, a Python library that makes various games available for
research, as well as all dependencies for the Atari games. Developed
by OpenAI, Gym offers public benchmarks for each of the games so
that the performance for various agents and algorithms can be
uniformly /evaluated.
Tensorflow, a deep learning library. This library gives us the ability
to run computations more efficiently. Specifically, it does this by
building mathematical functions using Tensorflow’s abstractions
that run exclusively on your GPU.
OpenCV, the computer vision library mentioned previously.
SciPy, a scientific computing library that offers efficient optimization
algorithms.
NumPy, a linear algebra library.
Install each of these packages with the following command. Note that
this command specifies which version of each package to install:
python -m pip install gym==0.9.5 tensorflow==1.5.0 tensorpack==0.8.0
numpy==1.14.0 scipy==1.1.0 opencv-python==3.4.1.15
Following
 this,
 use pip once more to install Gym’s Atari
environments, which includes a variety of Atari video games, including
Space Invaders:
python -m pip install gym[atari]
If your installation of the gym[atari] package was successful, your
output will end with the following:
Output
Installing collected packages: atari-py, Pillow, PyOpenGL
Successfully installed Pillow-5.4.1 PyOpenGL-3.1.0 atari-py-0.1.7
With these dependencies installed, you’re ready to move on and build
an agent that plays randomly to serve as your baseline for comparison.

Last modified: Wednesday, 25 June 2025, 12:16 PM