This project extends the C++ Nanodegree Capstone Project. It is a 2-player snake game where 2 players compete to get higher scores without bumping into themselves or each other. The blue snake shall go after the blue food and the orange snake shall go after the orange food.
Key | Action |
---|---|
Up | 1P goes up |
Down | 1P goes down |
Left | 1P goes left |
Right | 1P goes right |
W | 2P goes up |
S | 2P goes down |
A | 2P goes left |
D | 2P goes right |
SPACE | Pause the game |
- 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
- All installation instructions can be found here
- Note that for Linux, an
apt
orapt-get
installation is preferred to building from source.- e.g.
sudo apt-get install libsdl2-ttf-dev
- e.g.
- 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
.
Maintains the head and body of the snake. The moving and status checking of the snake are separated into different methods to decouple the behaviors. It uses std::mutex to be thread-safe since different threads in Game2P may be updating and checking the status of the snake at the same time.
Rresponsible for receiving user's input and change the state of the game accrodingly. When the game is in pause mode, it waits for only SPACE to be pressed then continues the game while other key strokes are ignored to avoid state changine during a pause.
An RAII class that manages the acquisition and release of TTF_Font. The font is used to render the "Pause" message.
Responsible for rendering the state of the game. The rendering of the "Pause" message is achieved using the SDL2_TTF library.
Controls the flow of the game and maintains the 2 snakes and their foods. The "Run()" method runs the game loop, which involves Input, Update, and Render steps. The Update step first moves the 2 snakes using 2 tasks, then check the necessary updates incurred by the movements again using 2 tasks.
- The project demonstrates an understanding of C++ functions and control structures.
- The project reads data from a file and process the data, or the program writes data to a file. (see sdlfont.cpp)
- The project accepts user input and processes the input. (see controller.h, controller.cpp)
- The project uses Object Oriented Programming techniques.
- Classes use appropriate access specifiers for class members.
- Class constructors utilize member initialization lists. (see renderer.cpp, snake.cpp, game2p.cpp)
- Classes abstract implementation details from their interfaces.
- Classes encapsulate behavior. |
- The project makes use of references in function declarations. (e.g. snake.cpp, controller.cpp)
- The project uses destructors appropriately. (e.g. renderer.cpp, sdlfont.cpp)
- The project uses scope / Resource Acquisition Is Initialization (RAII) where appropriate. (e.g. sdlfont.h, sdlfont.cpp)
- The project follows the Rule of 5. (see sdlfont.h, sdlfont.h)
- The project uses smart pointers instead of raw pointers. (see game2p.h, game2p.cpp)
- The project uses multithreading. (see game2p.cpp)
- A mutex or lock is used in the project. (e.g. snake.h, snake.cpp)