Flappy Bird AI Player

This project implements an AI that plays Flappy Bird automatically using computer vision and decision-making algorithms.

Flappy Bird Game Screenshot

AI Status: Ready

Score: 0

AI Settings

Features:

Python Code:

import cv2
import numpy as np
import pyautogui
import time

class FlappyBirdAI:
    def __init__(self):
        # Initialize game region coordinates
        self.game_region = (0, 0, 800, 600)  # Adjust based on your screen
        
    def find_game_window(self):
        """Locate the game window on screen"""
        # Implementation would use template matching with OpenCV
        pass
        
    def capture_screen(self):
        """Capture the game screen"""
        return pyautogui.screenshot(region=self.game_region)
        
    def detect_bird(self, image):
        """Detect bird position using color thresholding and contour detection"""
        # Convert to HSV color space
        hsv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2HSV)
        
        # Define range for bird color (yellow in Flappy Bird)
        lower_yellow = np.array([20, 100, 100])
        upper_yellow = np.array([30, 255, 255])
        
        # Threshold the HSV image
        mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
        
        # Find contours
        contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        if contours:
            # Get largest contour (bird)
            largest_contour = max(contours, key=cv2.contourArea)
            x, y, w, h = cv2.boundingRect(largest_contour)
            return x + w//2, y + h//2  # Return center point
        return None
        
    def detect_pipes(self, image):
        """Detect upcoming pipes"""
        # Similar to bird detection but looking for green pipes
        pass
        
    def should_jump(self, bird_pos, pipe_pos):
        """Decide whether to jump based on positions"""
        # Simple algorithm: jump when distance to next gap is below threshold
        if bird_pos and pipe_pos:
            distance_to_pipe = pipe_pos[0] - bird_pos[0]
            vertical_distance = abs(pipe_pos[1] - bird_pos[1])
                if distance_to_pipe < (100 - (sensitivity * 10)) and vertical_distance > (20 - sensitivity):
                    return True
        return False
    def play(self, sensitivity=5, speed=5):
        """Main game loop"""
        try:
            self.running = True
            while self.running:
screenshot = self.capture_screen()
                bird_pos = self.detect_bird(screenshot)
                pipe_pos = self.detect_pipes(screenshot)
                
                if self.should_jump(bird_pos, pipe_pos):
                    pyautogui.press('space')
                time.sleep(0.1 - (speed * 0.009))  # Adjust delay based on speed setting
except KeyboardInterrupt:
            print("Stopping AI player")

if __name__ == "__main__":
    ai = FlappyBirdAI()
    ai.play()