Skip to content

nhamngo29/Snake-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ Snake Game

A modern, sleek reimagining of the classic Snake game โ€” built with React, TypeScript, and AI-assisted development.

Built with AI React TypeScript Vite


๐ŸŽฎ Introduction

This is a modern take on the classic Snake game, featuring a clean, gradient-rich UI with smooth animations. Built entirely using React 19, TypeScript, and Vite with Tailwind CSS v4 and shadcn/ui components, this project showcases how AI-assisted development can rapidly prototype and polish a complete game experience.

The game includes all the nostalgia of the original โ€” eat food, grow longer, avoid walls and yourself โ€” with modern UX enhancements like dynamic difficulty, high score persistence, and keyboard-friendly controls.


๐Ÿง  Built with AI Vibe Coding

This project was designed, architected, and implemented using AI Vibe Coding โ€” an AI-assisted development approach where:

  • AI handles the heavy lifting: Code generation, pattern implementation, refactoring, and bug detection
  • Human provides direction: Feature requirements, design decisions, and quality oversight
  • Iterative collaboration: Continuous feedback loop between developer intent and AI execution

How AI Was Used

โœ… Architecture design โ€” Component structure, state management with custom hooks, game loop patterns
โœ… Code implementation โ€” TypeScript interfaces, React components, game logic algorithms
โœ… UI/UX polish โ€” shadcn/ui integration, Tailwind styling, responsive layouts
โœ… Problem solving โ€” Handling stale closures in game loops, optimizing re-renders with memo()
โœ… Documentation โ€” Inline comments, type definitions, and this README

This approach allowed rapid development while maintaining clean, production-quality code.


โœจ Features

Gameplay

  • ๐ŸŽฏ Classic Snake mechanics โ€” Navigate, eat, grow, survive
  • ๐Ÿ“ˆ Dynamic difficulty โ€” Speed increases progressively as you score points
  • ๐Ÿ† High score tracking โ€” Your best score persists across sessions (localStorage)
  • โธ๏ธ Pause & Resume โ€” Take a break anytime with Space or Escape

UI/UX

  • ๐ŸŽจ Modern design โ€” Gradient backgrounds, smooth animations, rounded corners
  • ๐Ÿ–ผ๏ธ Clean grid layout โ€” 20ร—20 game board with subtle grid lines
  • ๐Ÿ“ฑ Responsive components โ€” Built with shadcn/ui + Radix primitives
  • ๐ŸŽญ Game state overlays โ€” Visual feedback for idle, paused, and game over states

Technical

  • โšก Vite HMR โ€” Instant hot module replacement during development
  • ๐ŸŽฃ Custom React hooks โ€” useSnakeGame encapsulates all game logic
  • ๐Ÿ”„ Ref-based direction system โ€” Prevents stale closure bugs in game loop
  • ๐Ÿ’พ LocalStorage persistence โ€” High scores survive page refreshes

๐Ÿ›  Tech Stack

Technology Purpose
React 19 UI library with modern concurrent features
TypeScript 5.9 Type-safe development
Vite 7 Lightning-fast build tool with HMR
Yarn 4 Modern package manager
Tailwind CSS v4 Utility-first CSS framework (new CSS-first config)
shadcn/ui Accessible component system (Radix UI + CVA)
Lucide React Beautiful, consistent icons

๐Ÿš€ Getting Started

Prerequisites

  • Node.js 18+ (20+ recommended)
  • Yarn 4.x (included via Corepack)

Installation

  1. Clone the repository

    git clone <your-repo-url>
    cd snake-game
  2. Install dependencies

    yarn
  3. Start development server

    yarn dev
  4. Open in browser

    http://localhost:5173
    

Build for Production

yarn build      # TypeScript check + Vite build
yarn preview    # Preview production build locally

Linting

yarn lint       # Run ESLint with React 19 rules

๐ŸŽฎ How to Play

Controls

Key Action
Arrow Keys (โ†‘ โ†“ โ† โ†’) Move the snake
Space / Escape Pause / Resume game
Click or Any Arrow Key Start game when idle

Game Rules

  1. Eat the food (๐ŸŽ) to grow longer and score points (+10 per food)
  2. Avoid collisions with walls or your own body
  3. Speed increases as your score grows โ€” stay sharp!
  4. Beat your high score โ€” it's saved automatically

Scoring System

  • 10 points per food eaten
  • Dynamic speed โ€” Game speeds up every time you eat (from 150ms to minimum 50ms per tick)
  • High score โ€” Displayed at the top, persists in localStorage

๐Ÿ“ Project Structure

snake-game/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ components/          # React components
โ”‚   โ”‚   โ”œโ”€โ”€ ui/              # shadcn/ui primitives (Button, Card, etc.)
โ”‚   โ”‚   โ”œโ”€โ”€ GameBoard.tsx    # Main game grid + overlays
โ”‚   โ”‚   โ”œโ”€โ”€ Snake.tsx        # Snake rendering component
โ”‚   โ”‚   โ”œโ”€โ”€ Food.tsx         # Food rendering component
โ”‚   โ”‚   โ”œโ”€โ”€ ScoreBoard.tsx   # Score display
โ”‚   โ”‚   โ””โ”€โ”€ Controls.tsx     # Control buttons
โ”‚   โ”œโ”€โ”€ hooks/
โ”‚   โ”‚   โ””โ”€โ”€ useSnakeGame.ts  # Core game logic hook
โ”‚   โ”œโ”€โ”€ utils/
โ”‚   โ”‚   โ”œโ”€โ”€ gameLogic.ts     # Pure game functions (movement, collision, etc.)
โ”‚   โ”‚   โ””โ”€โ”€ storage.ts       # localStorage wrapper
โ”‚   โ”œโ”€โ”€ lib/
โ”‚   โ”‚   โ””โ”€โ”€ utils.ts         # cn() utility for class merging
โ”‚   โ”œโ”€โ”€ App.tsx              # Root component
โ”‚   โ””โ”€โ”€ main.tsx             # Entry point
โ”œโ”€โ”€ public/                  # Static assets
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ copilot-instructions.md  # AI coding agent guidelines
โ””โ”€โ”€ package.json             # Dependencies & scripts

๐ŸŒฑ Future Improvements

Potential enhancements for v2.0:

  • ๐ŸŽต Sound effects โ€” Eating, game over, high score sounds
  • ๐ŸŒˆ Themes โ€” Dark mode, color schemes (green, blue, neon)
  • ๐Ÿ“ฑ Mobile controls โ€” Touch-based swipe gestures
  • ๐Ÿ… Achievements โ€” Unlock badges for milestones
  • ๐ŸŒ Leaderboard โ€” Global high scores with backend integration
  • ๐ŸŽš๏ธ Difficulty levels โ€” Easy/Medium/Hard presets
  • ๐Ÿ Power-ups โ€” Slow-motion, invincibility, double points
  • ๐ŸŽฌ Animations โ€” Snake slithering, food spawning effects

๐Ÿ“œ License & Credits

This project is a personal learning project built to explore modern React patterns, TypeScript best practices, and AI-assisted development workflows.

Credits


๐Ÿค Contributing

This is primarily a showcase project, but suggestions and feedback are welcome! Feel free to:

  • ๐Ÿ› Open issues for bugs
  • ๐Ÿ’ก Suggest features
  • ๐Ÿ”€ Submit pull requests

๐Ÿ™ Acknowledgments

Special thanks to the AI coding tools that made this rapid development possible, and to the open-source community for the incredible frameworks and libraries that power modern web development.


Enjoy the game! ๐Ÿ๐ŸŽฎ

Made with โค๏ธ and ๐Ÿค– | View on GitHub

Releases

No releases published

Packages

 
 
 

Contributors