An intelligent CNN-based system that automatically plays Subway Surfers by analyzing screenshots and making real-time decisions. The bot adapts to different game speeds and achieves distances up to 20,000 meters!
- Features
- How It Works
- Installation
- Usage
- Project Structure
- Model Architecture
- Performance
- Configuration
- Contributing
- Authors
- ๐ฏ Real-time Decision Making: Analyzes game screenshots and executes actions in milliseconds
- โก Adaptive Speed System: Uses different models for slow/medium/fast game phases
- ๐ฎ Automated Gameplay: Achieves 20,000+ meters consistently
- ๐ Performance Monitoring: Real-time confidence tracking and decision logging
- ๐ฅ๏ธ User-friendly GUI: Easy-to-use interface for data collection and bot control
- ๐ Training Pipeline: Complete system for collecting data and training models
- ๐ง Lane Tracking: Intelligent position awareness system
The system uses a multi-stage approach:
- Screenshot Capture: Takes real-time screenshots of the game area
- CNN Analysis: Processes images through trained convolutional neural networks
- Decision Making: Predicts optimal actions (LEFT, RIGHT, UP, DOWN, NONE)
- Action Execution: Simulates keyboard inputs to control the game
- Adaptive Learning: Switches between speed-specific models as game accelerates
Screenshot โ CNN Model โ Action Prediction โ Keyboard Input โ Game Control
- Python 3.8+
- CUDA-compatible GPU (recommended)
- Windows OS (for keyboard simulation)
- Clone the repository
git clone https://github.com/yourusername/subway-surfers-ai.git
cd subway-surfers-ai- Install dependencies
pip install -r requirements.txt- Verify installation
python -c "import tensorflow as tf; print('TensorFlow version:', tf.__version__)"tensorflow>=2.8.0
pillow>=8.0.0
keyboard>=1.13.0
matplotlib>=3.5.0
numpy>=1.21.0
scikit-learn>=1.0.0
tkinter (usually included with Python)
pyautogui>=0.9.0
Collect training data by playing the game manually:
python screen_collector/data_collector.pywControls:
- Select screen area when prompted
- Use Arrow Keys or WASD to capture screenshots while playing
- Space: Pause/Resume collection
- R: Reset timer
- Shift+Esc: Exit
Train CNN models for different game speeds:
python src/model_manager.pyThis will train three separate models:
- Slow model: For 0-30 seconds gameplay
- Medium model: For 30-120 seconds gameplay
- Fast model: For 120+ seconds gameplay
Start the automated gameplay:
python subway_bot.pywBot Controls:
- Pause/Resume: Control bot execution
- Reset: Reset game timer
- Enable DC: Enable double-click feature
The bot displays real-time information:
- Current game time and speed category
- Model predictions and confidence levels
- Player lane position tracking
subway-surfers-ai/
โโโ ๐ screen_collector/
โ โโโ data_collector.pyw # Data collection interface
โ โโโ screens/ # Training data storage
โ โ โโโ slow/ # Slow phase data
โ โ โโโ medium/ # Medium phase data
โ โ โโโ fast/ # Fast phase data
โ โโโ src/
โ โโโ screen_selector.py # Screen area selection
โโโ ๐ src/
โ โโโ data_loader.py # Data preprocessing
โ โโโ model_builder.py # CNN architecture
โ โโโ trainer.py # Training pipeline
โ โโโ data_analyzer.py # Dataset analysis
โ โโโ confusion_visualizer.py # Results visualization
โ โโโ evaluator.py # Model evaluation
โโโ ๐ utils/
โ โโโ predict.py # Prediction utilities
โ โโโ get_speed_category.py # Speed categorization
โโโ ๐ trained_models/ # Saved model files
โโโ config.py # Configuration settings
โโโ subway_bot.pyw # Main bot interface
โโโ requirements.txt # Dependencies
The CNN architecture consists of:
Sequential([
Conv2D(32, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu'),
BatchNormalization(),
MaxPooling2D((2, 2)),
Flatten(),
Dropout(0.5),
Dense(512, activation='relu'),
Dropout(0.3),
Dense(256, activation='relu'),
Dropout(0.2),
Dense(5, activation='softmax') # [DOWN, LEFT, NONE, RIGHT, UP]
])Key Features:
- Input Size: 224x224x3 RGB images
- Output Classes: 5 actions (DOWN, LEFT, NONE, RIGHT, UP)
- Regularization: Dropout and BatchNormalization
- Optimizer: AdamW with learning rate 0.0001
- Training Accuracy: 94.2%
- Validation Accuracy: 89.7%
- Test Accuracy: 87.3%
- Inference Time: ~15ms per prediction
- Maximum Distance: ~20,000 meters
- Average Performance: 15,000-18,000 meters
- Success Rate by Phase:
- Early Game (0-30s): 98%
- Mid Game (30-120s): 87%
- Late Game (120s+): 72%
| Action | Precision | Recall |
|---|---|---|
| LEFT | 0.91 | 0.89 |
| RIGHT | 0.90 | 0.92 |
| UP | 0.84 | 0.87 |
| DOWN | 0.86 | 0.83 |
| NONE | 0.88 | 0.90 |
Modify config.py to customize behavior:
# Model parameters
IMG_SIZE = (224, 224) # Input image size
BATCH_SIZE = 16 # Training batch size
LEARNING_RATE = 0.0001 # Learning rate
# Speed thresholds (seconds)
SLOW_THRESHOLD = 30 # Slow to medium transition
MEDIUM_THRESHOLD = 120 # Medium to fast transition
# Bot settings
PRECISION_THRESHOLD = 0.5 # Minimum confidence for action
SLOW_DELAY = 0.5 # Delay between actions (slow phase)
MEDIUM_DELAY = 0.3 # Delay between actions (medium phase)
FAST_DELAY = 0.1 # Delay between actions (fast phase)Train with specific parameters:
from src.model_manager import train_speed_model
# Train custom model
model, history = train_speed_model(
speed_category="slow",
data_dir="custom_data/",
model_path="models/custom_model.keras"
)Check data distribution:
from src.data_analyzer import analyze_dataset
stats = analyze_dataset("screen_collector/screens/slow", CLASS_NAMES)Evaluate model performance:
from src.evaluator import evaluate_model
metrics = evaluate_model(model, test_data)
print(f"Accuracy: {metrics['accuracy']:.4f}")- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow PEP 8 style guidelines
- Add docstrings to new functions
- Include tests for new features
- Update documentation as needed
1. Screenshot capture not working
# Make sure you have proper permissions
# Run as administrator if needed2. Model loading errors
# Ensure models are trained first
python src/model_manager.py3. GPU memory issues
# Reduce batch size in config.py
BATCH_SIZE = 8 # Instead of 164. Keyboard input not working
# Install keyboard library with admin privileges
pip install keyboard --user- Reinforcement Learning: Integrate RL for self-improvement
- Multi-frame Analysis: Use temporal information for better predictions
- Transfer Learning: Leverage pre-trained vision models
- Cross-platform Support: Extend to mobile platforms
- Real-time Optimization: Further reduce latency
- Advanced GUI: Enhanced monitoring and control interface
This project is licensed under the MIT License - see the LICENSE file for details.
- TensorFlow team for the excellent deep learning framework
- Subway Surfers developers for creating an engaging game
- Computer Vision and Digital Systems Department at Silesian University of Technology
This project demonstrates practical applications of:
- Convolutional Neural Networks in real-time applications
- Computer vision for game automation
- Multi-model adaptive systems
โญ Star this repository if you found it helpful!
๐ Links: