This is the capstone project for C++ Nanodegree program. For this project the Option 1 has been chosen and has been realised as Snake-like game with bots.
The player's Snake shall follow the mouse pointer as long as there is focus on the game's window. Very basic collision detection has been built in. The player's target is to eat as much fruits as it is possible and do not be eaten. Bots have very rudimentary logic - they go from fruit to fruit, and new target is chosen with every collision of bot with fruit/powerup or level edge.
Changes made in options shall be directly after saving them, visible in the game.
- Snaker - main directory
- assets - here is the settings file and mapping file.
- fonts - here are fonts used in project (Horta.ttf is used)
- images - backgrounds, images, textures,
- lang - translations
- cmake - cmake search files
- readme_files - gif which you see on the top of this page.
- src - source files, main.h and main.cpp
- classes - here the classes (.h and .cpp files)
- Win - generates window, renderer and responsible for window events
- Timer - delivers timing functions
- Settings - load and saves settings from file
- LTexture - loads and render textures
- Tile - class for generating and rendering a tile for background.
- GameMenu - generates game menu, handles events for it
- Game - game logic, provides intermediate layer for events handling
- Fruit - base class that generates and renders basic fruits
- PowerUp - child class of Fruit specialised in PowerUps
- Snake - base class for Snakes (player and bots)
- Player - class for generating, event handling and rendering of Player's snake
- Bot - class for generating and rendering of bot's snakes
- functions - directory with files containing generic functions (.h and .cpp files)
- collisions - function for detecting collisions between objects
- initSDL - SDL function for initialising and basic configuration of library
- moveFruit - template functions for moving fruits/powerups to the Snake
- classes - here the classes (.h and .cpp files)
- assets - here is the settings file and mapping file.
The projects codebase at the begineng was about 2 years old. Part of it has been rewritten, but classes like Win
, LTexture
, Timer
, Tile
were only "refurbished" to meet project's specification.
- 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
- SDL>=2.0
- Linux (available through packaging system). SDL2 Packages needed:
- SDL2 (package libsdl2 on debian):
sudo apt-get install libsdl2-dev
- SDL2 image (package libsdl2-image):
sudo apt-get install libsdl2-image-dev
- SDL2 ttf (package libsdl2-ttf):
sudo apt-get install libsdl2-ttf-dev
- SDL2 (package libsdl2 on debian):
- Windows & Mac & other Linuxes - installation instructions can be found here
- Linux (available through packaging system). SDL2 Packages needed:
- 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
- Clone this repo.
- Go to directory where you will be working in.
- Open terminal and type
git clone https://github.com/Lukasz-Pe/Snaker.git
. - Hit enter.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./Snaker
.
If you want, just copy the command below:
- for build only:
mkdir build && cd build && cmake .. && make && ./Snaker
- for clone and build:
git clone https://github.com/Lukasz-Pe/Snaker.git && cd Snaker && mkdir build && cd build && cmake .. && make && ./Snaker
-
Loops, Functions, I/O
- file src/fucntions/moveFruis.cpp constains nested functions and function
moveFruitsAndPowerUps()
of class Game in /src/classes/Game.cpp utilizes for loop. - file operations are realised in class Settings in file /src/classes/Settings.cpp, functions loadSettings loads file and function saveSettings saves file.
- User input is being accepted by functions eventHandler in classes Game, GameMenu and Snake (with child classes) and is processed by functions updating setting values or calculating angle to given target (mouse pointer position inside window).
- file src/fucntions/moveFruis.cpp constains nested functions and function
-
Object oriented programming
-
For classes organisation please refer to section Directory/project structure.
-
All classes use access specifiers.
-
Classes
Snake
,Bot
,Player
,Game
andGameMenu
use initializer list. -
In all cases the implementation details are abstracted from interfaces.
-
Data encapsulation is provided for all classes.
-
Inheritance is used accordingly. Please refer to section Directory/project structure.
In all other cases composition was used as needed.
-
Overloading of functions has been used in
LTexture
class. -
Classes
Snake
,Player
andBot
are using dynamically allocated memory. -
Class
Snake
provide pure virtual functions which are overridden by classesPlayer
andBot
. -
In class
Timer
is template functiongetSeconds
. Functions in moveFruits.cpp are template functions.
Class Snake has also template function called
calculateNearestTargetPosition
. -
-
Memory management
- All classes use passing by reference and almost all functions are using pass-by-reference.
- No class made use of unmanaged dynamic memory allocation. Where it was possible the smart pointers were used.
- RAII with scopes is used in
Settings
class in functionsloadSettings
andsaveSettings
for opening, accessing, modifying and releasing the files. - Classes
Player
andBot
implement rule of five. - Move sematics is used in
Player
andBot
classes in implementation of rule of five. - In all classes where no SDL typical were passed around, smart pointer were used.
In class
Snake
is for exampleshared_ptr
toSettings
object, and inmain.cpp
isunique_ptr
to font object. -
Concurrency has not been implemented in this project.