Skip to content

This C++ game project is built using SDL2.0 game library. Built on top of a basic snake game, this project now has a peacock as a moving obstacle, displays different food items for the snake, and a snake whose shortest path to food is computed using A star search algorithm, all of these can be chosen as per player's wish before the game begins. …

License

Notifications You must be signed in to change notification settings

ram-ravan/SnakePeacock-Game

Repository files navigation

CPPND: Capstone SnakePeacock Game

This is my Capstone project in the Udacity C++ Nanodegree Program. The code for this repo was inspired by this excellent StackOverflow post and set of responses.

This project builds upon the startup code of Snake game provided by Udacity Udacity C++ Nanodegree Program.

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.

Tasks implemented

  1. Allowed users to enter their names and save it in a text file with their scores, so that one can get the top score
  2. Allowed the user to select the initial speed (Slow - Fast)
  3. Added a peacock as a moving obstacle to the game
  4. Displayed More than one type of food in the game
  5. Included another immortal snake to the game and allowed the computer to control it using AStar search algorithm
  6. User can choose between different game modes such as 1. Playing against a computer or 2. Play with peacock as a moving obstacle or 3. Have both in the game or 4. To play a basic snake game

New dependencies needed: SDL2_image (details in Dependencies for Running Locally section above)

Code started and worked for long in this github repository and the final code submitted is available in this github repository

Note

  • Comments are added (with task numbers as listed above) to the code in important places so that different tasks can be identified easily
  • Apart from the major improvements and modificatins listed below for each task, there are other minor changes made to reflect the major changes that are not mentioned here
  • Scores of the snake that is controlled by computer is not stored as it is immortal and possess more power than the player

Code Structure

Below is the code structure of the SnakePeacock Capstone project that shows the program flow through important components of the game.

This SnakePeacock game built up on the starter code implements many features such as allowing the users to enter their names to store and display (high) scores, let the user choose their game environment such as to let the user just play a basic snake game, or let the user choose peacock as a moving obstacle, or play against another snake that is controlled by the computer or to have both peacock and another snake in the game.

This new code structure of the completed project is inspired from the code structure below and carefully made changes such that the structure is intiutive and matching to the structure of the starter code as much as possible. This lets the mentor spot the new features and changes in the completed project comparing the code structure of the starter code.

Below are the details of all the tasks implemented followed by the project rubric points addressed.

Task 1

Objective - Allow users to enter their names and save it in a text file with their scores, so that one can get the top score

C++ Concepts involved - Basics of C++ and basics of Object Oriented Programming, File I/O

Steps performed to achieve the goal of the task

  1. Created scoreCard.h and scoreCard.cpp with necessary data structures and its attributes, methods, constructors, destructors etc.
  2. In the main(), an object of type scoreCard is created to invoke the constructor of Struct scoreCard
  3. Read/Write to file operations are implemented to sort score and display top scores etc.
User input(s) - Player name
Expected output/behaviour - User gets instructions and enters the name in command line on executing ./SnakeGame in the build directory

Task 2

Objective - Allow the user to select the initial speed (Slow - Fast)

C++ Concepts involved - Basics of C++ and basics of Object Oriented Programming

Steps performed to achieve the goal of the task

  1. Created startSpeed.h and startSpeed.cpp with necessary data structures and its attributes, methods, constructors, destructors etc.
  2. In the main(), an object of type startSpeed is created to invoke the constructor of Struct startSpeed
  3. Changed score type from int to float
  4. Changed the arguments list of Game::Game(...) to set a new private attribute named initSpeed with a user selected start speed value and modified Game::Update() to set different score increments for different modes (slow-speed)
  5. Snake::Snake(...) arguments list changed, attribute Snake::speed gets initialised according to user's speed choice instead of default initialisation with 0.1f
  6. Renderer::UpdateWindowTitle(..) arguments list modified and sstream library is used to display float with a score value of single precision.
User input(s) - User's choice of speed
Expected output/behaviour - User gets prompted with three choices slow, normal and fast in the command line followed by entering the player name (Task 1) and choosing the game environment (Task 6)

Task 3

Objective - Add a peacock as a moving obstacle to the game

C++ Concepts involved - Object Oriented Programming, Concurrency

Steps performed to achieve the goal of the task

  1. New header file and source file created for peacock which is a moving obstacle in the game
  2. Game::Update() (later changed to Game::UpdateSnake()) is called in the game loop using std::async(...) to make the game run faster
  3. A new peacock instance is created in Game class and peacock constructor receives the grid width and grid height attributes from game class
  4. Peacock moves one grid per second to one of eight directions possible in its surrounding
  5. This movement of peacock is done by calling Peacock::Update(...) from game loop once in a second
  6. Game::Update() is updated such that snake gets killed when its head or body touches the peacock in the game
  7. Renderer::Render(...) method now has Peacock instance in its arguments list
  8. Renderer::RenderPeacock(...) method receives Peacock's instance from Renderer::Render() to render it to the screen as an image using SDL_RenderCopy(...)
User input(s) - None
Expected output/behaviour - Now, a moving peacock image is rendered to the screen with the help of SDL_image library (instructions to SDL_image is in Dependencies for Running Locally section above)
Image location: Found in directory images in the same level as other project directories such as src, build etc.

Task 4

Objective - Display More than one type of food in the game

C++ Concepts involved - Object Oriented Programming

Steps performed to achieve the goal of the task

  1. Changed the type of Game::food from SDL_Point to SDL_Rect and change in all the source and header files accordingly to reflect the changes
  2. A new method Game::FoodCell(..) was created to check if any other object is in the same grid cells where the food is
  3. Created a new method Renderer::InitFoodImages() to let Renderer::foodImages attribute to hold the image paths
  4. A new method Renderer::RenderFood(...) was created to render the food images to the screen
User input(s) - None
Expected output/behaviour - Many food images are listed as images using SDL_image library (instructions to SDL_image is in Dependencies for Running Locally section above)
Image location: Found in directory images in the same level as other project directories such as src, build etc.

Task 5

Objective - Included another immortal snake to the game and allowed the computer to control it using AStar search algorithm

C++ Concepts involved - - Object Oriented Programming, Concurrency

Steps performed to achieve the goal of the task

  1. New header file and source file created for an another snake that plays as computer based on user's decision
  2. User's choice of whether or not to play against another snake that is controlled by computer is received using playComputer() method in main() function
  3. Create an instance of class SnakeComp in Game class and use it to set the attributes of the class and to call its methods to implement its functionality
  4. A new method named Game::UpdateSnakeComp() is created to update the SnakeComp's members that further calls Game::AStarSearch()
  5. Game::AStarSearch() further calls all neccessary methods to direct the SnakeComp using AStarSearch algorithm
  6. The name of the existing Game::Update() method is now changed to Game::UpdateSnake()
  7. Game::UpdateSnakeComp() (in the game loop) and Game::AStarSearch() are called using std::async(...) to improve the performance of the game
  8. Renderer::Render(...) arguments list changed accordingly to include the instance of SnakeComp
  9. Renderer::RenderSnakeComp(...) method receives SnakeComp's instance from Renderer::Render() to render it to the screen
  10. In the method Renderer::UpdateWindowTitle(...), changes are made such that the scores of both player and computer can be displayed
User input(s) - None
Expected output/behaviour - User can play along an immortal snake that resists attacks from peacock and that is completely immortal. By choosing this option, user gets to play with another snake that is controlled by the computer

Task 6

Objective - User choose between

  1. Playing against a computer or
  2. With peacock as a moving obstacle or
  3. Have both in the game or
  4. To play a basic snake game

C++ Concepts involved - Basics of C++ and basics of Object Oriented Programming

Steps performed to achieve the goal of the task

  1. A function named UserEnvSetUp() is implemented in main.cpp file to let the user choose between playing against a computer or with peacock as a moving obstacle or have both in the game or to play a basic snake game
  2. Changes in arguments list of Game::Game(...) and Peacock::Peacock(...) are made accordinglr to adopt this feature
  3. Modifications to code in one or more methods of class Game and class Renderer are made to adopt to this game feature
User input(s) - User's desired choice of game environments whether to
1. Play against computer or
2. To play with a moving obstacle or
3. Have both in the game or
4. To play a basic snake game
Expected output/behaviour - User prompted with different options of possible game environments as listed above

Rubric points addressed

  1. The project demonstrates an understanding of C++ functions and control structures
  • main.cpp: lines 48, 54
  • game.cpp: lines 142, 171, 186, 230, 235, 247
  • peacock.cpp: lines 16, 30
  • peacock.h: line 20
  • startSpeed.cpp: lines 9, 26
  • scoreCard.cpp: lines 16, 28, 21, 35, 41, 56, 58, 84, 102, 112
  • snakeComp.cpp: lines 17, 23, 50, 70, 73
  1. The project reads data from a file and process the data, or the program writes data to a file
  • scoreCard.cpp: lines 9, 10, 38, 53
  1. The project accepts user input and processes the input
  • main.cpp: lines 62, 25, 26
  • scoreCard.cpp: line 18
  • startSpeed.cpp: line 22
  1. The project uses Object Oriented Programming techniques
  • peacock.h: lines 10 - 44
  • snakeComp.h: lines 12 - 57
  • peacock.cpp: all method definitions in the file and usage of member attributes in those methods
  • snakeComp.cpp: all method definitions in the file and usage of member attributes in those methods
  1. Classes use appropriate access specifiers for class members
  • peacock.h: lines 12, 37
  • snakeComp.h: lines 14, 50
  • snake.h: line 37 (changed from private to protected)
  1. Class constructors utilize member initialization lists
  • peacock.h: lines 15 - 18
  • snakeComp.h: lines 19 - 22
  • game.cpp: lines 9 - 10, 14 - 16
  1. Classes abstract implementation details from their interfaces
  • game.cpp: new methods added across different task implementations, comments across the file helps identify methods part of different tasks
  • peacock.cpp: all method definitions in the file and usage of member attributes in those methods
  • snakeComp.cpp: all method definitions in the file and usage of member attributes in those methods
  1. Classes encapsulate behavior
  • peacock.h: contains methods and attributes related to peacock
  • snakeComp.h: contains methods and attributes related to snakeComp
  • game.cpp: line 161 - member attribute's state accessed via a member function
  • startSpeed.h: lines 13, 17 - member attribute's state accessed via a member function
  1. The project makes use of references in function declarations
  • renderer.h: lines 28 - 30
  1. The project uses multithreading
  • game.cpp: lines 38, 42, 135

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

About

This C++ game project is built using SDL2.0 game library. Built on top of a basic snake game, this project now has a peacock as a moving obstacle, displays different food items for the snake, and a snake whose shortest path to food is computed using A star search algorithm, all of these can be chosen as per player's wish before the game begins. …

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •