A fully on-chain competitive snake game built with Dojo and deployed on Starknet. Race against AI opponents, collect power-ups, and compete for the top spot on the leaderboard across multiple difficulty modes!
Snake Race is a multiplayer-style snake game where you compete against an AI opponent. The goal is to:
- Collect apples to grow and increase your score
- Race against time (2-minute rounds)
- Beat the AI by having a higher score when time runs out
- Win bonus: Your score doubles when you win, and your highscore is updated immediately!
- Avoid hitting walls or yourself (your score resets to 0 on collision)
- Real-time Snake Gameplay: Classic snake mechanics with smooth controls
- AI Opponent: Smart AI that adapts to difficulty settings
- Power-ups System:
- Double Points: Doubles your score per apple for 10 seconds
- Freeze: Freezes the AI for 10 seconds, giving you an advantage
- Score System: Points for eating apples, with bonus multipliers
- Time Limit: 2-minute rounds for fast-paced gameplay
- Easy Mode: Slower AI speed (1.2s per move) - Perfect for beginners
- Medium Mode: Balanced AI speed (0.9s per move) - Good challenge
- Hard Mode: Fast AI speed (0.45s per move) + Smart AI that chases power-ups
- Hard AI can collect power-ups and use them against you!
- Double Points doubles AI's score too
- Freeze power-up will freeze YOUR movement when AI collects it
- Per-Mode Leaderboards: Separate leaderboards for Easy, Medium, and Hard modes
- Top 10 Rankings: Compete for top positions across all difficulty levels
- Real-time Updates: Leaderboard updates automatically after each game
- Highscore Tracking: Personal best scores tracked per difficulty mode
- Fully On-Chain: All game state stored on Starknet blockchain
- Real-time Sync: Torii indexer provides instant state updates
- Responsive UI: Beautiful, modern interface that works on all screen sizes
- Wallet Integration: Cartridge Controller for seamless wallet management
This project is built in two main parts:
Dojo smart contracts deployed on Starknet that manage all game state:
-
Models (
src/models.cairo):PlayerState: Tracks player score, alive status, powerups, and modePosition: Player/AI snake positions and lengthsApple: Apple spawn locationPowerUp: Power-up spawn location and typeHighscore: Player highscores per difficulty modeLeaderboardEntry: Top 10 leaderboard entries per mode
-
Systems (
src/systems/actions.cairo):start: Initialize game with selected AI modemove: Handle player movementeat_apple: Increase score and grow snakeactivate_powerup: Apply power-up effectsupdate_highscore: Update player's best scoredouble_score_on_win: Double score when player winsclear_powerup: Clear expired power-up states
Frontend application that interacts with the contracts:
- HTML/CSS/JavaScript: Vanilla frontend (no React)
- Tailwind CSS: Modern, responsive styling
- Dojo SDK: Connects to world contract and Torii indexer
- Cartridge Controller: Wallet connection and transaction signing
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher) and pnpm package manager
- asdf (version manager) - for installing Dojo toolchain
- Google Chrome (recommended for Cartridge Controller support)
Install the Dojo toolchain using the official installer:
curl -L https://install.dojoengine.org | bashAfter installation, restart your terminal or run:
source ~/.bashrc # or ~/.zshrc depending on your shellVerify installation:
sozo --version
katana --version
torii --versioncd dojo-introcd client
pnpm install
cd ..From the contracts directory, run the development script:
cd contracts
scarb run devThis script will:
- Start Katana: Local Starknet development network
- Build Contracts: Compile Dojo contracts
- Deploy World: Deploy the world contract and all systems
- Start Torii: Start the indexing service on
http://localhost:8080
You should see output like:
๐ Starting Katana...
โ
Katana started
๐จ Building contracts...
โ
Contracts built
๐ Deploying world...
โ
World deployed
๐ก Starting Torii...
โ
Torii started
Keep this terminal running!
Open a new terminal and run:
cd client
pnpm run devThe client will start on https://localhost:5173 (HTTPS required for Cartridge Controller).
- Open your browser and navigate to
https://localhost:5173 - Click "Connect Wallet" to connect with Cartridge Controller
- Select your AI difficulty mode (Easy, Medium, or Hard)
- Click "Start Game" to begin
- Use arrow keys or WASD to control your snake
- Collect apples, avoid walls/yourself, and beat the AI!
- Arrow Keys or WASD: Move your snake
- Spacebar: Pause/Resume game
- H: Toggle help modal
- L: Toggle leaderboard modal
- Collect Apples: Each apple increases your score by 10 points (20 with Double Points active)
- Power-ups:
- ๐ข Double Points: Active for 10 seconds, doubles your score per apple
- ๐ต Freeze: Freezes AI for 10 seconds (or you if Hard AI collects it)
- Winning: Player wins if they have a higher score than AI when time runs out
- Losing: Player loses if:
- They hit a wall (score resets to 0)
- They hit themselves (score resets to 0)
- They have lower score than AI when time runs out
- Score Doubling: When you win, your score is automatically doubled and your highscore is updated!
- Regular apple: 10 points
- Apple with Double Points: 20 points
- Score doubles on win: 2x multiplier
- Highscore tracked separately for each difficulty mode
dojo-intro/
โโโ contracts/ # Dojo smart contracts
โ โโโ src/
โ โ โโโ models.cairo # Data models (PlayerState, Position, etc.)
โ โ โโโ systems/
โ โ โ โโโ actions.cairo # Game logic systems
โ โ โโโ lib.cairo # Library exports
โ โโโ Scarb.toml # Cairo package configuration
โ โโโ katana.toml # Katana devnet configuration
โ โโโ torii_dev.toml # Torii indexer configuration
โ โโโ dev.sh # Development script
โ
โโโ client/ # Frontend application
โ โโโ index.html # Main HTML file
โ โโโ game.js # Game logic and Dojo integration
โ โโโ controller.js # Cartridge Controller configuration
โ โโโ style.css # Custom styles
โ โโโ tailwind.config.js # Tailwind CSS configuration
โ โโโ package.json # Node.js dependencies
โ
โโโ README.md # This file
cd contracts
sozo buildcd contracts
sozo testThe dev.sh script handles deployment automatically. For manual deployment:
cd contracts
sozo build
sozo migrate --name devTorii provides GraphQL and REST APIs for querying on-chain data:
- GraphQL:
http://localhost:8080/graphql - REST API:
http://localhost:8080/entities
- The Cartridge Controller creates its own account that may need manual funding
- You can fund it using Katana's prefunded accounts or faucet
- Ensure Katana is running:
katana --allowed-origins "*" - Check that ports 5050 (Katana) and 8080 (Torii) are available
- Ensure you're using HTTPS (
https://localhost:5173) - Chrome is recommended for Cartridge Controller support
- Check browser console for connection errors
- Restart Torii:
pkill toriithen runscarb run devagain - Wait a few seconds after transactions for Torii to index
- This is a development/demo project for learning Dojo
- Never use production secrets or keys in development
- Katana devnet uses deterministic accounts for testing
- โ Fully On-Chain: Every game action is a blockchain transaction
- โ Real-time Updates: Torii indexer provides instant state sync
- โ Multiple Difficulty Modes: Separate leaderboards for each mode
- โ Smart AI: Hard mode AI can collect and use power-ups
- โ Responsive Design: Beautiful UI that works on all devices
- โ Score Doubling: Exciting win mechanic that doubles your score
Happy Gaming! ๐ฎ๐