This is a starter repo for the Capstone project in the Udacity C++ Nanodegree Program. The code for this repo was inspired by this excellent StackOverflow post and set of responses.
In this project, I extend this Snake game to the following directions:
- add obstacles (shown as grey rectangles) to the map
- add another snake which follows the path (shown as green grids) calculated from A* algorithm.
- allow user to enter their names before playing game
- log user's score in a file named
users_info.txt
located inbuild
folder.
The above is a short video to illustrate the extended game.
The most time-consuming part when I implemented the extensions is to determine the direction for each move according to the path from A* algorithm.
The moving speed is not accurate 0.1 so this cause some trouble to predict the accurate position after each move.
Through careful observations, I realize we can use static_cast<int>(head_x/y)
instead of head_x/y
because we only care about the head's integer position in rendering and determining whether it arrives at the food.
So I use static_cast<int>(head_x/y)
to infer the moving direction and determine whether we should pop out the previous goal (last grid in path) dynamically.
- 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.