cppsnek - A Simple Snake Game in C++ with SFML
Snake in C++17 ; uses SFML ; the snake is on fucking crack ; it moves like a fucking mig21
Features
- Classic Snake gameplay with grid-based movement
- Score tracking and milestone celebrations
- Programmatically generated sound effects (no external audio files needed)
- Simple pixel-art style graphics
- Responsive controls (WASD or arrow keys)
- Adjustable game speed that increases with score
- Visual grid overlay
- Pause/resume functionality (Space key)
Technical Details
- Language: C++17
- Libraries:
- SFML 2.5+ (Graphics, Window, System, Audio modules)
- Pattern: Object-oriented design with separate classes for game logic, rendering, and sound
- Notable Techniques:
- Programmatic sound generation
- Pixel pattern-based sprite creation
- Vertex arrays for grid rendering
- State management for game flow
Building the Project
Requirements
- CMake (version 3.10+)
- SFML development libraries (libsfml-graphics, libsfml-window, libsfml-system, libsfml-audio)
- C++17 compatible compiler
Build Steps
mkdir build && cd build
cmake ..
make
Running
./cppsnek
Controls
- W - Move Up
- A - Move Left
- S - Move Down
- D - Move Right
- Space - Pause/Resume game
Code Structure
.
├── main.cpp # Main game implementation
├── CMakeLists.txt # Build configuration
└── README.md # This file
Key classes:
Game: Main game loop and state managementSoundManager: Handles all audio generation and playbackNumericSprite: Creates sprites from numeric patternsSnakeSegment: Simple struct for snake body parts
Customization
You can easily modify:
- Game speed by changing the
speedinitial value inGameconstructor - Grid appearance by modifying the
createGrid()method - Snake and fruit visuals by editing the pattern arrays
- Sound characteristics in the
SoundManagerclass
Known Issues/Limitations
- Sound generation is CPU-intensive during initialization
- No proper game menu or start screen
- High scores aren't persisted between sessions
- Theme song generation is currently commented out
Learning Objectives
This project demonstrates:
- SFML basics (window management, rendering, input handling)
- Game loop implementation
- Procedural audio generation
- Simple collision detection
- State management in games
- Object-oriented game design
Description
Languages
C++
96.1%
CMake
3.9%
