Skip to content

AHossamHaloda/SnakeGame_2D

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game Project

This is a starter repo for the Capstone project in the Udacity C++ Nanodegree Program. The code for this repo was inspired by this excellent StackOverflow post and set of responses.


Game Overview

In this game, you control a snake navigating through a 2D grid by moving it up, down, left, and right using the arrow keys.

Starting the Game AT the Beginning the game asks u to select the mode

  • Start a new Game
  • Check Top Scores
  • QUIT

By choosing Start a new Game Enter your name to begin playing. Your score will be recorded and stored if it surpasses the previous highest scores.

Controls Arrow Up Key: Move the snake upwards. Arrow Down Key: Move the snake downwards. Arrow Left Key: Move the snake to the left. Arrow Right Key: Move the snake to the right. Esc Key: Pause or resume the game.

Scoring The game keeps track of scores, and only scores higher than existing top scores will be saved.

Max number of top scores is configurable through Max_NUMBER_PLAYERS in gameDataBaseCfg.h


Project Dependencies


Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./SnakeGame.

Project New Features

New Design:

  • Code Restructure: The code has been restructured for better organization by separating source files from header files and moving main.cpp to the parent directory for improved project organization.

  • GameManager Class: Introduced a new class, GameManager, which centralizes all project control. The Game Super Loop now runs in the GameManager, which interacts with all game objects. This design allows each object to focus on a single task, minimizing dependencies between objects. The Snake object is shared among all game objects using a std::shared_ptr, achieving dependency injection to reduce coupling, enhance design flexibility, and improve memory management.

  • Dependency Injection: Utilized the Snake object via a shared pointer in the GameManager class, passing it to other game objects. This approach reduces coupling and enhances flexibility in design changes.

  • State Machine Integration: Integrated a state machine in the Game Super Loop using two enums to represent the game menu options and the current game state.

  • Configurable Game Settings: All game settings are configured through preprocessor macros in configurable files gameManagerCfg.h and gameDataBaseCfg.h and assigned to const variables at compile/run time.

  • Worker Thread for Game Data: Introduced a worker thread to handle all operations related to the game database, improving performance and responsiveness.

New Features

  • Menu Options: Added a menu that allows players to start a new game, view top players, or quit the game.

  • GameDataBase Class: Introduced a new class, GameDataBase, to manage storing player scores and names in an external file GameTopscore.txt.

  • Pause/Resume Functionality: Implemented a new feature using the Esc key to allow players to pause and resume the game.

  • Reset Game Mechanism: Integrated a reset game mechanism within the state machine, enabling players to start a new game without restarting the program from scratch.


CC Attribution-ShareAlike 4.0 International

Shield: CC BY-SA 4.0

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

CC BY-SA 4.0


About

A simple Snake Game that uses SDL for graphics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published