- Modular OOP Architecture: Divided into highly encapsulated classes (
Snake,Food,Board,GameManager). - Cross-Platform Input System: Custom macOS terminal handling using raw input mode, supporting both standard WASD and Arrow Keys (ANSI escape sequences).
- Flicker-Free Rendering: Redraws the board on the fly by resetting the terminal cursor to the top-left rather than clearing the whole screen.
- Score & Level Mechanics: Tracks player score and increases speed as you eat more food.
- Collision Detection: Full wall-boundary and self-collision checks.
run.cpp- The entry point containing themain()function.game_manager.h/game_manager.cpp- Manages the main game loop, level difficulty, updates, and input processing.snake_class.h/snake_class.cpp- Manages the snake's body coordinates (usingstd::deque), movement direction, growth, and self-collision.board_class.h/board_class.cpp- Coordinates grid rendering (Draw) and handles wall boundary collisions.food_class.h/food_class.cpp- Uses modern C++ random number generation to spawn food at random coordinates not occupied by the snake.terminal.h/terminal.cpp- Configures the terminal to raw/non-blocking mode using the POSIXtermiosAPI.
- Movement:
W/Arrow Up— Move UpA/Arrow Left— Move LeftS/Arrow Down— Move DownD/Arrow Right— Move Right
- Quit: Press
Qat any time to exit the game safely and restore your terminal settings.
Ensure you have a C++ compiler installed (like clang++ or g++).
git clone https://github.com/your-username/snake-game-cpp.git
cd snake-game-cppCompile all source files together using C++17 standard:
clang++ -std=c++17 run.cpp game_manager.cpp snake_class.cpp food_class.cpp board_class.cpp terminal.cpp -o snake./snake