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.
The Capstone Project gives you a chance to integrate what you've learned throughout this program. This project will become an important part of your portfolio to share with current and future colleagues and employers.
In this project, you can build your own C++ application or extend this Snake game, following the principles you have learned throughout this Nanodegree Program. This project will demonstrate that you can independently create applications using a wide range of C++ features.
- cmake >= 3.7
- All OSes: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- SDL2 >= 2.0
- All installation instructions can be found here
Note that for Linux, an
apt
orapt-get
installation is preferred to building from source. - gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - install Xcode command line tools
- Windows: recommend using MinGW
- Clone this repo.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./SnakeGame
.
-
The games has been modified to include wrong foods(red ones) which when eaten by snake reduces snake's body size and speed. User has been given three life chances to continue playing the game if the snake dies in case of snake head hitting its own body or snake eating wrong food when its body size is one.
-
The title window shows the value of lives remaining and updates as the lives get reduced. Everytime a life chance is lost, the window refreshes with new positions of foods.
-
At the end of the game when all lives are lost, a dialog msg appears which allows you to restart the game if you want to continue the game.
- The project demonstrates an understanding of C++ functions and control structures.
In snake.cpp, the exisiting code for growth of snake body has been modified to include logic for reducing snake's body. For this purpose, switch statement where based on the value of the enum class growing, decision has been made to grow, reduce or not change. The following code is visible in UpdateBody() function from line 58 onwards.
For adding new feature of live counter for snake, new logic have been added on two instances of snake dying. One is when snake eats wrong food when its size is one and the other is when snake head hits its own body. The logic for the first instance is added in game.cpp from line 91 onwards in the Update() function and second instance logic is added from line 74 onwards inside UpdateBody(). When snake hit itself, the snake body and size is reset to default irrespective of how much it has grown before hitting itself.
- The project uses Object Oriented Programming techniques.
New translations units food.h and food.cpp are created. Food constructor is defined with Type of food as argument. A Type named enum has been created to initialise with right or wrong food.
-
Classes use appropriate access specifiers for class members. Food class consist of public member functions and variables as well as a private member. New member functions and member variables are added in snake.h and game.h for checking the lives count and refreshing the placement of food when the snake dies.
-
Class constructors utilize member initialization lists. Food constructor does use of member initialization list.
-
Classes abstract implementation details from their interfaces. The member functions PlaceFood and CheckFood have appropriate functions names. One is void and other is a boolean function which is explanantory with the function name given.
-
The project makes use of references in function declarations. The member functions PlaceFood and CheckFood have Snake object being passed as reference.
main.cpp was modified to add the message box when the game run ends after user has lost all life chances and calls main function recursively so that the restart can happen more than once.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.