diff --git a/SAVEGAME b/SAVEGAME new file mode 100644 index 0000000..925f347 Binary files /dev/null and b/SAVEGAME differ diff --git a/SNAKE.BAK b/SNAKE.BAK index be06b86..103f5f0 100644 --- a/SNAKE.BAK +++ b/SNAKE.BAK @@ -3,7 +3,7 @@ SNAKE XENZIA ------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - This is a basic snake game which is commonly + This is a basic snake game which is commonly found in old nokia phones. This game has a snake the player has to move with the arrow keys. There will be food particles generated @@ -37,12 +37,12 @@ #include #include -#define ARR_X 200 -#define ARR_Y 200 //dimensions of pos_key matrix +#define ARR_X 100 +#define ARR_Y 100 //dimensions of pos_key matrix char pos_key[ARR_X][ARR_Y]; //matrix corresponding to every position on the screen -void keystroke(); //function which receives and maps keystrokes and decides the corresponding movement +void keystroke(int); //function which receives and maps keystrokes and decides the corresponding movement void adjustxy(int&, int&); //helps in swapping screen sides when snake reaches one edge of screen void movehead(int, int); //prints the head of the snake void movetail(char [ARR_X][ARR_Y]); //replaces the last element of the snake with " " @@ -55,7 +55,8 @@ void screen(); //sets all screen parameters at the beginning of the program void frame(); //prints the borders of the screen int pause(); //manages pause option int select(int , int , int [], int); //helps in option selection in the game menus with arrow keys -void highscore(int); +void highscore(int); //to print and modify highscores +void continuegame(int, char &); int MAX_X; //maximum horizontal coordinate of screen int MIN_X; //minimum horizontal coordinate of screen @@ -73,6 +74,7 @@ int x2, y2; //food coordinates int snaklen; //records length of the snake(to show score at end) int frame_width = 150; //sets default game speed int counter = 0; //records the no. of times the game is played +int savedgamestatus; void main() { @@ -81,12 +83,13 @@ void main() { randomize(); //to seed the random() function in addfood() screen(); + continuegame(2, 'k'); } ///////WELCOME SCREEN////////// int choice; char choice2; flag3: - snaklen = 1; + snaklen = 1; for(int l = 0; l=6 && i<9) p_s[i].difficulty = 50; } - +////HIGHSCORES WILL BE STORED IN BINARY FILE 'HIGHSCOR'//// ifstream finout; finout.open("HIGHSCOR", ios::in | ios::nocreate | ios::binary); +////TO CREATE 'HIGHSCOR' FILE IF IT IS NOT PRESENT//// if(finout == 0) { ofstream ftemp; @@ -771,6 +797,7 @@ void highscore(int s) } p_s[line_no].score = s; } +////////////////////HIGHSCORES PAGE///////////////////// clrscr(); frame(); gotoxy((MAX_X - MIN_X + 1)/2 - 5, (MAX_Y - MIN_Y + 1)/2 - 7); textcolor(YELLOW); @@ -815,14 +842,16 @@ void highscore(int s) } else { - _setcursortype(_SOLIDCURSOR); + gotoxy((MAX_X - MIN_X + 1)/2 - 10, (MAX_Y - MIN_Y + 1)/2 + 7); + cout<<"Enter your name..."; + _setcursortype(_SOLIDCURSOR); char name[8]; for(int i = 0; p_s[i].difficulty != frame_width; i = i + 3){} gotoxy((MAX_X - MIN_X + 1)/2 - 7, line_no + (i / 3) + (MAX_Y - MIN_Y + 1)/2 - 5); cout<<" "; gotoxy((MAX_X - MIN_X + 1)/2 - 7, line_no + (i / 3) + (MAX_Y - MIN_Y + 1)/2 - 5); - while(kbhit()){ getch(); } - cin.getline(name, 8); + cin.get(name, 9); + cin.ignore(1000, '\n'); strcpy(p_s[line_no].name, " "); strcpy(p_s[line_no].name, name); _setcursortype(_NOCURSOR); @@ -836,3 +865,81 @@ void highscore(int s) } } +void continuegame(int read, char &c) +{ + struct game_status + { + int x, y, x1, y1, x2, y2, snaklen, frame_width, MAX_X, MIN_X, MAX_Y, MIN_Y; + char pos_key[ARR_X][ARR_Y], c; + char screen[10000]; + int savedgamestatus; + }g_s; + if(read == 0) + { + g_s.x = x; + g_s.y = y; + g_s.x1 = x1; + g_s.y1 = y1; + g_s.x2 = x2; + g_s.y2 = y2; + g_s.snaklen = snaklen; + g_s.frame_width = frame_width; + g_s.MAX_X = MAX_X; + g_s.MIN_X = MIN_X; + g_s.MAX_Y = MAX_Y; + g_s.MIN_Y = MIN_Y; + for(int i = 0; i < ARR_X; ++i) + for(int j = 0; j < ARR_Y; ++j) + { g_s.pos_key[i][j] = pos_key[i][j]; } + g_s.c = c; + gettext(::MIN_X - 1, ::MIN_Y - 1, ::MAX_X + 1, ::MAX_Y + 1, g_s.screen); + g_s.savedgamestatus = savedgamestatus = 1; + ofstream fout; + fout.open("SAVEGAME", ios::out | ios::binary); + fout.write((char *) &g_s, sizeof(game_status)); + fout.close(); + } + else if (read == 1) + { + ifstream fin; + fin.open("SAVEGAME", ios::in | ios::binary); + fin.read((char *) &g_s, sizeof(game_status)); + fin.close(); + x = g_s.x; + y = g_s.y; + x1 = g_s.x1; + y1 = g_s.y1; + x2 = g_s.x2; + y2 = g_s.y2; + snaklen = g_s.snaklen; + frame_width = g_s.frame_width; + MAX_X = g_s.MAX_X; + MIN_X = g_s.MIN_X; + MAX_Y = g_s.MAX_Y; + MIN_Y = g_s.MIN_Y; + for(int i = 0; i < ARR_X; ++i) + for(int j = 0; j < ARR_Y; ++j) + { pos_key[i][j] = g_s.pos_key[i][j]; } + c = g_s.c; + clrscr(); + puttext(::MIN_X - 1, ::MIN_Y - 1, ::MAX_X + 1, ::MAX_Y + 1, g_s.screen); + savedgamestatus = g_s.savedgamestatus = 0; + ofstream fout; + fout.open("SAVEGAME", ios::out | ios::binary); + fout.write((char *) & g_s, sizeof(game_status)); + fout.close(); + } + else + { + ifstream fin; + fin.open("SAVEGAME", ios::in | ios::binary | ios::nocreate); + if(fin == 0) + { + savedgamestatus = 0; + return; + } + fin.read((char *) &g_s, sizeof(game_status)); + fin.close(); + savedgamestatus = g_s.savedgamestatus; + } +} diff --git a/SNAKE.CPP b/SNAKE.CPP index b2ff858..f49241a 100644 --- a/SNAKE.CPP +++ b/SNAKE.CPP @@ -3,7 +3,7 @@ SNAKE XENZIA ------------ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - This is a basic snake game which is commonly + This is a basic snake game which is commonly found in old nokia phones. This game has a snake the player has to move with the arrow keys. There will be food particles generated @@ -37,12 +37,12 @@ #include #include -#define ARR_X 200 -#define ARR_Y 200 //dimensions of pos_key matrix +#define ARR_X 100 +#define ARR_Y 100 //dimensions of pos_key matrix char pos_key[ARR_X][ARR_Y]; //matrix corresponding to every position on the screen -void keystroke(); //function which receives and maps keystrokes and decides the corresponding movement +void keystroke(int); //function which receives and maps keystrokes and decides the corresponding movement void adjustxy(int&, int&); //helps in swapping screen sides when snake reaches one edge of screen void movehead(int, int); //prints the head of the snake void movetail(char [ARR_X][ARR_Y]); //replaces the last element of the snake with " " @@ -56,23 +56,25 @@ void frame(); //prints the borders of the screen int pause(); //manages pause option int select(int , int , int [], int); //helps in option selection in the game menus with arrow keys void highscore(int); //to print and modify highscores +void continuegame(int, char &); //to continue a previously incomplete game -int MAX_X; //maximum horizontal coordinate of screen -int MIN_X; //minimum horizontal coordinate of screen -int MAX_Y; //maximum vertical coordinate of screen -int MIN_Y; //minimum vertical coordinate of screen -int max_xs; //MAX_X for small screen -int max_xm; //MAX_X for medium screen -int max_xl; //MAX_X for large screen -int max_ys; //MAX_X for small screen -int max_ym; //MAX_X for medium screen -int max_yl; //MAX_X for large screen +int MAX_X; //maximum horizontal coordinate of screen +int MIN_X; //minimum horizontal coordinate of screen +int MAX_Y; //maximum vertical coordinate of screen +int MIN_Y; //minimum vertical coordinate of screen +int max_xs; //MAX_X for small screen +int max_xm; //MAX_X for medium screen +int max_xl; //MAX_X for large screen +int max_ys; //MAX_X for small screen +int max_ym; //MAX_X for medium screen +int max_yl; //MAX_X for large screen int x, y; //head coordinates int x1, y1; //tail coordinates int x2, y2; //food coordinates -int snaklen; //records length of the snake(to show score at end) +int snaklen; //records length of the snake(to show score at end) int frame_width = 150; //sets default game speed int counter = 0; //records the no. of times the game is played +int savedgamestatus; //records if a saved game is present or not void main() { @@ -81,12 +83,13 @@ void main() { randomize(); //to seed the random() function in addfood() screen(); + continuegame(2, 'k'); } ///////WELCOME SCREEN////////// int choice; char choice2; flag3: - snaklen = 1; + snaklen = 1; for(int l = 0; l