Skip to content

A CLI-based Snake game implementation from scratch in C++20.

Notifications You must be signed in to change notification settings

Hichemchir/snake_game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game

A CLI-based Snake game implementation in C++20.

Description

Classic Snake game where the player controls a snake that grows by eating fruits. The game ends when the snake hits a wall or bites its own tail.

Game Rules

  • Navigate the snake using ZQSD keys
  • Eat fruits (@) to grow and earn points
  • Each fruit gives 10 points
  • Avoid walls and your own tail
  • The snake moves continuously in the current direction

Installation

# Clone the repository
git clone https://github.com/yourusername/snake_game.git
cd snake_game

# Create build directory
mkdir build
cd build

# Configure and compile
cmake ..
cmake --build .

Usage

# From the build directory
./snake

Controls

  • Z - Move up
  • S - Move down
  • Q - Move left
  • D - Move right
  • Ctrl+C - Quit game

Project Structure

snake_game/
├── include/           # Header files
│   ├── Board.hpp
│   ├── Constants.hpp
│   ├── Direction.hpp
│   ├── Fruit.hpp
│   ├── Input.hpp
│   ├── Position.hpp
│   └── Snake.hpp
├── src/              # Source files
│   ├── Board.cpp
│   ├── Direction.cpp
│   ├── Fruit.cpp
│   ├── Input.cpp
│   ├── main.cpp
│   ├── position.cpp
│   └── Snake.cpp
├── CMakeLists.txt
└── README.md

Architecture

The game is built around several key components:

  • Position: Represents coordinates on the board
  • Direction: Manages movement directions
  • Snake: Handles snake logic, movement, and collision detection
  • Fruit: Manages fruit spawning and point values
  • Board: Manages game boundaries and fruit placement
  • Input: Handles keyboard input in non-blocking mode

Configuration

Game parameters can be modified in include/Constants.hpp:

constexpr int BOARD_WIDTH = 30;
constexpr int BOARD_HEIGHT = 20;
constexpr int FRUIT_POINTS = 10;

About

A CLI-based Snake game implementation from scratch in C++20.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published