Skip to content

Prithvi824/Space-Waves-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Space Waves AI - Deep Q-Learning Game Bot

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.

๐ŸŽฏ Project Overview

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.

๏ฟฝ Demo

Watch the AI in action:

AI Gameplay Demo Example of the trained AI playing Space Waves - showing real-time obstacle detection and avoidance

๏ฟฝ๐Ÿ—๏ธ Architecture

Neural Network (SpaceWavesNN)

  • 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)

Computer Vision Pipeline (SpaceWavesVision)

  • 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

Training Algorithm

  • 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

๐Ÿ“‹ Requirements

System Requirements

  • OS: Windows (uses pyautogui for keyboard control)
  • Python: 3.10 or higher
  • GPU: CUDA-compatible GPU recommended for faster training

Dependencies

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
]

๐Ÿš€ Installation

  1. Clone the repository:

    git clone <repository-url>
    cd AI_ML
  2. Set up Python environment:

    # Using uv (recommended)
    uv venv
    uv pip install -e .
    
    # Or using pip
    pip install -e .
  3. 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

๐ŸŽฎ Usage

Training the AI

  1. Open Space Waves game in your browser
  2. Position the game window according to the monitor settings in project_settings.py
  3. 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

Configuration

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
    }

Processing Recorded Gameplay

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"
)

๐Ÿ“ Project Structure

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

๐Ÿง  How It Works

1. Vision Processing

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)

2. Decision Making

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()  # Exploit

3. Learning

The 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()

๐ŸŽ›๏ธ Hyperparameters

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

๐Ÿ”ง Troubleshooting

Common Issues

  1. Game not detected: Adjust screen_monitor settings in project_settings.py
  2. Player detection fails: Modify player_min_area or detection thresholds
  3. Performance issues: Reduce input_frame_size or use CPU-only mode
  4. Keyboard control not working: Ensure pyautogui has proper permissions

Debug Mode

Enable debug visualization:

vision.process_video_file(video_path, debug=True)  # Step through frames
vision.process_from_screen(display_map=True)       # Show real-time processing

๐Ÿ“Š Performance

The 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

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

๐Ÿ“„ License

This project is open source. Please check the license file for details.

๐Ÿ™ Acknowledgments

  • 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.

About

AI agent that plays the Space Waves browser game using Deep Q-Learning and computer vision. Includes real-time screen processing, obstacle detection, automated keyboard control, and a Double DQN neural network trained entirely through gameplay.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages