-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSNAKE AND BOX.CPP
607 lines (598 loc) · 16.1 KB
/
SNAKE AND BOX.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX 50
#define UP_ARROW 72
#define DOWN_ARROW 80
#define LEFT_ARROW 75
#define RIGHT_ARROW 77
#define WinMinX 40
#define WinMaxX 600
#define WinMinY 40
#define WinMaxY 440
enum Direction {Forward,Backward,Upward,Downward};
//********************************************************************
//********************************************************************
struct Coord
{
int x , y;
};
class Snake;
//********************************************************************
//********************************************************************
class Point
{
int x , y , color ;
public:
Point ( )
{
set ();
}
void set();
void draw( );
int getx() { return x; }
int gety() { return y; }
friend int point_vanished ( Point &p , Snake &s );
};
//********************************************************************
//********************************************************************
class Snake
{
Coord *_Snake;
int _CurSize, _color,_MaxSize, _Points;
char _player;
Direction _Direction;
public:
Snake ( int size = 20, int color = RED , char player = 'M' )
{
_Snake = new Coord [ size ];
_CurSize = 3;
if ( player == 'C' )
{
_Snake [0].x = WinMaxX - 10;
_Direction = Backward;
}
else
{
_Snake [0].x = WinMinX + 10;
_Direction = Forward;
}
_Snake [0].y = WinMinY + 10;
_color = color;
_MaxSize = size;
_player = player;
_Points = 0;
}
void set( int size = 20, int color = RED , char player = 'M' )
{
delete _Snake;
_Snake = new Coord [ size ];
_CurSize = 3;
if ( player == 'C' )
{
_Direction = Backward;
_Snake [0].x = WinMaxX - 10;
}
else
{
_Snake [0].x = WinMinX + 10;
_Direction = Forward;
}
_Snake [0].x = WinMinX + 10;
_color = color;
_MaxSize = size;
_player = player;
_Points = 0;
}
void change_direction ( Direction d);
void increment ();
void inc_disp ();
void shift_all ();
void display ( int color = BLACK );
void com_play ( Point p1 );
friend int point_vanished ( Point &p , Snake &s );
};
//********************************************************************
//********************************************************************
void Sound ( int s );
void Message_Display ( char msg[30] , char color );
void show_Header();
void signature();
int menu ();
void whats_new();
void drawMenu ( int selected , int defCol , int selCol );
void show_About();
void show_HowTOPlay();
void Play();
//********************************************************************
//********************************************************************
int main()
{
int g = DETECT , d;
initgraph ( &g , &d , "d:\tc\bgi" );
int selected_option;
Start:
selected_option = menu();
switch ( selected_option )
{
case 1:Play();
goto Start;
case 2:show_HowTOPlay();
goto Start;
case 3:whats_new();
goto Start;
case 4:show_About();
goto Start;
case 5:return 1;
}
return 1;
}
//********************************************************************
//********************************************************************
void Snake :: increment ( )
{
shift_all();
if ( _Direction == Forward )
{
if ( _Snake[0].x >= WinMaxX )
{
_Snake[0].x = WinMinX ;
}
else
_Snake[0].x = _Snake[0].x + 10;
}
else if ( _Direction == Backward )
{
if ( _Snake[0].x <= WinMinX )
{
_Snake[0].x = WinMaxX ;
}
else
_Snake[0].x = _Snake[0].x - 10;
}
else if ( _Direction == Upward )
{
if ( _Snake[0].y <= WinMinY )
{
_Snake[0].y = WinMaxY ;
}
else
_Snake[0].y = _Snake[0].y - 10;
}
else if ( _Direction == Downward )
{
if ( _Snake[0].y >= WinMaxY )
{
_Snake[0].y = WinMinY ;
}
else
_Snake[0].y = _Snake[0].y + 10;
}
}
//********************************************************************
//********************************************************************
void Snake :: shift_all ()
{
int i;
for ( i = _CurSize -1 ; i > 0; i-- )
{
_Snake[i].x = _Snake[i-1].x;
_Snake[i].y = _Snake[i-1].y;
}
}
//********************************************************************
//********************************************************************
void Snake :: inc_disp ()
{
display ( BLACK );
increment();
display ( _color );
}
//********************************************************************
//********************************************************************
void Snake :: display ( int color)
{
setfillstyle ( 1, color );
if ( color == 0 )
{
setcolor ( 0 );
bar ( _Snake[_CurSize - 1].x - 5 , _Snake[_CurSize - 1].y - 5 , _Snake[_CurSize - 1].x + 5 , _Snake[_CurSize - 1].y + 5 );
rectangle ( _Snake[_CurSize - 1].x - 5 , _Snake[_CurSize - 1].y - 5 ,_Snake[_CurSize - 1].x + 5 , _Snake[_CurSize - 1].y + 5 );
}
else
{
setcolor ( WHITE );
for ( int i = 0; i< _CurSize; i++ )
{
bar ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y + 5 );
rectangle ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y + 5 );
}
setfillstyle ( 1 , 0 );
fillellipse ( _Snake[0].x , _Snake[0].y , 2 , 2);
char msg[50];
setcolor ( WHITE );
if ( _player == 'C' )
{
bar ( 250 , 12 , 630 , WinMinY - 10 );
sprintf ( msg , "Com Snake at :- ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y , _Points );
outtextxy ( 250 , 12 , msg );
}
else
{
bar ( 250 , 1 , 630 , WinMinY - 10 );
sprintf ( msg , "Ur Snake at :- ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y , _Points );
outtextxy ( 250 , 1 , msg );
}
}
}
//********************************************************************
//********************************************************************
void Snake :: change_direction ( Direction d)
{
if ( ( _Direction == Forward ) && ( d == Backward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Backward ) && ( d == Forward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Upward ) && ( d == Downward ) )
{
Sound ( -1 );
}
else if ( ( _Direction == Downward ) && ( d == Upward ) )
{
Sound ( -1 );
}
else
{
_Direction = d;
Sound ( 1 );
}
}
//********************************************************************
//********************************************************************
void Point :: draw ( )
{
char msg[30];
setfillstyle ( 1 , color );
setcolor ( YELLOW );
bar ( x - 4 , y - 4 , x + 4 , y + 4 );
rectangle ( x - 4 , y - 4 , x + 4 , y + 4 );
setfillstyle ( 1 , 0 );
fillellipse ( x , y , 2 , 2 );
bar ( 1 , 1 , 300 , WinMinY - 10 );
sprintf ( msg , "Point at :- ( %d , %d )", x , y );
outtextxy ( 40 , 1 , msg );
}
//********************************************************************
//********************************************************************
void Point :: set ( )
{
color = random ( 15 ) + 1;
x = random ( ( ( WinMaxX - WinMinX ) / 10 ) ) ;
y = random ( ( ( WinMaxY - WinMinY ) / 10 ) ) ;
x = ( x * 10 ) + WinMinX;
y = ( y * 10 ) + WinMinY;
draw ( );
}
//********************************************************************
//********************************************************************
int point_vanished ( Point &p , Snake &s )
{
if ( ( s._Snake[0].x == p.x ) && ( s._Snake[0].y == p.y ) )
{
s._CurSize++;
if ( s._CurSize == s._MaxSize )
{
return 2;
}
s.increment ();
s.display ( RED );
Sound ( 2 );
delay ( 100 );
s._Points = s._Points + 20 ;
p.set();
return 1;
}
else
{
return -1;
}
}
//********************************************************************
//********************************************************************
void whats_new()
{
cleardevice();
setbkcolor ( BLACK );
show_Header();
setcolor ( WHITE );
settextstyle ( 0 , 0 , 0 );
outtextxy ( 20 , 100 , "IDEA:" );
outtextxy ( 20 , 150 , "WHATS NEW:" );
outtextxy ( 20 , 220 , "UPDATE:" );
setcolor ( LIGHTGREEN );
outtextxy ( 120 , 120 , "The Idea of Developing it is taken from NOKIA MOBILE" );
outtextxy ( 120 , 170 , "Everything is new as it is developed using C++" );
outtextxy ( 120 , 240 , "New changes would be brought soon new version in progress");
outtextxy ( 120 , 250 , "Difficulty level would be higher" );
setcolor( CYAN );
outtextxy ( 120 , 260 , "AASAN KAAM NAHI HAI YEH" );
setcolor( LIGHTGREEN );
signature();
getch();
}
//********************************************************************
//********************************************************************
void Sound ( int s )
{
if ( s == -1 )
{
sound ( 150 );
delay ( 30 );
sound ( 250 );
delay ( 30 );
nosound ();
}
else if ( s == 1 )
{
sound ( 450 );
delay ( 20 );
nosound ();
}
else if ( s == 2 )
{
sound ( 650 );
delay ( 20 );
nosound ();
}
}
//********************************************************************
//********************************************************************
void Snake :: com_play ( Point p1 )
{
if ( p1.getx() < _Snake[0].x )
{
if ( _Direction == Forward )
_Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
else
_Direction = Backward;
}
else if ( p1.getx() > _Snake[0].x )
{
if ( _Direction == Backward )
_Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
else
_Direction = Forward;
}
else
{
if ( p1.gety() < _Snake[0].y )
{
_Direction = Upward;
}
else if ( p1.gety() > _Snake[0].y )
{
_Direction = Downward;
}
}
}
//********************************************************************
//********************************************************************
void Message_Display ( char msg[30] , char color )
{
settextstyle ( 1 , 0 , 5 );
setcolor ( 8 );
outtextxy ( 195 , 205 , msg);
settextstyle ( 1 , 0 , 5 );
setcolor ( color );
outtextxy ( 200 , 200 , msg);
delay ( 1000 );
}
//********************************************************************
//********************************************************************
int menu ()
{
int ch;
int selected = 1;
int TotalOptions = 5;
cleardevice();
setbkcolor ( BLUE );
show_Header();
signature();
drawMenu ( selected , RED , GREEN );
do
{
ch = getch();
if ( ch == DOWN_ARROW )
{
selected = selected >= TotalOptions ? 1 : selected + 1;
drawMenu ( selected , RED , GREEN );
}
else if ( ch == UP_ARROW )
{
selected = selected < 2 ? TotalOptions : selected - 1;
drawMenu ( selected , RED , GREEN );
}
}while ( ch != ' ' );
return selected;
}
//********************************************************************
//********************************************************************
void drawMenu ( int selected , int defCol , int selCol )
{
int x = 250;
int y = 100;
int width = 150;
int height = 30;
int i;
int TotalOptions = 5;
char menu_option[5][14]= {
" PLAY ",
" HOW TO PLAY ",
" WHAT'S NEW ",
" ABOUT ME ",
" EXIT "
};
setcolor ( WHITE );
for ( i = 1; i <= TotalOptions; i++ )
{
if ( i == selected )
setfillstyle ( 1 , selCol );
else
setfillstyle ( 1 , defCol );
bar ( x , y , x + width , y + height );
rectangle ( x , y , x + width , y + height );
outtextxy ( x-8 , y-5 , menu_option[i - 1] );
y = y + height + 30;
}
}
//********************************************************************
//********************************************************************
void show_About()
{
cleardevice();
setbkcolor ( BLACK );
show_Header();
setcolor ( WHITE );
settextstyle ( 0 , 0 , 0 );
outtextxy ( 20 , 100 , "DEVELOPER:" );
outtextxy ( 20 , 150 , "CHECKER:" );
outtextxy ( 20 , 220 , "ABOUT GAME:" );
setcolor ( LIGHTGREEN );
outtextxy ( 120 , 120 , "Fully developed by Gaurav Narula." );
outtextxy ( 120 , 170 , "Game is Error free and it's done by Gaurav Narula." );
outtextxy ( 120 , 240 , "Game is Interesting n anybody who doesn't find it so");
outtextxy ( 120 , 250 , "should develop it by itself" );
setcolor( CYAN );
outtextxy ( 120 , 260 , "AASAN KAAM NAHI HAI YEH" );
setcolor( LIGHTGREEN );
signature();
getch();
}
//********************************************************************
//********************************************************************
void show_HowTOPlay()
{
cleardevice();
setbkcolor ( BLACK );
show_Header();
settextstyle ( 0 , 0 , 0 );
setcolor ( WHITE );
outtextxy ( 20 , 100 , "Objective:" );
outtextxy ( 20 , 150 , "Playing:" );
outtextxy ( 20 , 220 , "Tip:" );
setcolor ( LIGHTGREEN );
outtextxy ( 120 , 120 , "To collect 50 boxes before the computer Snake." );
outtextxy ( 120 , 170 , "1. Use arrow keys to control your Snake." );
outtextxy ( 120 , 180 , "2. To collect the box just come near to the BOX." );
outtextxy ( 120 , 190 , "3. Press <ESC> to QUIT any time." );
outtextxy ( 120 , 240 , "1. Use shortcuts to collect the BOX. [ Computer Snake never " );
outtextxy ( 120 , 250 , " uses shortcut]" );
outtextxy ( 120 , 260 , "2. Computer Snake can't Hurt you, so enjoy moving around." );
signature();
getch();
}
//********************************************************************
//********************************************************************
void signature()
{
outtextxy ( 350 , 400 , " GAURAV NARULA " );
}
//********************************************************************
//********************************************************************
void show_Header()
{
setcolor ( RED );
settextstyle ( 1 , 0 , 4 );
outtextxy ( 193 , 27 , " SNAKE WAR - I " );
setcolor ( YELLOW );
outtextxy ( 195 , 25 , " SNAKE WAR - I " );
}
//********************************************************************
//********************************************************************
void Play()
{
Snake s1 ( MAX , GREEN , 'M' );
Snake s2 ( MAX , MAGENTA , 'C' );
char ch , KeyPressed = 0;
cleardevice();
randomize ();
rectangle ( WinMinX - 7, WinMinY - 7, WinMaxX + 7 , WinMaxY + 7 );
Point p1;
setbkcolor ( BLUE );
s1.inc_disp();
s2.inc_disp();
setcolor ( YELLOW );
outtextxy ( 5 , 420 , "> Collect 50 Boxes to WIN.");
setcolor ( CYAN );
outtextxy ( 2 , 440 , "> Use <ESC> to QUIT anytime.");
getch();
KeyPressed = 1;
ch = 'R';
while ( 1 )
{
while ( !kbhit() )
{
s1.inc_disp();
if ( point_vanished ( p1 , s1 ) == 2 )
{
Message_Display ( "YOU WIN " , GREEN );
ch=0x1b;
getch();
break;
}
s2.com_play ( p1 );
s2.inc_disp();
if ( point_vanished ( p1 , s2 ) == 2 )
{
Message_Display ( "YOU LOSE " , GREEN );
ch=0x1b;
getch();
break;
}
delay ( 100 );
if ( KeyPressed == 1 )
KeyPressed = 0;
}
if ( ch == 0x1b )
break;
ch = getch();
if ( KeyPressed == 1 )
{
KeyPressed = 0;
continue;
}
if ( ch == 0x1b )
break;
else if ( ch == 0 )
{
ch = getch ();
if ( ch == UP_ARROW )
{
s1.change_direction ( Upward );
KeyPressed = 1;
}
else if ( ch == DOWN_ARROW )
{
s1.change_direction ( Downward );
KeyPressed = 1;
}
else if ( ch == LEFT_ARROW )
{
s1.change_direction ( Backward );
KeyPressed = 1;
}
else if ( ch == RIGHT_ARROW )
{
s1.change_direction ( Forward );
KeyPressed = 1;
}
}
}
}