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
). TheGame::Game
constructor creates aSnake
object, whileGame::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. TheSnake::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).
- Download
build/SnakeGame
and run using./SnakeGame
from Unix command line in same directory.
Or:
- Create own executable by cloning repo.
- If no build directory, create one
mkdir build
. - Compile
cd build && cmake .. && make
. - Run
./SnakeGame
-
Figured out how to make flashing colored items! Have two
SDL_SetRenderDrawColor
s next to each other and it will effectively be a strobe light! -
Renderer
andController
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 aconst
object or mutate values within the object. The food is implemented as anSDL_Point
and not a class, which is perfect, but for some reason it's being passed in as a reference to theRenderer
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 onlyconst
values to render a snapshot of all logic representations. -
Speed mode is 100% solved without deeply touching classes (handled completely in
main.cpp
).
- cmake >= 3.7
- All OSes: click here for installation instructions
- make >= 4.1 (Linux, Mac), 3.81 (Windows)
- Linux: make is installed by default on most Linux distros
- Mac: install Xcode command line tools to get make
- Windows: Click here for installation instructions
- SDL2 >= 2.0
- All installation instructions can be found here
Note that for Linux, an
apt
orapt-get
installation is preferred to building from source. - gcc/g++ >= 5.4
- Linux: gcc / g++ is installed by default on most Linux distros
- Mac: same deal as make - install Xcode command line tools
- Windows: recommend using MinGW
Udacity team credited this excellent StackOverflow thread.
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/
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.