Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

🐍 Snake Game

A pixel-art Snake game built with React, Vite and Firebase. Play as a guest or sign up to save your high score, track your best run and compete on a global leaderboard.

Features

  • Smooth gameplay: the snake glides between cells with CSS transitions, the head rotates with the direction of travel, and fruits pop in with a small animation.
  • Responsive controls: keyboard (WASD / arrow keys) on desktop and swipe gestures on mobile. Inputs are queued, so quick turns are never lost and the snake can't reverse into itself.
  • Three fruits worth 1, 2 and 3 points, each tracked separately.
  • Accounts: email and password sign up / log in with Firebase Authentication.
  • High score and profile: your best score and the fruits eaten in that run are stored in Firestore.
  • Leaderboard: global top 10 with medals for the top three players.
  • Guest mode: play without an account (scores aren't saved).
  • Responsive UI: the board scales to the screen, and on small screens the profile and leaderboard open as overlays.
  • Respects prefers-reduced-motion.

Tech stack

Area Tools
UI React 19, React Router 7
Styling Tailwind CSS 4, custom CSS animations
Build Vite
Backend Firebase Authentication and Cloud Firestore
Notifications React Toastify
Linting ESLint 10

Getting started

Prerequisites

  • Node.js 20 or newer
  • A Firebase project with Authentication (Email/Password) and Cloud Firestore enabled

Installation

git clone https://github.com/r0b-3rt/snake_game_robby.git
cd snake_game_robby
npm install
npm run dev

The app runs at http://localhost:5173.

Firebase setup

  1. Create a Firebase project and add a Web app.
  2. Enable Authentication → Sign-in method → Email/Password.
  3. Create a Firestore Database.
  4. Put your web app config in src/auth/firebase.js.
  5. Publish the security rules (see below).

Scripts

Command Description
npm run dev Start the dev server with HMR
npm run build Create a production build in dist/
npm run preview Preview the production build locally
npm run lint Run ESLint

Controls

Action Keyboard Touch
Up W / ↑ Swipe up
Down S / ↓ Swipe down
Left A / ← Swipe left
Right D / → Swipe right

Security rules

The game runs in the browser, so a client can never be fully trusted. firestore.rules limits what a player can write:

  • Profiles are readable by everyone (needed for the leaderboard) and contain only userName, highScore and bestFoodStats. Emails are not stored there.
  • A user can create and update only their own document, and cannot delete it.
  • A new high score must be higher than the previous one.
  • The score must equal 1·a + 2·b + 3·c for the fruit counts a, b, c that were saved with it.
  • At most 143 fruits can be eaten (a 12×12 board minus the starting cell), so the maximum score is 429.

Publish the rules with the Firebase CLI:

npx firebase-tools login
npx firebase-tools deploy --only firestore:rules --project <your-project-id>

Limitation: without a server there is no way to prove that a score was really played. These rules block impossible scores, but not a plausible score sent by someone who knows the API. For strict validation, a Cloud Function would have to replay the game on the server.

Project structure

src/
├── auth/            Firebase initialisation
├── components/      Board, snake, food, scoreboard, panels, game-over modal
├── game/
│   ├── constants.js   Grid size, speed, fruit types
│   ├── gameLogic.js   One step of the game (movement, collisions, eating)
│   ├── helpers.js     Random food placement and collision helpers
│   └── useSnakeGame.js  Game loop, input queue and state (React hook)
├── pages/           Home, Login, Register, Game
├── services/        Firestore access (profile, high score, leaderboard)
├── App.jsx          Routing and auth state
└── index.css        Tailwind theme and animations
firestore.rules      Firestore security rules
public/images/       Pixel-art assets

How the game works

The game loop runs on a fixed interval (GAME_SPEED, 150 ms). Each tick takes the next queued direction, advances the snake one cell, then checks for walls, self-collision and food. Rendering is separate from logic: positions are applied with CSS transform and a transition equal to the tick length, which makes the movement continuous. Rotation is applied on an inner element without a transition, so segments never spin the long way around when the snake turns.

Deployment

npm run build

Upload the dist/ folder to any static host (Firebase Hosting, Netlify, Vercel, GitHub Pages). Remember to add your deployed domain under Authentication → Settings → Authorized domains.

Possible improvements

  • Server-side score validation: replay the game in a Cloud Function so scores can't be forged (needs the Firebase Blaze plan).
  • Difficulty levels: selectable speeds, or a speed that increases as the snake grows.
  • Pause and start screen: pause with Space / Esc, and wait for the first input before the snake starts moving.
  • Sound effects and music with a mute toggle.
  • Mobile controls: an optional on-screen D-pad next to swipe gestures.
  • Profile customisation: change username, avatar and snake skin.
  • Extra game modes: walls that wrap around, obstacles, power-ups or timed fruits.
  • Richer stats: games played, total fruits eaten and a per-user score history.
  • Password reset and email verification.
  • Tests: unit tests for the game logic (gameLogic.js, helpers.js) and Firestore rules tests with the emulator.
  • Performance: code-splitting the Firebase bundle to shrink the initial load.

Author

Robby

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages