I added two new features to the basic snake game: walls and different food types.
The walls are gray spots in the game that cause the snake to die when they are hit.
I decided to create four different food types that affect the speed and direction of the snake: normal, fast, slow, and reverse. All food types cause the snake to grow in lenth by 1
- The normal food type increases the snake's speed by 0.02
- The fast food type increases the snake's speed by 0.04
- The slow food type decreases the snake's speed by 0.02
- The reverse food type causes the snake to switch directions
-
Loops / functions / IO Inputs
- Functions
- I created several new functions: EatFood in snake.cpp, PlaceWall in game.cpp, and SetFoodColor in renderer.cpp
- Each function I created either deals with the different types of foods to force the snake to do different things or places walls
- I also edited many functions in the code to take in a Food instance instead of an SDL_Point for the food items.
- Control Structures
- I added switch statements on both food type and direction to do different things when different foods are eaten and to implement my reverse direction food
- I used a variety of vector methods including push_back, pop_back, and reverse to reverse the direction of my snake.
- Functions
-
Object oriented programming
- I created a new class Food that holds the type of food and the location. When a new piece of food is created, the food constructor randomly assigns a type. The food types are contained in an enum which is used in game, snake, and render
- In Food.h, I overrode the cout function to allow the type of food to be printed to the console.
- In Food.h, I defined both the construtor and the destructor
- New methods are self documenting such as Snake::EatFood, Renderer::SetFoodColor, and Game::PlaceWall
- I specified every new variable as either private or public as apprpriate
-
Memory Managment
- SetFoodColor and EatFood both pass food in as a reference.
- 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
- Clone this repo.
- Make a build directory in the top level directory:
mkdir build && cd build
- Compile:
cmake .. && make
- Run it:
./SnakeGame
.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.