Code Monkey home page Code Monkey logo

snake_game's Introduction

Snake_Game

License: MIT macOS Atom

Welcome to my Capstone Project for my Udacity's C++ Nanodegree!

For this project, I chose to improve the SDL game Snake inspired by this StackOverflow question and you can find Udacity's original repo here.

I will add/have added the following features to the game:
๐Ÿ The snakes dies when it runs into the edges of the grid, instead of going through
๐Ÿ Replay button to restart the game from zero
๐Ÿ Save the latest 5 highest scores
๐Ÿ There are four different types of food that affect the snake! The bad foods timeout after a couple of seconds. The meaning of every food is as follows:
ย ย ย ย ๐ŸŸข Good food that will increase your score, the snake grows and the speed will go up
ย ย ย ย ๐Ÿ”ต Good food that will give you one extra live! You can gather up to 4 lives
ย ย ย ย ๐ŸŸ  The snake will slow down and shrink one block. You lose 1 point from your score
ย ย ย ย ๐Ÿ”ด Bad food that will take one live from the snake!

Snake gameSnake game

File and Class Structure

The folder and file structure of the project is as follows:
๐Ÿ“‚ cmake folder where extra .cmake files are stored. FindSDL2_image.cmake file helps locate the SDL and SDL_Image libraries in the computer
๐Ÿ“‚ imgs folder with the images for the replay screen and the readme
๐Ÿ“‚ include folder with all the .hpp files
๐Ÿ“‚ src folder with all the .cpp files
๐Ÿ“‚ data folder contains the .txt file with the 5 highest scores in the game

The class structure is as follows:
๐Ÿ A Snake class defined in the file snake.hpp and instantiated in the file snake.cpp. This class handles all the snake features, such as size, life status, body, head location, speed and reset.
๐ŸŽฎ A Controller class defined in the file controller.cpp and instantiated in the file controller.cpp. This class handles all the user interactions (keys pressed and mouse events).
๐Ÿ–ฅ A Renderer class defined in the file renderer.cpp and instantiated in the file renderer.cpp. This class takes care of the graphics of the game (colour of the snake, the food, creating the window, etc.).
๐Ÿ‘พ A Game class defined in the file game.cpp and instantiated in the file game.cpp. This class handles the game loop (input, update, render).

The main.cpp file creates objects type Game, Renderer and Controller, to interact with each other.

Environment

๐Ÿ‘‰ macOS 12.1 Monterey with 8-Core Intel Core i7 chip
๐Ÿ‘‰ Homebrew 3.3.5
๐Ÿ‘‰ cMake 3.21.3
๐Ÿ‘‰ GNU Make 3.81
๐Ÿ‘‰ C++ 17
๐Ÿ‘‰ Apple clang version 13.0.0
๐Ÿ‘‰ SDL 2.0.18. Download SDL from the official website.
๐Ÿ‘‰ SDL_Image 2.0

Compiling and Running

SDL_Image 2

Ubuntu

Run the following command in the terminal

sudo apt-get install libsdl2-image-dev

macOS

Download the binary (.dmg) from here and wait for it to download. Then move the SDL2_image.framework folder to /Library/Frameworks/.

If you do not have the correct permission in the computer (i.e. root or admin), read the ReadMe.txt inside the downloaded .dmg

Compile

Create a build folder and change to that directory

mkdir build && cd build

Within the build directory run

cmake .. && make

Running

The executable is within the build directory. You can run it as follows

./Snake

Rubric Points

Group Rubric File Line Numbers
Loops, Functions, I/O The project demonstrates and understanding of C++ functions and control structures snake.hpp
renderer.hpp
8 11
14 23 31
The project reads data from a file and process the data, or the program writes data to a file game.cpp 141 160
The project accepts user input and process the input controller.cpp 17
Object Oriented Programming The project uses Object Oriented Programming techniques main.cpp 18 19 20 21
Classes use appropriate access specifiers for class members snake.hpp
food.hpp
controller.hpp
renderer.hpp
game.hpp
10 46
12 26 35 47
10 14
33 43
20 33
Class constructors utilise member initialisation lists controller.cpp
snake.hpp
food.cpp
game.cpp
renderer.cpp
6
21
20
5
6
Classes encapsulate behaviour snake.hpp
food.hpp
23-40 48-49
13-22 36-45
Overloaded functions allow the same function to operate on different parameters renderer.hpp 39 40
Memory Management The project makes use of references in function declarations renderer.hpp
game.hpp
controller.hpp
food.hpp
snake.hpp
40
25
11 22
44 45
49
The project uses smart pointers instead of raw pointers renderer.hpp
game.hpp
controller.hpp
food.hpp
40 45 46 47
34
11 22
48

snake_game's People

Contributors

michellemouse avatar

Watchers

 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.