This is my Capstone project for the Udacity C++ Nanodegree Program.
The starting point for this project is a simple snake clone, to which I have added a number of 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
aptorapt-getinstallation 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.
- Fix food placement bug
- In the starter code, the food could sometimes be placed at x and/or y coordinate of 32, which is outside of the grid (given that the 32x32 grid coords are zero-indexed).
- I corrected this "off by one" error.
- Move direction change logic into snake class
- Changing direction, and determining whether a change in a particular direction is possible, is more appropriately encapsulated within the snake class.
- I also created a helper method
Snake::OppositeDirection()to encapsulate the logic of determining the opposite of a Snake::Direction value. - Following these changes, it was possible to make the
Snake::directionmember private.
- Second snake
- The adversary snake is a user controlled snake controlled using the w/a/s/d keys.
- It has a green head to differentiate from the primary user.
- Each snake has its own score that is displayed in the title bar and and the end of the same.
- If a snake crashes into another snake, it dies. The snake that has been crashed into continues (unless they both crashed into each other's head).
- A dead snake remains as an obstacle for the other snake.
- The game continues until both snakes are dead.
- Expiring food
- Food expires over time, visibly fading until it disappears and is replaced with the next morsel, unless it is first consumed.
- The speed at which food expires is random within a set range.
| Feature / refactoring | Rubric criteria satisfied | Evidence |
|---|---|---|
| Expiring food | The project uses Object Oriented Programming techniques. | here |
| Classes use appropriate access specifiers for class members. | here and here | |
| Class constructors utilize member initialization lists. | here | |
| Classes abstract implementation details from their interfaces. | here | |
| The project makes use of references in function declarations. | here | |
| Classes encapsulate behavior. | here | |
| Second snake | The project accepts user input and processes the input. | here |
| The project makes use of references in function declarations. | here and here | |
| Class constructors utilize member initialization lists. | here | |
| Move direction change logic into snake class | Classes encapsulate behavior. | here and here |
| The project uses Object Oriented Programming techniques. | here and here | |
| Classes abstract implementation details from their interfaces. | here and here | |
| The project demonstrates an understanding of C++ functions and control structures. | here |
The project structure hasn't changed significantly from the starter project. Most changes have taken place within the context of that structure.
However, an additional class Food has been created to encapsulate the behaviour of a single piece of food,
including its position and expiry. The code for Food is in src/food.h and src/food.cpp.
A full diff can be viewed here.
