Post

Building an AI-Powered Frogger Game: Training and Challenges

Have you ever played the classic arcade game Frogger? You know, the one where you guide a little frog across busy roads and rivers to reach the other side? Well, I recently built a version of this game using Python and Pygame, and then took it a step further by adding artificial intelligence (AI) to train an agent to play it automatically. In this post, I’ll explain what this project is, how to use it, and why a missing feature makes it a bit tricky to work with locally.

What Is This Project?

This is a simple implementation of Frogger, the classic game, combined with machine learning. The game itself is built using Pygame, a popular library for creating 2D games in Python. The frog starts at the bottom of the screen and needs to reach the top by crossing:

  • Roads with moving cars that can squish the frog
  • Rivers with floating logs that the frog can ride on

The AI part uses a technique called Deep Q-Learning, which is a type of reinforcement learning. Basically, the AI learns by playing the game over and over, getting rewards for good moves (like moving forward or reaching the goal) and penalties for bad ones (like getting hit by a car or falling in the water). After training, the AI should be able to play the game pretty well on its own.

The project includes four main files:

  • frogger_game.py: The game logic and graphics
  • ai_trainer.py: The AI training code
  • ai_play.py: Script to run the trained AI autonomously
  • generate_frogger_icons.py: A script to create simple game icons

How to Use It

Playing the Game Manually

To play the game yourself:

  1. Make sure you have Python and Pygame installed:

    pip install pygame numpy tensorflow
  2. Run the game:

    python frogger_game.py

Playing with Trained AI

After training the AI, you can watch it play autonomously:

  1. Make sure you have a trained model (from running ai_trainer.py):
    python ai_play.py

This will load the trained AI model and run the game, showing the frog making decisions in real-time. You can watch as the AI attempts to cross the roads and rivers using what it learned during training.

Training the AI

To train the AI agent:

  1. Run the trainer:
    python ai_trainer.py

This will start training the AI for 1000 episodes (games). Each episode, the AI plays the game, learns from its mistakes, and gets better over time. The training can take a while, especially on slower computers, but you can stop it anytime with Ctrl+C and it will save progress.

During training, you’ll see output like:

Episode: 1/1000, Score: -10, Epsilon: 0.99, Steps: 50, Episode Time: 2.34s
  • Score: How well the AI did (higher is better)
  • Epsilon: How much the AI explores vs. uses what it knows (starts high, decreases)
  • Steps: How many moves it took
  • Time: How long the episode took

After training, you’ll get a plot showing the AI’s progress and a saved model file.

What Happens in Each Episode

Each training episode follows this process:

  1. Game Reset: The frog starts back at the bottom with 3 lives, and all cars and logs are repositioned randomly.

  2. Decision Making: For every move, the AI decides whether to explore (try a random action) or exploit (use its learned knowledge). Early in training, it explores a lot (epsilon starts at 1.0), but gradually shifts to exploiting what it knows (epsilon decreases to 0.05).

  3. Action and Movement: The AI chooses one of 4 actions: move up, down, left, or right. The frog moves one grid space in that direction.

  4. Environment Response: Cars and logs move automatically. The AI checks for collisions or successful crossings.

  5. Learning: After each move, the AI receives a reward (see below), stores this experience in its memory, and trains on a random batch of past experiences to improve its decision-making.

  6. Episode End: The episode continues until the frog loses all 3 lives (by getting hit by cars or falling in water). Then it starts over with the next episode.

The Rewards System

The AI learns through a reward system that encourages good behavior and discourages bad outcomes:

  • +1 point: Moving forward (up) toward the goal
  • -0.5 points: Moving backward (down) away from the goal
  • -25 points: Getting hit by a car or falling into water (collision penalty)
  • +50 points: Successfully reaching the goal area

These rewards help the AI understand that reaching the top is good, avoiding obstacles is crucial, and moving forward is better than backward. The AI uses these rewards to learn the best strategies over many episodes.

Overcoming the Inference Challenge

Originally, this project had a common limitation in machine learning projects – while it could train the AI, it didn’t provide an easy way to actually see the trained AI play the game. This “inference” capability is crucial for testing, debugging, and demonstrating results.

To address this, I added an ai_play.py script that loads the trained model and runs the game autonomously. Now you can:

  • Demo the results: Watch the AI play after training
  • Debug performance: See exactly how the AI behaves in different situations
  • Compare training runs: Easily test different models or training parameters

This makes the project much more complete and user-friendly for local development and sharing with others.

Conclusion

This Frogger AI project demonstrates the power of reinforcement learning in game environments. By implementing both training and inference capabilities, it provides a complete example of how AI can learn and perform complex tasks. The addition of the autonomous play feature makes it much easier to work with locally and share results.

If you’re interested in AI or game development, give this project a try! Now you can train your own AI frog and watch it attempt to master the classic challenge.

This post is licensed under CC BY 4.0 by the author.