Coder Social home page Coder Social logo

snaker's Introduction

Snaker

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.

Directory/project structure:

  • 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

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.

Dependencies for compiling and running locally

Basic Build Instructions (for linux)

  1. Clone this repo.
    1. Go to directory where you will be working in.
    2. Open terminal and type git clone https://github.com/Lukasz-Pe/Snaker.git.
    3. Hit enter.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. 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

Project specs (from optional section)

  1. Loops, Functions, I/O

    1. file src/fucntions/moveFruis.cpp constains nested functions and function moveFruitsAndPowerUps() of class Game in /src/classes/Game.cpp utilizes for loop.
    2. file operations are realised in class Settings in file /src/classes/Settings.cpp, functions loadSettings loads file and function saveSettings saves file.
    3. 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).
  2. Object oriented programming

    1. For classes organisation please refer to section Directory/project structure.

    2. All classes use access specifiers.

    3. Classes Snake, Bot, Player, Game and GameMenu use initializer list.

    4. In all cases the implementation details are abstracted from interfaces.

    5. Data encapsulation is provided for all classes.

    6. Inheritance is used accordingly. Please refer to section Directory/project structure.

      In all other cases composition was used as needed.

    7. Overloading of functions has been used in LTexture class.

    8. Classes Snake, Player and Bot are using dynamically allocated memory.

    9. Class Snake provide pure virtual functions which are overridden by classes Player and Bot.

    10. In class Timer is template function getSeconds. Functions in moveFruits.cpp are template functions.

    Class Snake has also template function called calculateNearestTargetPosition.

  3. Memory management

    1. All classes use passing by reference and almost all functions are using pass-by-reference.
    2. No class made use of unmanaged dynamic memory allocation. Where it was possible the smart pointers were used.
    3. RAII with scopes is used in Settings class in functions loadSettings and saveSettings for opening, accessing, modifying and releasing the files.
    4. Classes Player and Bot implement rule of five.
    5. Move sematics is used in Player and Bot classes in implementation of rule of five.
    6. In all classes where no SDL typical were passed around, smart pointer were used.

    In class Snake is for example shared_ptr to Settings object, and in main.cpp is unique_ptr to font object.

  4. Concurrency has not been implemented in this project.

snaker's People

Contributors

lukasz-pe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.