Dive into a nostalgic gaming experience with this terminal-based rendition of the timeless Snake game! Crafted with C++ and the ncurses library, this project brings the simple yet addictive fun of Snake right to your command line.
- Classic Gameplay: Relive the original thrill of guiding your ever-growing snake.
- Smooth Controls: Intuitive arrow key navigation for precise movements.
- Dynamic Growth: Watch your snake get longer with every tasty morsel consumed.
- Collision Detection: Beware of the walls and your own tail – a true test of skill!
- Terminal-Powered Graphics: Retro-style visuals brought to life with the ncurses library.
- Lightweight & Fast: Runs smoothly in most terminal environments.
- Language: C++
- UI/Graphics: ncurses library (for terminal-based interface)
- Compiler: GCC / Clang (or any standard C++ compiler)
Ready to chase some food? Here's how to get the game up and running on your system.
Before you begin, ensure you have the following installed:
- A C++ Compiler:
- Most Linux distributions and macOS come with
g++
orclang
.
- Most Linux distributions and macOS come with
- ncurses Library: This library is essential for the terminal graphics.
- On Debian/Ubuntu-based systems:
sudo apt-get update sudo apt-get install libncurses5-dev libncursesw5-dev
- On Fedora/RHEL/CentOS-based systems:
sudo dnf install ncurses-devel
- On macOS (using Homebrew):
brew install ncurses
- On Debian/Ubuntu-based systems:
-
Clone the Repository (if applicable):
# git clone <your-repository-url> # cd snake-game
(If you just have the
main.cpp
file, skip this step and ensuremain.cpp
is in your current directory.) -
Compile the Code: Open your terminal and use the C++ compiler to build the executable. Link against the ncurses library.
g++ main.cpp -o snake_game -lncurses
-
Run the Game: Execute the compiled program to start playing!
./snake_game
The rules are simple, but mastery is a challenge!
- Objective: Grow your snake as long as possible by eating food.
- Controls:
- Arrow Up: Move Up
- Arrow Down: Move Down
- Arrow Left: Move Left
- Arrow Right: Move Right
- Game Over: The game ends if the snake collides with:
- The game board boundaries (walls).
- Its own body.
A brief look at the engine under the hood:
init_game()
: Sets up the ncurses environment, initializes the game board, places the initial snake, and spawns the first piece of food.draw_board()
: Renders the playing area boundaries.draw_snake()
/draw_food()
: Responsible for visually representing the snake and food on the terminal.move_snake()
: Updates the snake's position based on player input and current direction. This function also handles the logic for the snake's body following its head.check_collision()
: Detects if the snake has hit a wall or itself, triggering the game over state.check_food_eaten()
: Checks if the snake's head has reached the food. If so, it grows the snake and repositions the food.game_over()
: Displays the end-game message and awaits player action to close.main()
: The heart of the game, containing the main game loop that processes input, updates game logic, and refreshes the display.
This classic can always learn new tricks! Here are some potential ideas:
- 🏆 Scoring System: Keep track of points for each food eaten.
- 🔥 Difficulty Levels: Introduce varying speeds or obstacles.
- 🎨 Color Customization: Allow players to choose colors for the snake, food, or board.
- 🚀 Power-ups: Special food items that grant temporary abilities (e.g., speed boost, invincibility, score multiplier).
- 🧱 Obstacles: Add static or moving obstacles within the game board.
- 🎵 Sound Effects: Basic beeps for eating or game over (if terminal supports).
- High Score Board: Save and display top scores.
- "Ghost Mode": Allow passing through walls to appear on the opposite side.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page (if you plan to host this on GitHub/GitLab).
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
(Optional: Add a License section here if you have one, e.g., MIT License)