Skip to content

cemdusenkalkan/capstone-snake-game

 
 

Repository files navigation

Capstone Snake Game

The original project had a fully functional Snake game implemented. A Snake game has a keyboard controlled snake that's constantly moving. When it eats a food (by running over it), it grows in length by one block while still moving. The original implementation infinitely runs until the snake runs into itself.

Original implementation:

  • A main function handling creating objects of the three main steps of the game loop (Controller, Game, Renderer). The Game::Game constructor creates a Snake object, while Game::Run starts the game loop.
  • A Game::Update method that takes care of snake movement, handling the snake eating food, basic movement in response to user input.
  • Score updated regularly.
  • The Snake stores float coordinates of the head, and a vector of points (int cell coordinates) as the body. The head has float coordinates to suggest speed as a gradient, versus blockily moving from point to point. Location updates based on the snake's speed are all handled. The Snake::Update method both handles updating the head and the body.
  • Most of the SDL usage is already written.

The project spec asked to create features to the original game which used concepts discussed through the course.

Added features:

  • Create a GUI with speed (frames per second) toggling, leaderboard option, original mode, obstacle mode, computer snake mode. Used Nuklear to do so.
  • Leaderboard is read and written from a *.txt file. The text file is encrypted using a symmetric key. I kept the key up on GitHub, so it's not like it's actually a security improvement. I just felt very uncomfortable leaving a plaintext file with data public. I could've left an optional parameter to toggle encryption, but decided to make it a mandatory design feature.
  • New Obstacle class with inheritance (fixed, moving).
  • New snake controlled by A* algorithm, which will lead to a loss if run into it or automatic win/rankings multiplier boost if eat the snake by hitting the last box of it.
  • Gave the A* algorithm snake a handicap where it can't use the wraparound to go across the screen.
  • End print message with score, size, and leaderboard ranking (two leaderboards - respective mode and the general leaderboard).

How to Run

  1. Download build/SnakeGame and run using ./SnakeGame from Unix command line in same directory.

Or:

  1. Create own executable by cloning repo.
  2. If no build directory, create one mkdir build.
  3. Compile cd build && cmake .. && make.
  4. Run ./SnakeGame

Notes

  • Figured out how to make flashing colored items! Have two SDL_SetRenderDrawColors next to each other and it will effectively be a strobe light!

  • Renderer and Controller don't own any of the class representations e.g. Snake, Obstacle. They just take in references or copies and either draw out a renderering from a const object or mutate values within the object. The food is implemented as an SDL_Point and not a class, which is perfect, but for some reason it's being passed in as a reference to the Renderer instead of another deep copy. I am not sure why the original project creators decided to introduce that inconsistency. I may change it later.

  • Renderer contains no game logic and takes in only const values to render a snapshot of all logic representations.

  • Speed mode is 100% solved without deeply touching classes (handled completely in main.cpp).

Dependencies for Running Locally

Credits

Udacity team credited this excellent StackOverflow thread.

Links

https://dexp.in/articles/nuklear-intro/ https://cpp.hotexamples.com/examples/-/-/nk_option_label/cpp-nk_option_label-function-examples.html https://www.geeksforgeeks.org/encrypt-and-decrypt-text-file-using-cpp/

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

A 2D Snake game with creative additions; uses C++ and SDL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 88.6%
  • Makefile 8.2%
  • C++ 2.3%
  • CMake 0.7%
  • Python 0.2%
  • Shell 0.0%