1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream> // including required headers
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <fstream>
#include <iomanip>
using namespace std;
//several functions for certain operations
void menu();
void game();
void controls(char s[][22],int temp[][22],int eating);
void credits();
void table(char s[][22],int score);
bool checkFood(char s[][22],int fx,int fy);
void spawnFood(char s[][22],int & fx,int & fy);
bool gameover(char s[][22],int temp[][22]);
void endgame(int score,int highscore);
void help();
void options();
void difficulty();
void pause();
int lenght; //several variables
char contr='n';
int chek[32][22];
double diff=0.5; //difficulty variable, how fast snake moves (default-normal(0.5 sec)
int main()
{
srand(time(0)); //seeding random numb gen
int i;
char sl; //selection variable
for(i=0;i<9999;i++)
{
if(i>0)
{
cout<<""<<endl;
cout<<"Do you want to play again ? [y/n] ";
cin>>sl;
if(sl=='y')
{
system("cls"); //clearing screen
goto start;
}
else goto quit;
}
start: //starting program
{
menu(); //showing menu screen
}
cin>>sl; //selections for playing,options and exit
if(sl=='1')
{
system("cls"); //clears screen
game(); //calling game function, starting game
i=1;
}
else if(sl=='2')
{
options();
system("cls");
goto start;
i=1;
}
else if(sl=='3') goto quit;
else
{
system("cls");
i=1;
goto start;
}
}
quit: //quits program
{
system("exit");
}
return 0;
}
void table(char s[][22],int score) //function for showing table (screen in which snake moves
{
int x=32; //i am using coordinates for displaying the table
int y=22;
int i,j;
for(i=0;i<y;i++)
{
for(j=0;j<x;j++)
{
if(j<(x-1)) cout<<s[j][i];
if(j==(x-1)) cout<<s[j][i]<<endl;
}
}
cout<<""<<endl;
cout<<"Your score: "<<score;
}
void game()
{
char s[32][22]; //a 2-dimensional array for storing all particles in the screen;
int temp[32][22]; //an array for numbering snake parts and storing them
int i,j,fx,fy,x,y,score=0,highscore=score; //several variables
double k;
int eating; //if snake eats food, this becomes 1, and snake gains lenght
//preparations for game: setting table borders
for(i=0;i<22;i++)
{
for(j=0;j<32;j++)
{
if(i==0 || i==21) s[j][i]='-';
if(i>0 && i<21) s[j][i]=' ';
if(j==0 || j==31) s[j][i]='|';
}
}
spawnFood(s,fx,fy); //calling food spawn function
for(y=1;y<21;y++)
{
for(x=1;x<31;x++)
{
temp[x][y]=0; //setting all the temp values to 0,
} //to see where snake parts are, and where nothing (0);
}
s[17][10]='o'; //finally, sets primary snake position. o is head.
s[16][10]='*';
s[15][10]='*';
temp[15][10]=3; //numbering snake parts
temp[16][10]=2;
temp[17][10]=1;
system("cls");
table(s,score); //calling table function to show a table
lenght=3; //snake primary lenght
while(true) //game starts
{
eating=0; //setting value to 0, because snake hasnt eaten anything
Sleep(diff*1000); //delaying some time, how fast snake moves depends on difficulty
if(kbhit()) //checking if some key is pressed
{
contr=getch(); //if pressed, getting info about that key. this is needed for snake controls
if(contr=='0') pause(); //if 0, pauses game
goto movemake; //starts making snake moves
}
else
{
goto movemake; //if no key is pressed, snake just moves
}
movemake:
{
for(x=1;x<31;x++) //a loop for checking where are snake parts
{
for(y=1;y<21;y++)
{
if(s[x][y]=='*')
{
chek[x][y]=1; //this is needed for checking if snake hits its tail or borders
}
else chek[x][y]=0;
}
}
if(checkFood(s,fx,fy)==true) //if snake eats something, some info needs to be sent for snake to gain lenght
{
eating=1;
score=score+1;
controls(s,temp,eating); //calling snake control and movement function
spawnFood(s,fx,fy);
}
else
{
controls(s,temp,eating); //snake only moves
}
if(gameover(s,temp)==true) goto gameEnd; //if snake has hit something, game terminates
else
{
goto LoopEnd;
}
}
LoopEnd: //when loop ends, screen clears, updated table and score shows
{
system("cls");
table(s,score);
if(score>highscore) highscore=score;
}
}
gameEnd:
{
endgame(score,highscore); //calls endgame screen
return; //returns to int main
}
}
|