A modern implementation of the classic Asteroids arcade game enhanced with artificially intelligent agents trained using neural networks and genetic algorithms. Watch AI pilots evolve from novices to experts as they learn to navigate space, avoid hazards, and destroy asteroids with increasing efficiency.
This project reimagines the retro-style Asteroids game with a modern twist: AI agents trained to play the game autonomously. The core gameplay involves navigating a spaceship through a field of asteroids while shooting them to score points and avoid collisions.
The AI implementation combines neural networks with evolutionary algorithms to develop agents that learn optimal navigation and shooting strategies through a process mimicking natural selection.
- Manual Mode: Classic gameplay with keyboard controls
- AI Mode: Watch AI agents navigate and survive autonomously
- Training Visualization: Real-time display of neural network decision-making
- Neural network-driven decision making
- Genetic algorithm for evolutionary learning
- Performance tracking across generations
- Visual representation of AI decision processes
- Physics-based movement with momentum and inertia
- Asteroid splitting mechanics
- Screen-wrapping (toroidal space)
- Progressive difficulty scaling
- Python 3.8 or higher
- Pygame (for game rendering and mechanics)
- Clone the repository:
git clone https://github.com/yourusername/asteroids-ai.git
cd asteroids-ai- Install dependencies:
pip install -r requirements.txt- Run the game:
# For manual gameplay:
python game_base.py
# For AI training mode:
python learning.py- Control: Keyboard (manual) or neural network (AI mode)
- Physics: Realistic momentum with acceleration/deceleration
- Movement: Rotation, thrust, and shooting capabilities
- Boundaries: Wraps around screen edges
- Variety: Three size categories (large, medium, small)
- Behavior: Split into smaller fragments when shot
- Generation: Random initial position, velocity, and rotation
- Collision: Destroys ship or splits upon bullet impact
- Points: Large asteroids (20), Medium asteroids (50), Small asteroids (100)
- AI Fitness: Combined function of survival time and score
- Manual Mode: Level-based asteroid density increase
- AI Mode: Time-based periodic spawning with increasing frequency
The AI agents utilize a feedforward neural network to process game state information and output control decisions.
Input features are normalized to ensure efficient learning:
- Player velocity components:
$\frac{v_x}{v_{max}}$ ,$\frac{v_y}{v_{max}}$ - Distance to nearest asteroid:
$\frac{d_{min}}{d_{scale}}$ where$d_{scale} = 300$ - Relative angle to nearest asteroid:
$\frac{\theta}{π}$ - Relative velocity of nearest asteroid:
$v_{rel_x}$ ,$v_{rel_y}$
Hidden Layer (10 neurons)
Each hidden neuron
where
Each output neuron
The four outputs correspond to binary actions:
- Rotate Left: $a_1 = \begin{cases} 1 & \text{if } o_1 > 0.5 \ 0 & \text{otherwise} \end{cases}$
- Rotate Right: $a_2 = \begin{cases} 1 & \text{if } o_2 > 0.5 \ 0 & \text{otherwise} \end{cases}$
- Thrust: $a_3 = \begin{cases} 1 & \text{if } o_3 > 0.5 \ 0 & \text{otherwise} \end{cases}$
- Shoot: $a_4 = \begin{cases} 1 & \text{if } o_4 > 0.5 \ 0 & \text{otherwise} \end{cases}$
- Input to Hidden:
$W_1 \in \mathbb{R}^{6 \times 10}$ (60 weights) - Hidden to Output:
$W_2 \in \mathbb{R}^{10 \times 4}$ (40 weights) - Total parameters: 100 weights
The genetic algorithm evolves the neural network weights across generations to improve performance.
- Population Size: 20 agents per generation
- Genome Representation: 100 floating-point weights in range [-1, 1]
The fitness of an agent is calculated as:
where
Tournament selection with the following process:
- Select top 20 performing agents
- Randomly sample 5 agents from this pool
- Choose the agent with highest fitness as parent
For each weight in the child network:
Each weight has a 5% chance of mutation:
where
- Top 10 agents preserved unchanged
- Bottom 10 replaced with offspring from crossover and mutation
- Generate initial population with random weights
- Evaluate each agent's fitness in the game environment
- Sort population by fitness
- Implement elitism (preserve top performers)
- Generate offspring through selection, crossover, and mutation
- Replace lower-performing agents with offspring
- Repeat for specified number of generations
To accelerate learning, behavior heuristics are implemented:
-
Rotation Assistance: If
$\frac{d_{min}}{300} < 0.5$ and$|\theta| > 0.2\pi$ , force rotation toward asteroid -
Shooting Regulation: If
$|\theta| > 30°$ , disable shooting -
Thrust Management: If
$\frac{d_{min}}{300} < 0.4$ and$v < 1.5$ , enable thrust
These heuristics provide initial guidance while allowing the neural network to develop more sophisticated strategies over time.
During training, the following are displayed:
- Current generation number
- Agent index within population
- Current survival time
- Best survival time achieved
- Visual indicator connecting ship to nearest asteroid
The project began with implementing a classic Asteroids game with manual controls. This established the core mechanics:
- Physics-based movement
- Collision detection
- Asteroid spawning and splitting
- Scoring system
The first AI implementation featured:
- Simple neural network with basic inputs
- Genetic algorithm with fitness based only on score
- Limited environment awareness
Challenge: Agents showed poor survival skills as they lacked incentive for evasive maneuvers.
To improve AI performance:
- Added survival time to fitness function
- Expanded neural inputs to include relative velocity
- Implemented continuous asteroid spawning for consistent challenge
Challenge: Agents struggled with precise navigation and shooting alignment.
Introduced heuristic overrides to:
- Guide rotation when asteroids are close
- Prevent wasting bullets when misaligned
- Manage thrust based on proximity and velocity
Final improvements included:
- Asyncio integration for consistent execution across platforms
- Optimized rendering for training speed
- Refined genetic algorithm parameters
Typical performance progression across generations:
- Gen 1-5: Random movements, average survival < 5 seconds
- Gen 6-15: Basic avoidance, survival time 10-20 seconds
- Gen 16-30: Coordinated movement and shooting, survival time 30-60 seconds
- Gen 31+: Advanced strategies, survival time 60+ seconds
- Survival Time: Primary measure of agent fitness
- Score: Secondary measure of shooting efficiency
- Generation-to-Competence: Number of generations to reach 30+ seconds survival
- Implement convolutional layers for direct screen input
- Add recurrent connections for temporal awareness
- Explore reinforcement learning approaches
- Extend inputs to track multiple nearest asteroids
- Implement attention mechanisms for threat prioritization
- Implement novelty search to encourage exploration
- Add curriculum learning with progressive difficulty
- Explore neuroevolution of augmenting topologies (NEAT)
- Player vs. AI competitive mode
- AI difficulty selection based on generation
- Interactive training interface
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
=
