Phys291 Project: Snake

CONTENT

Introduction

In this project I am going to create a Snake game that can be playd in Linux Terminal. In the game of Snake the player moves a small dot in line to collect "food" appearing randomly in the playarea. The goal of the game is to get as much points and growing as long as possible which is achieved by eating food on the screen. The player loses if the snake runs into the playarea borders or into its own tail. For this project I will be using C++ programing language and terminal to run the game.

Program Desctiprion

The program is written in C++ and uses the standard librery with one exeption which is "ncurses.h". The program is based on a Snake game written by me 6 years ago in C# using Visual Studios on Windows. It uses also a _kbhit() function written by Morgan McGuire. In this program the user uses arrow-keys to control the snake in a predefined playarea. For each piece of food eaten by the snake the player gets one piont and the snake grows by one unit of lenght. The program also logs keystrokes thet can be used to desplai in a histogram for total moves and for exemple moves in each direction.

Method

In the beginning the idea was to use TCanvas to display the playara and GetEvent() function, but for after using a lot of time on trying to get it to work the best option was to drop the idea and move on with a different approach.

After doing some reaserch and looking at some exemples of a snake geme for the terminal on the internet the best way to develop this program was to use the old code writen by me as a base. For that the ncurses librery was needed in order to be able to use functions like getch() to determine whish key was pressed on the keyboard. Before that the program had to know if any key was pressed at all. This proved to be more difficult then anticipated. After many atempts of trying different things and functions, the desperate solution was to use a function found on the internet kreated by Morgan McGuire. After that the rest was preaty much straight forward and the result is a game of Snake printed out in Linux terminal.

ncurses.h and _kbhit()

For this program ncurses librery developed by GNU Project and _kbhit() function created by Morgan McGuire was used.

Ncurses is equivalent of conio.h librery for Windows, it allows the programer to write Text based User Interfac(TUI). The reason why ncurses had to be used was to get the input form the keyboard arrow-keys. This proved to be the easiest solution given the time at thet moment.

The _kbhit() function returnes a TRUE or FALSE value depending on if the a key on the keyboard was hit which was needed to do further test for whish key was pressed.

Conclusion

The project turned out to be slightly more difficoult then first anticipated, but it made it more satisfying when things startet working. Now the program works just as planed. One thing that could be improved is the low refreshtime. Sometimes it can be hard to hit the food because of the lag. With more time there are some things that can be implemented, like for exemple: the ability for the user to change the playare-size or the speed of the game. These are some simple ideas for further developemnt of the program. The other thing that did not make it to the final program because of lack of time is the function thet prints a histogram of live feed from keystrokes. This can be easy implementet since the data i stored automaticly while the program is runing.
In the end the project ended up beaing a success.

Update Log

Snakev2: Added three difficutlties, Hard, Medium and Easy. increese in difficulty increases speed at which the snake is moving.

Snakev2WKeylog: Added a Keystroke logger that displays the amount of turns made during one game.