How imskr/flappy-bird-ai works
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).