This is 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.
-
Player
- Enter player name before game starts
- Display player information after game over
- File:
player.h
-
Thread-safe Moving Obstacle
- Moving obstacle that travels horizontally across the screen
- Game over when snake collides with obstacle
- File:
obstacle.h/cpp
main.cpp
: Program entry, initializes gamecontroller.h/cpp
: Input handlinggame.h/cpp
: Core game logicrenderer.h/cpp
: Game renderingsnake.h/cpp
: Snake movement and growthobstacle.h/cpp
: Moving obstacle implementationplayer.h/cpp
: Player info management
- 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. - 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:
git clone <url>
- 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.
- functions and control structures.
- accepts user input and processes the input (
main.cpp
, lines 7-12)
- two classes are added to the project with appropriate access specifiers for class members (
obstacle.h/.cpp
,player.h
) - Class constructors utilize member initialization lists (
obstacle.cpp
, lines 3-8,player.h
, lines 7-13) - Classes abstract implementation details from their interfaces.
- use references (
player.h
, lines 12,game.cpp
, lines 97,renderer.cpp
, lines 41). - use destructors appropriately (
obstacle.cpp
, lines 10) - use scope / Resource Acquisition Is Initialization (RAII) where appropriate (std::lock_guard).
- use move semantics to move data instead of copying it, where possible (
player.h
, lines 9).
- use multithreading
- Obstacle movement runs in separate thread (
obstacle.cpp
, lines 56)
- Obstacle movement runs in separate thread (
- use mutex and lock
- Position protection with mutex (
obstacle.cpp
, lines 22) - Thread-safe position updates (
obstacle.cpp
, lines 37)
- Position protection with mutex (