Skip to content

RichardvdK/CppND-Capstone-Snake-Game

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPPND: Capstone Snake Game Example

This is a starter repo for 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.

The Capstone Project gives you a chance to integrate what you've learned throughout this program. This project will become an important part of your portfolio to share with current and future colleagues and employers.

In this project, you can build your own C++ application or extend this Snake game, following the principles you have learned throughout this Nanodegree Program. This project will demonstrate that you can independently create applications using a wide range of C++ 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.

CC Attribution-ShareAlike 4.0 International

Shield: CC BY-SA 4.0

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

CC BY-SA 4.0

Additional information for Capstone

This program additionally uses the sdl2 image library. See Dependencies for Running Locally above.

New Features:

Eagle

It is widely known that eagles eat snakes, so I added an eagle to the game that chases the snake and tries to eat it! The eagle follows the snake, and on contact, the game terminates with the message "Eagle caught the snake!". The speed of the eagle is always relative to that of the snake, and this value changes with different difficulty levels. The renderer loads a nice image of an eagle which moves across the screen to try and catch the snake.

Difficulty levels

Three difficulty levels are added to the game: Easy, Medium, and Hard. The difficulty level changes the relative speed of the eagle, making it easier or harder for the eagle to catch the snake. You can change the difficulty by pressing the left and right bracket keys "[" and "]". Easy = 0, Medium = 1, and Hard = 2.

Extra food

I added an extra option that spawns extra green food every 10 seconds. You have 3 seconds to catch the extra food and obtain an additional +5 points! After that, you have to wait 7 seconds before new extra food spawns randomly and you can try again.

Compiling and Testing

The program compiles and runs the same way as it is described in Basic Build Instructions.

Project Contributions Related to Rubrics

I added a lot of new functionalities with all kinds of techniques. I tried to name most of them below, but for all the changes and updates, I refer you to the "diff" section of GitHub for all the changes I made.

Loops, Functions, I/O:

2.1: The project demonstrates an understanding of C++ functions and control structures:

The project uses numerous control structures throughout the program to control the flow of the game.

2.2: File I/O:

The program loads an image "eagle_small.png" in renderer.cpp as an SDL_Surface and renders it to the screen accordingly.

2.3: The project accepts user input and processes the input:

The program handles additional keyboard inputs. Namely the bracket keys "[" and "]" to change the difficulty of the game.

2.4: Constructor Argument Change:

In the renderer class, the argument of Renderer has been changed to const Snake& to introduce an immutable variable. Additionally, the eagle class uses getters and setters for its private members.

Object-oriented programming:

3.1: Access Specifiers:

The snake and eagle classes now use appropriate access specifiers for class members.

3.2: Class constructors utilize member initialization lists:

The eagle class uses member initialization lists to initialize the class.

3.3: Classes abstract implementation details from their interfaces:

Member functions do not change the program state in undocumented ways. Additionally, the Snake class has been reorganized.

Memory Management:

4.1: The project makes use of references in function declarations:

References are used throughout the program to pass variables by reference. For example in the Renderer::Render() method.

4.2: Proper Use of Destructors:

The project uses destructors properly.

4.6: The project uses smart pointers instead of raw pointers:

For example, the eagle class has a shared pointer to the snake and uses that to chase the snake.

Concurrency:

5.1: The project uses multithreading:

The project uses multithreading to spawn extra food on a timer. The PlaceExtraFoodTimer() function is started on a separate thread and has a timer to create additonal food for extra points. The program waits for the thread and exits cleanly. An atomic variable is used to signal the ExtraFoodThread that the program has exited on both a SIGINT and on clicking the cross to exit the program (SDL_QUIT).

5.3: A mutex or lock is used in the project:

The lock type unique_lock is used to protect variables such as extra_food and extra_food_placed.

5.4: A condition variable is used in the project:

A condition variable is used to notify the thread that extra food has been placed.

Extra notes:

Good luck with getting the high score! On the difficulty high, I found it extremely hard to get a score above 20, so I am curious if people get more :).

About

A 2D Snake game using C++ and SDL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 82.5%
  • CMake 17.5%