This is an Enhanced version of the starter Snake Game supplied by Udacity C++ Nanodegree Program. The udacity project was inspired by this excellent StackOverflow post and set of responses.
This enhanced version supplies a number of extra features to the standard Snake game. Running this program should open a new Snake Game window where you'll be able to play.
The new version utilizes new classes to help organize the game. The enhanced version also uses the SDL_ttf and SDL_image libraries to allow showing text and images on the screen.
- Snakes love donuts! Eating a donut causes the snake to grow more and move faster than normal food.
- Snakes hate bombs. Eating a bomb cause the game to instantly end.
- Restart Game A restart game button was added so the player won't have to re-run the program each time they die.
- Show Score Added a show score button that renders a new Score Screen.
- Save Score Added a feature where the the player can save their score to a file. By default the file is saved into the same directly where the program was run. The user can add a path to their own save directory. Old scores are not removed when new scores are added.
- Loops, Functions, I/O -A variety of control structures are used in the project-
- The program utilizes switches and while loops. /File:
game.cpp
line: 56/, /File:obstacle.cpp
line: 96/
- Loops, Functions, I/O -The project code is clearly organized into functions.-
- Care was taken to make sure that large files like
renderer.cpp
were broken into smaller, more reusable functions. /File:renderer.cpp
line: 179/, /File:renderer.cpp
line: 207/, /File:renderer.cpp
line: 219/
- Loops, Functions, I/O -The project reads data from an external file or writes data to a file as part of the necessary operation of the program.-
- The program uses
<fstream>
to write scores to a file. It reads in image files with SDL_Image. It also reads in a font file with SDL_ttf. /File:Helpers.cpp
line: 51/, /File:obstacle.cpp
line: 89/
- Loops, Functions, I/O -The project accepts input from a user as part of the necessary operation of the program.-
- The program takes user input from mouseclicks and keyboard events. Mouseclicks allow buttons to be tapped. Keyboard events allow for adding the users own file path. /File:
controller.cpp
line: 47/, /File:controller.cpp
line: 68/
- Object Oriented Programming -The project code is organized into classes with class attributes to hold the data, and class methods to perform tasks.-
- The program is split up into a number of new classes. Classes like Texture and it's Inheritor - Obstacle - allow for logic to be encapsulated and reused. The program also has a struct - RandomPoint - that encapsulates logic but didn't need to be a full blown class. The program uses Enums as well. /File:
texture.cpp
line: 1/, /File:obstacle.cpp
line: 1/, /File:button.cpp
line: 1/, /File:Helpers.cpp
line: 9/, /File:Helpers.h
line: 8/
-
main.cpp - This starts the entire program and setups of the environment.
-
controller.cpp - This handles user input and user interaction with the game. Through SDL it continuously polls for user input events.
-
game.cpp - This is the heart of the programs logic. It handles the games state as well as sending commands to the SDL_Renderer.
-
renderer.cpp - This is the major class that setups how objects are rendered to the screen.
-
texture.cpp - This is the base class for rendered textures and is used for rendering obstacles and text to the screen.
-
obstacle.cpp - This inherits functionality from Texture and expands on the specifics of how obstacles are loaded and rendered. It also has a static function to help check for collisions of game objects.
-
button.cpp - This is a simple class that encapsulates data for the game interactive buttons.
-
snake.cpp - This is the class that encapsulates the behavior and data for the Snake.
-
Helpers.cpp - This is a Helper file that contains the RandomPoint struct, writeScoreFile function and MouseActionButtons enum.
-- **main.cpp**
-- **controller.cpp**
-- **game.cpp**
-- **button.cpp**
-- **snake.cpp**
-- **renderer.cpp**
-- **texture.cpp**
-- **obstacle.cpp**
-- **Helpers.cpp**
- 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_TTF >= 2.0
- Instructions [here] (https://www.libsdl.org/projects/SDL_ttf/)
- Note that for Linux, an
apt
orapt-get
installation is preferred to building from source.
- SDL2_Image >= 2.0
- Instructions [here] (https://www.libsdl.org/projects/SDL_image/)
- 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.
- Install CMAKE version >= 3.7. Use instructions to upgrade here and here. MacOS users should install with Homebrew. Here are the instructions for Ubuntu 16.0.4:
wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | sudo apt-key add -
sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ xenial main'
sudo apt-get update
sudo apt-get --only-upgrade install cmake=3.15.2-0kitware1
- install SDL2_image and SDL2_ttf. here. MacOS users should install with Homebrew. Here are the instructions for Ubuntu 16.0.4:
apt-get install libsdl2-image-dev
apt-get install libsdl2-ttf-dev
- Use the appropriate
CMakeLists.txt
file. For example on Ubuntu 16.0.4 replaceCMakeLists.txt
withCMakeLists_ubuntu_16.txt
. - Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./SnakeGame
.
Credits are due to the resources I used to help build the program:
- For the initial project: Udacity C++ Nanodegree Program and this
- For help with the SDL library and general Game Programming: LazyFoo
- For the open source sprites:
- For help with c++ troubleshooting, SDL installation, and many other bugs StackOverflow