Skip to content

lian999111/CppND-Capstone-Snake-Game

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPPND: Capstone 2-Player Snake Game

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.

Keyboard Control Guide:

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

Dependencies for Running Locally

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./SnakeGame.

Project Class Structure

Snake:

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.

Controller:

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.

SDLFont:

An RAII class that manages the acquisition and release of TTF_Font. The font is used to render the "Pause" message.

Renderer:

Responsible for rendering the state of the game. The rendering of the "Pause" message is achieved using the SDL2_TTF library.

Game2P:

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.

Rubrics Fulfilled

Loops, Functions, I/O

  • 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)

Object Oriented Programming

  • 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. |

Memory Management

Concurrency

About

A 2D Snake game using C++ and SDL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 63.5%
  • CMake 36.5%