How tomasis7/snake-game-2d-typescript-oop works
Project type and purpose
• This is a single-page, front-end browser game built in TypeScript using the p5.js canvas library. There is no server or database backend—everything runs in the user’s browser. The build tool is Vite, producing a bundled JS file and static HTML/assets.
Main components of the system
a. Development environment
• Node.js/npm – package management and script runner (npm run dev, npm run build).
• Vite dev server – serves the app locally, watches TS files, hot-reloads.
• TypeScript compiler – compiles .ts to .js.
b. Build artifact
• bundle.js (in /build or /dist) – the compiled and minified game code plus p5.js.
• index.html – the shell that loads bundle.js and bootstraps the canvas.
• assets folder – fonts, images, sounds, music.
c. Runtime (Browser)
• HTML5 Canvas – drawing surface managed by p5.js.
• Game engine modules (in /src):
– Entry point: main.ts → initializes Game.
– Game loop: game.ts → orchestrates screens, input, entity updates.
– Screens/UI: startmenu.ts, gamescreen.ts, interactionscreen.ts, gameOverScreen.ts.
– Entities: player.ts, ghost.ts, plant.ts, star.ts, heart.ts, block.ts, winBlock.ts, tetrisBlocks.ts.
– Managers/factories: collisionmanager.ts, scoreManager.ts, levelfactory.ts, countdown.ts.
d. External libraries
• p5.js and p5.sound (for audio).
• Type definitions from global.d.ts.
Relationships and data flow
• Dev tools → (TS compile/Vite) → Build artifact
• Browser loads index.html → bundle.js + assets
• p5.js runtime calls main.setup() → instantiates Game
• User input (keyboard/mouse) → event handlers in game.ts → affects entity properties
Architectural patterns and design principles
• Modular code: each game concept (screen, entity, manager) in its own file
• Game loop / event-driven architecture (update + render per frame)
• Factory pattern (levelfactory) for dynamic level composition
• Manager classes for collision and scoring concerns
• Separation of concerns: UI screens vs. game logic vs. asset management
Capturing essential details
• Asset retrieval (e.g., loadSound() in global.d.ts)
• How the level factory injects entities into the gameboard
• Collision manager linking Entities to Game logic
• ScoreManager feeding back into UI screens (e.g., game-over / win screen)
• That all modules live in the browser—no server calls
Final advice
• High level (Dev → Build → Runtime)
• Mid level (Game loop → Screens/Entities/Managers → Canvas/Audio)