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. - SDL2 Images
- A simple library to load images of various formats as SDL surfaces. Install using
sudo apt-get install libsdl2-image-dev
- A simple library to load images of various formats as SDL surfaces. Install using
- 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
.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
This program additionally uses the sdl2 image library. See Dependencies for Running Locally above.
It is widely known that eagles eat snakes, so I added an eagle to the game that chases the snake and tries to eat it! The eagle follows the snake, and on contact, the game terminates with the message "Eagle caught the snake!". The speed of the eagle is always relative to that of the snake, and this value changes with different difficulty levels. The renderer loads a nice image of an eagle which moves across the screen to try and catch the snake.
Three difficulty levels are added to the game: Easy, Medium, and Hard. The difficulty level changes the relative speed of the eagle, making it easier or harder for the eagle to catch the snake. You can change the difficulty by pressing the left and right bracket keys "[" and "]". Easy = 0, Medium = 1, and Hard = 2.
I added an extra option that spawns extra green food every 10 seconds. You have 3 seconds to catch the extra food and obtain an additional +5 points! After that, you have to wait 7 seconds before new extra food spawns randomly and you can try again.
The program compiles and runs the same way as it is described in Basic Build Instructions.
I added a lot of new functionalities with all kinds of techniques. I tried to name most of them below, but for all the changes and updates, I refer you to the "diff" section of GitHub for all the changes I made.
The project uses numerous control structures throughout the program to control the flow of the game.
The program loads an image "eagle_small.png" in renderer.cpp
as an SDL_Surface and renders it to the screen accordingly.
The program handles additional keyboard inputs. Namely the bracket keys "[" and "]" to change the difficulty of the game.
In the renderer class, the argument of Renderer has been changed to const Snake&
to introduce an immutable variable. Additionally, the eagle class uses getters and setters for its private members.
The snake and eagle classes now use appropriate access specifiers for class members.
The eagle class uses member initialization lists to initialize the class.
Member functions do not change the program state in undocumented ways. Additionally, the Snake class has been reorganized.
References are used throughout the program to pass variables by reference. For example in the Renderer::Render() method.
The project uses destructors properly.
For example, the eagle class has a shared pointer to the snake and uses that to chase the snake.
The project uses multithreading to spawn extra food on a timer. The PlaceExtraFoodTimer() function is started on a separate thread and has a timer to create additonal food for extra points. The program waits for the thread and exits cleanly. An atomic variable is used to signal the ExtraFoodThread that the program has exited on both a SIGINT and on clicking the cross to exit the program (SDL_QUIT).
The lock type unique_lock is used to protect variables such as extra_food and extra_food_placed.
A condition variable is used to notify the thread that extra food has been placed.
Good luck with getting the high score! On the difficulty high, I found it extremely hard to get a score above 20, so I am curious if people get more :).