# imskr/flappy-bird-ai architecture

> Architecture diagram of the GitHub repository imskr/flappy-bird-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/imskr/flappy-bird-ai
- Repository: https://github.com/imskr/flappy-bird-ai

## Overview

##### Project type and purpose
- This is a browser-based single-page JavaScript application that implements the classic “Flappy Bird” game, but with an AI “brain” driving the birds.
- The AI is built from scratch using a simple feed-forward neural network and a genetic algorithm for evolving populations of birds.

##### File structure
- index.html: Hosts the canvas, links CSS, Processing.js (ga.js), and main scripts.
- assets/: Static assets (images, screenshots).
- css/style.css: Page styling.
- ga.js: Likely includes Processing.js or Genetic Algorithm helper code.
- bird.js, pipe.js, sketch.js: Core game-loop and entity logic.
- neuralnetwork/: Contains the AI implementation
- matrix.js – linear algebra utilities
- nn.js – neural network forward-pass and mutation logic

##### README for additional insights
- Third-party libraries: Processing.js for canvas drawing, plain JavaScript.
- The neural network + genetic algorithm drives decision-making (flap or not).
- References indicate a population-based evolutionary loop.

a. Main components
- User Interface / Renderer (Processing.js + Canvas)
- Game Engine (sketch.js orchestrates game loop, time steps)
- Entity Modules
– Bird module (bird.js)
– Pipe module (pipe.js)
- AI Brain
– Neural Network (nn.js)
– Matrix utilities (matrix.js)
- Genetic Algorithm Controller (selection, crossover, mutation routines)
- Data Store (in-memory JS arrays representing populations)
- External Libraries (Processing.js)

b. Relationships and interactions
- The Game Engine drives the main loop:
– On each frame it updates Bird and Pipe positions
– It queries each Bird’s AI Brain for “flap” decisions
– It renders all entities via the UI/Renderer
- When all birds lose, the Genetic Algorithm Controller
– Collects fitness scores
– Performs selection & mutation on Neural Networks
– Seeds the next generation back into the Game Engine
- Matrix utilities are invoked by the Neural Network for forward passes and weight updates.
- External library Processing.js is used by the UI layer for rendering primitives on canvas.

c. Architectural patterns and design principles
- Event-driven game loop (tick/update/render cycle)
- Separation of concerns:
– Rendering vs. Game Logic vs. AI logic
- Population-based evolutionary computing
- Modular JS files for each domain

d. Key technologies, frameworks, libraries
- JavaScript (ES5/ES6 modules)
- Processing.js (canvas abstraction)
- Custom Neural Network & Genetic Algorithm

##### Capturing essential architectural elements
- File-to-component mapping for clarity (e.g., nn.js → Neural Network Module).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI / Rendering Layer
    subgraph "UI / Rendering"
        UI[index.html]:::ui
        CSS[css/style.css]:::ui
        Processing["Processing.js"]:::external
    end

    %% Game Logic Layer
    subgraph "Game Logic"
        Engine[sketch.js]:::logic
        Bird[bird.js]:::logic
        Pipe[pipe.js]:::logic
    end

    %% AI Logic Layer
    subgraph "AI Logic"
        NN[neuralnetwork/nn.js]:::ai
        GA[ga.js]:::ai
    end

    %% Utilities
    subgraph "Utilities"
        Matrix[neuralnetwork/matrix.js]:::util
    end

    %% Static Assets
    Assets[assets/]:::external

    %% Data Flows and Interactions
    UI -->|loads assets| Assets
    UI -->|draw calls| Processing
    Processing -->|renders canvas| UI
    Engine -->|requests render| UI
    Engine -->|updates positions| Bird
    Engine -->|updates positions| Pipe
    Engine -->|"sensor inputs"| NN
    NN -->|"matrix ops"| Matrix
    NN -->|"flap decision"| Engine
    Engine -->|"fitness scores"| GA
    GA -->|"new weights"| NN

    %% Click Events
    click UI "https://github.com/imskr/flappy-bird-ai/blob/master/index.html"
    click CSS "https://github.com/imskr/flappy-bird-ai/blob/master/css/style.css"
    click Processing "https://github.com/imskr/flappy-bird-ai/blob/master/ga.js"
    click Engine "https://github.com/imskr/flappy-bird-ai/blob/master/sketch.js"
    click Bird "https://github.com/imskr/flappy-bird-ai/blob/master/bird.js"
    click Pipe "https://github.com/imskr/flappy-bird-ai/blob/master/pipe.js"
    click NN "https://github.com/imskr/flappy-bird-ai/blob/master/neuralnetwork/nn.js"
    click Matrix "https://github.com/imskr/flappy-bird-ai/blob/master/neuralnetwork/matrix.js"
    click GA "https://github.com/imskr/flappy-bird-ai/blob/master/ga.js"
    click Assets "https://github.com/imskr/flappy-bird-ai/tree/master/assets/"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085
    classDef logic fill:#d4edda,stroke:#155724,color:#155724
    classDef ai fill:#fff3cd,stroke:#856404,color:#856404
    classDef util fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef external fill:#f5c6cb,stroke:#721c24,color:#721c24
```
