An educational browser game for children to learn geometric shapes by placing them on a digital peg board.
- Interactive Peg Board: Snaps shapes perfectly to a grid of pegs.
- Drag & Drop System: Smooth custom drag and drop logic that works directly from the shape panel to the board.
- Collision Detection: Prevents shapes from overlapping or being placed off-board with visual red/green feedback.
- Shape Management: Supports adding, rotating (90 degrees), scoring, and deleting shapes without complex logic errors.
- Audio Feedback: Synthesizes cheerful blips and clicks using the Web Audio API on snap and rotation events. No external files need to be loaded!
- Responsive & Deployable: Easily scalable on web browsers with a modular architecture that requires no build step. Can be deployed straight to GitHub Pages.
project/
│
├── index.html # Main HTML UI structure
├── style.css # Interactive UI styling with CSS Grid/Flexbox
│
├── js/
│ ├── config.js # Game constants (grid sizing, shape SVGs, colors)
│ ├── board.js # Dynamically renders SVG peg holes over the visible area
│ ├── shapes.js # Manages shape state, creation, storage, and DOM updates
│ ├── dragDrop.js # Calculates mouse diffs, creates ghost shapes, updates dragging
│ ├── snap.js # Calculates nearest grid peg coordinates
│ ├── collision.js # Predicts overlap with other pieces or boundaries
│ └── main.js # Connects modules and UI audio/buttons
│
└── README.md # This file
- Download or clone this repository to your computer.
- Open the directory containing the files.
- Simply double-click on
index.htmlto open it in your default web browser. No local server or build tools are required!
The game ensures pieces align perfectly with the pegs by rounding their raw X and Y positions. We accomplish this using:
Math.round(coordinate / spacing) * spacing
When dragging, the piece follows the mouse normally, but its actual applied transform properties run through snap.js first. This forces the geometry to tick firmly into position.
The game uses a lightweight Axis-Aligned Bounding Box (AABB) collision check. Before placing or rotating a block, the engine generates an AABB bounding box for the proposed new shape state based on width, height, x, y, and rotation. It then:
- Validates the box fits strictly within the 0 to
width/heightbounds of theBoard. - Iterates over all other
placedShapesto verify no intersections exist.intersect(rect1, rect2)calculates overlap. Invalid placements show a red aura using CSS filters (invalid-placement) and prevent drop completion.