A polished, feature-rich implementation of the classic Snake game built with C++ and SFML. This project showcases clean code architecture, smooth gameplay mechanics, and engaging visual effects.
Featuring particle effects, smooth animations, and polished gameplay
- Classic Snake Mechanics - Grow by eating food, avoid collisions
- Toggleable Wall Collision - Play with or without boundary walls
- Progressive Difficulty - Speed increases every 5 points
- Screen Wrapping - Snake wraps around edges when walls are disabled
- Persistent High Score - Your best score is saved between sessions
- ๐ Particle System - Sparkle effects when eating food
- ๐ฅ Death Explosion - Dramatic particle burst on collision
- ๐ Score Popups - Floating score indicators
- ๐จ Smooth Animations - Polished transitions and effects
- ๐ฎ Main Menu - Clean, intuitive navigation
- โ๏ธ Settings Toggle - Enable/disable walls from menu
- ๐ Instructions Screen - Clear game controls and rules
- โธ๏ธ Pause Functionality - Pause/resume with 'P' key
- ๐ High Score Display - Always visible during gameplay
| Key | Action |
|---|---|
| Arrow Keys | Move snake (Up/Down/Left/Right) |
| P | Pause/Resume game |
| Enter | Start game from menu |
| R | Restart from game over screen |
| M | Return to menu from game over |
| Mouse | Navigate menus and click buttons |
- Object-Oriented Design - Clean separation of concerns
- Component-Based - Modular game systems (Snake, Food, Score, Particles)
- State Management - Robust game state system (Menu, Playing, Game Over, Instructions)
- Memory Optimized - Pre-allocated vectors, efficient particle management
โโโ Snake - Player-controlled entity with collision detection
โโโ Food - Spawning system with grid-based placement
โโโ Game - Core game loop and logic coordinator
โโโ Score - Scoring system with persistent high score
โโโ ParticleSystem - Visual effects and particle physics
โโโ UIManager - Menu system and user interface
โโโ UIButton - Interactive menu buttons
- Predictive Collision Detection - Checks next position before moving
- Smart Food Spawning - Avoids snake body with infinite loop prevention
- Visual Effects - Sparkle effects, death explosion and score floating effects
- Comprehensive Comments - Well-documented code for easy understanding
- SFML 2.5+ - Simple and Fast Multimedia Library
- CMake 3.10+ - Build system generator
- C++17 Compiler - GCC, Clang, or MSVC
- MinGW-w64 (Windows) - For compiling on Windows
- โ Windows (Primary)
- โ Linux
- โ macOS
-
Install Dependencies
# Install MSYS2 from https://www.msys2.org/ # Open MSYS2 MinGW 64-bit terminal pacman -S mingw-w64-x86_64-gcc pacman -S mingw-w64-x86_64-cmake pacman -S mingw-w64-x86_64-sfml
-
Clone and Build
git clone https://github.com/swati048/snake-game.git cd snake-game mkdir build && cd build cmake -G "MinGW Makefiles" .. cmake --build .
-
Run
./snake.exe
-
Install Dependencies
# Ubuntu/Debian sudo apt-get install build-essential cmake sudo apt-get install libsfml-dev # Arch Linux sudo pacman -S base-devel cmake sfml
-
Clone and Build
git clone https://github.com/swati048/snake-game.git cd snake-game mkdir build && cd build cmake .. make
-
Run
./snake
-
Install Dependencies
# Install Homebrew from https://brew.sh/ brew install cmake brew install sfml -
Clone and Build
git clone https://github.com/swati048/snake-game.git cd snake-game mkdir build && cd build cmake .. make
-
Run
./snake
Clean, intuitive menu with wall toggle option
Smooth gameplay with walls enabled and score display
Smooth gameplay with walls disabled and score display
Game over screen displaying score and highscore with restart, menu and exit button
Precise game control and rules instructions
snake-game/
โโโ src/
โ โโโ main.cpp # Entry point
โ โโโ Snake.cpp # Snake entity logic
โ โโโ Food.cpp # Food spawning system
โ โโโ Game.cpp # Game coordinator
โ โโโ Score.cpp # Score management
โ โโโ ParticleSystem.cpp # Visual effects
โ โโโ UIManager.cpp # UI and menu system
โ โโโ UIButton.cpp # Button component
โโโ include/
โ โโโ Snake.hpp
โ โโโ Food.hpp
โ โโโ Game.hpp
โ โโโ Score.hpp
โ โโโ ParticleSystem.hpp
โ โโโ GameState.hpp
โ โโโ UIManager.hpp
โ โโโ UIButton.hpp
โโโ resources/
โ โโโ snake_head.png # Snake head texture
โ โโโ snake_body.png # Snake body texture
โ โโโ food.png # Food texture
โ โโโ background.png # Game background
โ โโโ menu_background.png # Menu background
โ โโโ gameOver_bg.png # Game over background
โ โโโ CherryBombOne-Regular.ttf # Game font
โโโ .vscode/
โ โโโ c_cpp_properties.json # VSCode C++ config
โ โโโ launch.json # Debug configuration
โ โโโ settings.json # CMake settings
โโโ CMakeLists.txt # Build configuration
โโโ highscore.txt # Persistent high score
โโโ README.md # This file
In main.cpp:
const float SNAKE_SPEED = 20.0f; // Change block size/speedIn Game.cpp:
// Speed increases every 5 points by default
if (score.getValue() % 5 == 0) {
speedMultiplier += 0.1f; // Adjust increment
}In Game.cpp:
// Food collection particles
particleSystem.createBurst(position, 15, color); // Adjust count
// Death explosion particles
particleSystem.createExplosion(position, 40, color); // Adjust countIn respective .cpp files:
// Wall color
wall.setFillColor(sf::Color(150, 75, 0)); // Brown
// Particle colors
sf::Color::Yellow // Food particles
sf::Color::Red // Death particles- None currently! ๐
- Sound effects and background music
- Multiple difficulty levels
- Power-ups (speed boost, invincibility, score multiplier)
- Obstacles and special food types
- Leaderboard with player names
- Theme selector (different color schemes)
- Multiplayer mode
- Mobile touch controls
Why SFML?
- Cross-platform compatibility
- Simple API for 2D graphics
- Good performance for particle effects
- Active community support
Why Static Linking?
- Single executable distribution
- No external DLL dependencies
- Easier for portfolio deployment
Particle System Design
- Used vector with reserve() for memory efficiency
- Lifetime-based particle removal
- Physics simulation (velocity, gravity, fade)
- Separate burst and explosion types for variety
Collision Detection
- Predictive checking prevents teleportation bugs
- Separate wall and self-collision logic
- Grid-aligned movement prevents sub-pixel issues
Contributions are welcome! Feel free to:
- Report bugs
- Suggest features
- Submit pull requests
- Improve documentation
Swati Thakur
- GitHub: @swati048
- LinkedIn: Swati Thakur
- Email: thakurswati048@gmail.com
- SFML Team - For the excellent multimedia library
- Cherry Bomb Font - Font used in the game
- Classic Snake Game - Original inspiration from Nokia phones
โญ Star this repository if you found it helpful! โญ
Made with โค๏ธ and C++
