Welcome to my Capstone Project for my Udacity's C++ Nanodegree!
For this project, I chose to improve the SDL game Snake inspired by this StackOverflow question and you can find Udacity's original repo here.
I will add/have added the following features to the game:
๐ The snakes dies when it runs into the edges of the grid, instead of going through
๐ Replay button to restart the game from zero
๐ Save the latest 5 highest scores
๐ There are four different types of food that affect the snake! The bad foods timeout after a couple of seconds. The meaning of every food is as follows:
ย ย ย ย ๐ข Good food that will increase your score, the snake grows and the speed will go up
ย ย ย ย ๐ต Good food that will give you one extra live! You can gather up to 4 lives
ย ย ย ย ๐ The snake will slow down and shrink one block. You lose 1 point from your score
ย ย ย ย ๐ด Bad food that will take one live from the snake!
The folder and file structure of the project is as follows:
๐ cmake folder where extra .cmake
files are stored. FindSDL2_image.cmake
file helps locate the SDL and SDL_Image libraries in the computer
๐ imgs folder with the images for the replay screen and the readme
๐ include folder with all the .hpp
files
๐ src folder with all the .cpp
files
๐ data folder contains the .txt
file with the 5 highest scores in the game
The class structure is as follows:
๐ A Snake
class defined in the file snake.hpp
and instantiated in the file snake.cpp
. This class handles all the snake features, such as size, life status, body, head location, speed and reset.
๐ฎ A Controller
class defined in the file controller.cpp
and instantiated in the file controller.cpp
. This class handles all the user interactions (keys pressed and mouse events).
๐ฅ A Renderer
class defined in the file renderer.cpp
and instantiated in the file renderer.cpp
. This class takes care of the graphics of the game (colour of the snake, the food, creating the window, etc.).
๐พ A Game
class defined in the file game.cpp
and instantiated in the file game.cpp
. This class handles the game loop (input, update, render).
The main.cpp
file creates objects type Game
, Renderer
and Controller
, to interact with each other.
๐ macOS 12.1 Monterey with 8-Core Intel Core i7 chip
๐ Homebrew 3.3.5
๐ cMake 3.21.3
๐ GNU Make 3.81
๐ C++ 17
๐ Apple clang version 13.0.0
๐ SDL 2.0.18. Download SDL from the official website.
๐ SDL_Image 2.0
Run the following command in the terminal
sudo apt-get install libsdl2-image-dev
Download the binary (.dmg
) from here and wait for it to download. Then move the SDL2_image.framework
folder to /Library/Frameworks/
.
If you do not have the correct permission in the computer (i.e. root or admin), read the ReadMe.txt
inside the downloaded .dmg
Create a build
folder and change to that directory
mkdir build && cd build
Within the build directory run
cmake .. && make
The executable is within the build
directory. You can run it as follows
./Snake
Group | Rubric | File | Line Numbers |
---|---|---|---|
Loops, Functions, I/O | The project demonstrates and understanding of C++ functions and control structures | snake.hpp renderer.hpp |
8 11 14 23 31 |
The project reads data from a file and process the data, or the program writes data to a file | game.cpp | 141 160 | |
The project accepts user input and process the input | controller.cpp | 17 | |
Object Oriented Programming | The project uses Object Oriented Programming techniques | main.cpp | 18 19 20 21 |
Classes use appropriate access specifiers for class members | snake.hpp food.hpp controller.hpp renderer.hpp game.hpp |
10 46 12 26 35 47 10 14 33 43 20 33 |
|
Class constructors utilise member initialisation lists | controller.cpp snake.hpp food.cpp game.cpp renderer.cpp |
6 21 20 5 6 |
|
Classes encapsulate behaviour | snake.hpp food.hpp |
23-40 48-49 13-22 36-45 |
|
Overloaded functions allow the same function to operate on different parameters | renderer.hpp | 39 40 | |
Memory Management | The project makes use of references in function declarations | renderer.hpp game.hpp controller.hpp food.hpp snake.hpp |
40 25 11 22 44 45 49 |
The project uses smart pointers instead of raw pointers | renderer.hpp game.hpp controller.hpp food.hpp |
40 45 46 47 34 11 22 48 |