Skip to content

Repository files navigation

Terminal Snake Game in C++

Features

  • 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.

Project Structure

  • run.cpp - The entry point containing the main() 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 (using std::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 POSIX termios API.

🎮 How to Play

  • Movement:
    • W / Arrow Up — Move Up
    • A / Arrow Left — Move Left
    • S / Arrow Down — Move Down
    • D / Arrow Right — Move Right
  • Quit: Press Q at any time to exit the game safely and restore your terminal settings.

🛠️ Installation & Compilation

Ensure you have a C++ compiler installed (like clang++ or g++).

1. Clone the repository

git clone https://github.com/your-username/snake-game-cpp.git
cd snake-game-cpp

2. Compile the source code

Compile 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

3. Play the game

./snake

About

erminal-based Snake Game written in C++17, designed specifically for macOS (and Unix-like environments). It utilizes clean Object-Oriented Programming (OOP) principles and the POSIX `termios` API for real-time, non-blocking keyboard input—entirely free of heavy external graphics engines or libraries.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages