A reinforcement learning project that trains a neural network to play the Space Waves game using Deep Q-Network (DQN) with computer vision for real-time gameplay.
This project implements an AI agent that learns to play Space Waves, a browser-based obstacle avoidance game, using:
- Computer Vision: Real-time screen capture and image processing to detect game state
- Deep Q-Learning: Neural network-based reinforcement learning with experience replay
- Automated Control: Direct keyboard input simulation for game interaction
The AI processes visual information from the game screen, identifies the player position and obstacles, and learns optimal movement strategies through trial and error.
Watch the AI in action:
Example of the trained AI playing Space Waves - showing real-time obstacle detection and avoidance
- Input: 3-channel 128x128 images containing:
- Player position map
- Distance-to-obstacle map
- Obstacle location map
- Architecture: Convolutional layers โ Fully connected layers
- Output: Q-values for 2 actions (hold space / release space)
- Player Detection: Triangle detection using contour analysis
- Obstacle Mapping: Binary thresholding and distance transforms
- Game State Recognition: Template matching for game over/pause states
- Real-time Processing: Screen capture and frame preprocessing
- Double DQN: Reduces overestimation bias in Q-learning
- Experience Replay: Stores and samples past experiences for stable learning
- Epsilon-Greedy: Balances exploration vs exploitation during training
- OS: Windows (uses
pyautoguifor keyboard control) - Python: 3.10 or higher
- GPU: CUDA-compatible GPU recommended for faster training
dependencies = [
"mss==10.1.0", # Screen capture
"numpy==2.2.6", # Numerical computations
"opencv-python==4.12.0.88", # Computer vision
"pyautogui==0.9.54", # Keyboard/mouse automation
"pydantic-settings==2.12.0", # Configuration management
"torch", # Deep learning framework
]-
Clone the repository:
git clone <repository-url> cd AI_ML
-
Set up Python environment:
# Using uv (recommended) uv venv uv pip install -e . # Or using pip pip install -e .
-
Install PyTorch:
# For CUDA (recommended) uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118 # For CPU only uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
- Open Space Waves game in your browser
- Position the game window according to the monitor settings in
project_settings.py - Run training:
python train.py
The training script will:
- Wait 5 seconds for you to focus the game window
- Automatically detect when the game starts/ends
- Train the neural network through gameplay
- Save model checkpoints after each episode
Edit project_settings.py to customize:
class ProjectSettings(BaseSettings):
frame_size: Tuple[int, int] = (853, 480) # Game window size
player_min_area: int = 200 # Player detection threshold
screen_monitor: Dict = { # Screen capture area
"height": 1080, "left": 0, "top": 0, "width": 1920
}from environment.space_waves_vision import SpaceWavesVision
# Initialize vision system
vision = SpaceWavesVision(frame_size=(853, 480), player_min_area=200)
# Process video file
vision.process_video_file(
video_path="path/to/gameplay.mp4",
target_fps=30,
save_path="processed_output.mp4"
)AI_ML/
โโโ environment/
โ โโโ models.py # Data models and base vision class
โ โโโ space_waves_vision.py # Computer vision pipeline
โโโ assests/
โ โโโ game_over.png # Template for game over detection
โ โโโ pause_btn.png # Template for game start detection
โ โโโ example_img.png # Sample processed frame
โ โโโ example_run.mp4 # Sample gameplay video
โโโ neural_net.py # DQN implementation and replay buffer
โโโ train.py # Main training script
โโโ project_settings.py # Configuration settings
โโโ pyproject.toml # Project dependencies
โโโ README.md # This file
The system captures screen frames and processes them to extract game information:
# Detect player triangle
player_coords = vision.get_player_cord(frame)
# Generate obstacle and distance maps
obstacle_map, distance_map = vision.get_obstacles_map(frame, player_coords)
# Create input tensor for neural network
inputs = np.stack([player_map, distance_map, obstacle_map], axis=0)The neural network processes the visual input and outputs Q-values for each action:
# Get Q-values from network
q_values = policy_net(inputs)
# Choose action (epsilon-greedy)
if random.random() < epsilon:
action = random.choice([0, 1]) # Explore
else:
action = q_values.argmax().item() # ExploitThe agent learns from experience using Double DQN:
# Calculate target Q-values
next_actions = policy_net(next_states).argmax(dim=1)
target_q = target_net(next_states).gather(1, next_actions.unsqueeze(1))
targets = rewards + gamma * target_q * (1 - dones)
# Update network
loss = F.mse_loss(current_q, targets)
optimizer.step()Key training parameters in train.py:
gamma = 0.99 # Discount factor
batch_size = 64 # Training batch size
lr = 1e-4 # Learning rate
buffer_capacity = 100_000 # Replay buffer size
eps_start = 1.0 # Initial exploration rate
eps_end = 0.05 # Final exploration rate
eps_decay_steps = 200_000 # Exploration decay steps- Game not detected: Adjust
screen_monitorsettings inproject_settings.py - Player detection fails: Modify
player_min_areaor detection thresholds - Performance issues: Reduce
input_frame_sizeor use CPU-only mode - Keyboard control not working: Ensure
pyautoguihas proper permissions
Enable debug visualization:
vision.process_video_file(video_path, debug=True) # Step through frames
vision.process_from_screen(display_map=True) # Show real-time processingThe AI typically achieves:
- Training time: 2-4 hours for basic competency
- Frame processing: 30+ FPS on modern hardware
- Success rate: 70-90% obstacle avoidance after training
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source. Please check the license file for details.
- OpenCV for computer vision capabilities
- PyTorch for deep learning framework
- MSS for efficient screen capture
- Space Waves game developers
Note: This project is for educational purposes. Ensure you comply with the game's terms of service when using automated tools.