A modern, colorful, and interactive terminal-based Snake Game built using C++.
Designed to run seamlessly on both Windows and Linux, it demonstrates cross-platform programming, terminal manipulation, and object-oriented design principles.
- Overview
- Features
- System Architecture
- Technical Implementation
- Installation & Compilation
- Usage
- Design Patterns & Best Practices
- Performance Considerations
- Future Enhancements
- Team Contributors
The Snake Game is a classic arcade-style project recreated in C++ using only standard libraries and terminal control techniques.
It showcases real-time keyboard input, smooth movement, dynamic rendering, and persistent high-score storage.
This project was developed as part of an Introduction to Programming (ITP) course to demonstrate proficiency in:
- Structured and Object-Oriented Programming (OOP)
- File handling
- Platform abstraction
- Interactive console applications
| Feature | Description |
|---|---|
| 🎮 Cross-Platform | Compatible with both Windows and Linux environments. |
| 🌈 Colorful UI | Enhanced visuals using ANSI escape codes and emojis. |
| 💾 High Score System | Saves and loads scores automatically via file I/O. |
| ⚡ Real-Time Input | Non-blocking input handling ensures smooth gameplay. |
| 🧱 Dynamic Walls | Detects and ends the game on wall collisions. |
| 🧠 Self-Collision Detection | Ends the game if the snake runs into itself. |
| 🔄 Restart & Exit Menu | Easy navigation after game over. |
| 🧍 User-Friendly Interface | Simple control scheme using both arrow keys and WASD. |
The system is divided into modular components for clarity and maintainability:
+------------------------------------------------------+
| Game (Main) |
| - Controls flow and game loop |
| - Manages rendering, updates, and user input |
+------------------------------------------------------+
| | |
v v v
+-------------+ +--------------+ +----------------+
| Snake | | Food | | HighScoreManager |
+-------------+ +--------------+ +----------------+
| Body logic | | Food spawn | | File I/O logic |
| Movement | | Position gen | | Save/load data |
+-------------+ +--------------+ +----------------+
Snake→ Handles movement, direction, growth, and self-collision.Food→ Randomly spawns food at positions not occupied by the snake.HighScoreManager→ Manages reading/writing the high score file.Game→ Coordinates gameplay, input, rendering, and end conditions.
-
Language: C++ (C++11 and above)
-
Libraries Used:
<deque>– for snake body storage<fstream>– for file I/O (high score system)<termios.h>and<ioctl.h>– for non-blocking input on Linux<conio.h>and<windows.h>– for Windows compatibility- ANSI escape codes for color and cursor control
-
Game Loop Structure:
while (!game.over()) { game.input(); game.update(); game.draw(); Sleep(game.speed()); }
-
Cross-Platform Input Handling:
Separate implementations for_WIN32and POSIX systems using preprocessor directives.
- Open Command Prompt or PowerShell.
- Compile using:
g++ main.cpp -o snake
- Run the game:
snake.exe
- Open Terminal.
- Compile using:
g++ main.cpp -o snake
- Run the game:
./snake
💡 Note: The program automatically handles raw terminal mode and restores it safely upon exit.
Eat as many apples 🍎 as possible without hitting the wall or your own body.
Each apple increases your score and makes the snake grow longer.
| Key | Action |
|---|---|
| W / ↑ | Move Up |
| S / ↓ | Move Down |
| A / ← | Move Left |
| D / → | Move Right |
| ESC | Exit Game |
| R | Restart after Game Over |
| Q | Quit Game |
-
Object-Oriented Design (OOP):
Clear separation between data (state) and behavior (methods) across Snake, Food, and Game classes. -
Encapsulation:
Class members are managed through well-defined interfaces. -
Single Responsibility Principle:
Each class performs one distinct task (e.g.,Snakehandles movement only). -
Platform Abstraction:
#ifdef _WIN32blocks separate Windows-specific and Linux-specific code, ensuring maintainability. -
File Handling Best Practices:
Safe opening, reading, and writing to maintain data persistence for high scores.
-
Non-blocking I/O:
Real-time responsiveness through minimal CPU waiting usingkbhit()andread()timeouts. -
Optimized Rendering:
Uses ANSI cursor repositioning (\033[H) to redraw efficiently instead of clearing the screen entirely. -
Memory Efficiency:
std::dequeallows fast push/pop operations for snake body movement.
Planned improvements for future versions:
- 🏁 Multiple difficulty levels (speed adjustments)
- 🧱 Obstacles and barrier mechanics
- ⏸️ Pause and resume functionality
- 🧾 Scoreboard with player names
- 🌍 Border wrapping or maze modes
- Varunee (202512067)
- Nishka (202512035)
🎓 Project developed as part of the IT603 – Introduction to Programming (ITP) course, 2025.
Faculty: Prof. Ankush Chander
Teaching Assistant (TA): Monson Verghese, Devansh Shah
MSc IT Department, Dhirubhai Ambani University (DAU)