-
Notifications
You must be signed in to change notification settings - Fork 1
/
WINDOW.CPP
334 lines (326 loc) · 8.39 KB
/
WINDOW.CPP
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
#include<fstream.h>
#include<conio.h>
void centertext(char *);
void score_store(char *,int);
void singleplayer();
void multiplayer();
void credits();
void dispgameover();
void leaderboard();
int gameOver;
const int width = 50;
const int height = 20;
int x, y, x0, y0, fruitX, fruitY, score1, score2;
int tailX[100], tailY[100];
int nTail;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
void showloading();
void Setup()
{
gameOver = 0;
dir = STOP;
x = width/2;
y = height/2;
fruitX = rand() % width;
fruitY = rand() % height;
score1 = 0;
score2 = 0;
}
void Draw()
{
system("cls"); //clears the screen
for(int i = 0;i < width+2;i++)
cout << "Ü"; //upper boundary
cout << endl;
for(i =0; i <height; i++)
{
for(int j = 0;j < width;j++)
{
if(j == 0)
cout << "Û"; //Boundary Left
if( i== y && j== x)
{ textcolor(GREEN);
cprintf(""); //Face
}
else if(i==fruitY && j==fruitX)
printf("%c",1);
else
{
int print = 0;
for(int k=0;k<nTail;k++)
{
if(tailX[k] == j && tailY[k] == i)
{ cprintf(""); //Snake Body
print = 1;
}
}
if(!print)
cout << " ";
}
if(j== width - 1)
cout << "Û";
}
cout << endl;
}
for(i = 0; i < width+2;i++)
cout << "ß"; //Lower Boundary
cout << endl;
cout << "Score: " << score1 <<endl;
}
void Input()
{
if(kbhit())
{
switch(getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 's':
dir = DOWN;
break;
case 'w':
dir = UP;
break;
case 'x':
gameOver = 1;
break;
}
}
}
void Logic()
{
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for(int i=1;i<nTail;i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch(dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
//if(x > width || x < 0 || y > height || y < 0) if i wanna end the game on hitting the wall
// gameOver = true;
if(x>=width)
x=0;
else if(x<0)
x=width-1;
if(y>=height)
y=0;
else if(y<0)
y=height-1;
for(i=0;i<nTail;i++)
if(tailX[i] == x && tailY[i] == y)
gameOver = 1;
if(x == fruitX && y == fruitY)
{
score1 = score1+10;
fruitX = rand()%width;
fruitY = rand()%height;
nTail++;
}
}
int main()
{ _setcursortype(_NOCURSOR);
showloading();
int ch;
_main:
window(1,1,80,25);
clrscr();
cout<<"\n\n\n\n\n\n";
centertext("MAIN MENU");
cout<<"\n\n\n";
centertext("1. Single Player");
centertext("2. Play Multiplayer");
centertext("3. Leader Board");
centertext("4. Credits");
centertext("5. Exit");
cout<<"\n\n\n";
centertext("Enter Your Choice :");
cout<<"\t\t\t\t ";
cin>>ch;
switch(ch)
{ case 1: singleplayer();
break;
case 2: multiplayer();
break;
case 3: leaderboard();
break;
case 4: credits();
break;
case 5: exit(0);
break;
default: cout<<"Wrong choice!!! Enter again";
getch();
goto _main;
}
goto _main;
}
void showloading()
{
system("cls");
cout<<" ÜÜÜÜÜÜÜÜÜÜÜÜÜ ÜÜÛÛÛÜÜ \n";
cout<<" ÜÛÛÛÛÛÛÛÛßßÛÛÛÛÛÛÛÜ ÜÛÛÛÛÛÛÛÛÛÜ \n";
cout<<" ÜÛÛÛÛÛÛÛÛÛ ÛÛßßßßß ÛÛÛÛß ßÛÛÛÜ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÜÜÛÛÛÜÜÜÜÜ ÛÛÛÛÜ ÜÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛß ßÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛßßßßßßßß ßÛÛÛÛÛßß \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÜ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÜ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÜ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ßÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ßÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ßÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÜÜÜÜÜÜÜÜÜÜÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÜÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛß \n";
cout<<" ßÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛß \n";
cout<<" ßßßßßßßßßßßßßßß \n";
delay(1000);
window(10,21,70,25);
cout<<"Loading";
window(10,22,70,25);
for(int x=0;x<60;x++)
{
cout<<"Û";
delay(60);
}
window(10,23,70,25);
cout<<"completed...";
window(10,24,70,25);
cout<<"Press any Key to continue...";
}
void centertext(char *s)
{ int space = (80 - strlen(s))/2;
for(int i=0;i<space;i++)
{ cout<<" ";
}
for(i=0;i<strlen(s);i++)
{ cout<<s[i];
}
cout<<endl;
}
void singleplayer(){
clrscr();
Setup();
while(!gameOver)
{
Draw();
Input();
Logic();
delay(60);
} dispgameover();
getch();
}
void multiplayer(){
clrscr();
cout<<"Multiplayer Content Goes HEre";
getch();
}
void settings(){
clrscr();
cout<<"\n\n\n";
centertext("SETTINGS");
getch();
}
void credits(){
clrscr();
cout<<"\n\n\n";
centertext("CREDITS");
cout<<"\n\n";
centertext("Shashank Srivastava");
endl;endl;
centertext("Avinash Kumar");
endl;endl;
centertext("Sakshi Jha");
endl;endl;
centertext("Fahad");
cout<<"\n\n\n";
window(25,1,25,80);
centertext("Press Any key to go back!!!");
getch();
}
void dispgameover()
{
clrscr();
cout<<" ÛÛÛÛÛÛÛÛÛ ÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÜÜÜßßßßßßßßß ÜÜÜßßßßßßßÜÜÜ ÛÛÛÛÛÛ ÛÛÛÛÛÛ ÛÛÛßßßßßß \n";
cout<<" ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛßßßÜÜÜßßßÛÛÛ ÛÛÛ \n";
cout<<" ÛÛÛ ÛÛÛÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛ ßßßÛÛÛ ÛÛÛÛÛÛÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛßßßß \n";
cout<<" ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛÛÛ \n";
cout<<" ßßßßßßßßßß ßßß ßßß ßßß ßßß ßßßßßßßßß \n";
cout<<"\n";
cout<<" ÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛÛÛ ÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÜÜÜßßßßßßßÜÜÜ ÛÛÛ ÛÛÛ ÛÛÛßßßßßß ÛÛÛßßßßßßÜÜÜ \n";
cout<<" ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ \n";
cout<<" ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛ ÛÛÛÜÜÜÜÜÜÛÛÛ \n";
cout<<" ÛÛÛ ÛÛÛ ßßßÜÜÜ ÜÜÜßßß ÛÛÛßßßß ÛÛÛÛÛÛÛÛÛÛÛ \n";
cout<<" ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ ÛÛÛ \n";
cout<<" ÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛÛÛÛÛÛÛ ÛÛÛ ÛÛÛ \n";
cout<<" ßßßßßßß ßßß ßßßßßßßßß ßßß ßßß \n";
cout<<" \n";
char name[25]="UNKNOWN";
cout<<"\t\t\t\tYOUR SCORE : "<<score1<<endl;
cout<<"Enter Your Name : ";
scanf("%s",name);
score_store(name,score1);
getch();
}
void score_store(char _name[25],int sc)
{ofstream fout; //Write data on file
fout.open("leader.txt",ios::ate);
fout<<_name<<" "<<sc<<endl;
fout.close();
}
void leaderboard()
{ char data[30];
int score_;
ifstream fin;
fin.open("leader.txt",ios::in);
clrscr();
while(!fin.eof())
{ fin>>data;
fin>>score_;
cout<<"\n\n\n"<<setw(40)<<"NAME : SCORE\n\n";
cout<<setw(40)<<data<<" : "<<score_<<endl;
}
getch();
}