A console-based version of the timeless Snake game, developed using C++. This project serves as a great beginner-friendly example of game loop mechanics, input handling, and basic console manipulation in C++.
- Features
- Prerequisites
- How to Compile and Run
- How to Play
- Code Overview
- Future Improvements
- Contributing
- Classic Snake Mechanics: Eat the fruit (
F) to grow your snake (o) and increase your score. - Dynamic Score Keeping: Your score is displayed and updated in real-time.
- Infinite Walls: The snake can wrap around the screen, moving from one edge to the opposite.
- Collision Detection: The game ends if the snake's head (
O) collides with its own body. - Simple Controls: Easy-to-use keyboard controls for fluid movement.
To compile and run this game, you will need:
- A C++ compiler (like G++, MinGW, or the compiler included with Visual Studio).
- A Windows operating system (due to the use of
<windows.h>and<conio.h>).
- Open a terminal or command prompt.
- Navigate to the directory where you saved the
Snake Game (PF Project).cppfile. - Compile the code using your C++ compiler. For example, if you are using g++:
g++ "Snake Game (PF Project).cpp" -o SnakeGame.exe - Run the game by executing the compiled file:
./SnakeGame.exe
The goal is simple: guide the snake to eat as much fruit as possible without running into itself!
-
Controls:
W- Move UpA- Move LeftS- Move DownD- Move RightX- Exit the game at any time.
-
Objective:
- The snake (
Ois the head,ois the tail) follows your commands. - Each time you eat a fruit (
F), the snake gets longer and your score increases by 10. - The game ends if the snake's head hits any part of its tail.
- The snake (
The logic is contained within a single C++ file and is organized into several key functions:
setup(): Initializes all game variables, sets the snake's starting position, and places the first fruit.draw(): Renders the current state of the game to the console. It clears the screen and redraws the border, snake, fruit, and score for each frame.input(): Listens for keyboard presses to change the snake's direction or to exit the game.logic(): The core of the game. It updates the snake's position based on the current direction, checks for collisions with fruit or the snake's own tail, and updates the tail segments.main(): The main function that contains the game loop, which continuously calls thedraw,input, andlogicfunctions until thegameOverflag is set totrue.
This is a simple implementation, but it could be expanded with new features:
- High Score System: Save the highest score to a file so it persists between games.
- Difficulty Levels: Add options to increase the snake's speed.
- Obstacles: Add walls or other obstacles inside the play area.
- Power-ups: Introduce special fruits that have different effects (e.g., slow down time, shrink the snake).
- Cross-Platform Support: Refactor the code to remove Windows-specific libraries and allow it to run on macOS and Linux.
Contributions are welcome! If you have ideas for new features or improvements, feel free to fork the repository, make your changes, and submit a pull request.