samfrances/CppND-Capstone-Snake-Game

A 2D Snake game using C++ and SDL

★ 0Forks 0C++GitHub ↗Compare

README

CPPND: Capstone - 2-Player Snake Game

This is my Capstone project for the Udacity C++ Nanodegree Program.

The starting point for this project is a simple snake clone, to which I have added a number of features.

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.

Added features / refactorings

  • Fix food placement bug
    • In the starter code, the food could sometimes be placed at x and/or y coordinate of 32, which is outside of the grid (given that the 32x32 grid coords are zero-indexed).
    • I corrected this "off by one" error.
  • Move direction change logic into snake class
    • Changing direction, and determining whether a change in a particular direction is possible, is more appropriately encapsulated within the snake class.
    • I also created a helper method Snake::OppositeDirection() to encapsulate the logic of determining the opposite of a Snake::Direction value.
    • Following these changes, it was possible to make the Snake::direction member private.
  • Second snake
    • The adversary snake is a user controlled snake controlled using the w/a/s/d keys.
    • It has a green head to differentiate from the primary user.
    • Each snake has its own score that is displayed in the title bar and and the end of the same.
    • If a snake crashes into another snake, it dies. The snake that has been crashed into continues (unless they both crashed into each other's head).
    • A dead snake remains as an obstacle for the other snake.
    • The game continues until both snakes are dead.
  • Expiring food
    • Food expires over time, visibly fading until it disappears and is replaced with the next morsel, unless it is first consumed.
    • The speed at which food expires is random within a set range.

Rubric

Feature / refactoring Rubric criteria satisfied Evidence
Expiring food The project uses Object Oriented Programming techniques. here
Classes use appropriate access specifiers for class members. here and here
Class constructors utilize member initialization lists. here
Classes abstract implementation details from their interfaces. here
The project makes use of references in function declarations. here
Classes encapsulate behavior. here
Second snake The project accepts user input and processes the input. here
The project makes use of references in function declarations. here and here
Class constructors utilize member initialization lists. here
Move direction change logic into snake class Classes encapsulate behavior. here and here
The project uses Object Oriented Programming techniques. here and here
Classes abstract implementation details from their interfaces. here and here
The project demonstrates an understanding of C++ functions and control structures. here

Project structure

The project structure hasn't changed significantly from the starter project. Most changes have taken place within the context of that structure.

However, an additional class Food has been created to encapsulate the behaviour of a single piece of food, including its position and expiry. The code for Food is in src/food.h and src/food.cpp.

A full diff can be viewed here.

Contributors

samfrancesswwelch

Issues