-
Notifications
You must be signed in to change notification settings - Fork 0
/
SNAKEGAME.CPP
225 lines (220 loc) · 6.4 KB
/
SNAKEGAME.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
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include "SNAKEGAME.hpp"
// array arr(2-dimensional) is used to track the position of the snake coordinates
// LEN is initial length of the snake
// x,y is used for present head position
// xx,yy is used to represent the position of food
// w,a,s,d(up,left,down,right) respectively are used to play the snake game
// time is used to represent the delay i.e., how fast it should move after some time
// ch1,ch2 are used to track the present and previous charactars used by the player to play game
// graphics is used to remove cursor from the screen
int game()
{
// to clear the screen
clrscr();
int gd = DETECT,gm;
//detectgraph(&gd,&gm);
//initgraph(&gd,&gm,"C:\TC\BGI");
// initialization of position of food and snake , length of snake initially
int i=0, j=0, x=8, y=5, arr[MAAX][2], xx=50, yy=15, flag=0, len=LEN, time=0;
char ch1 = 'd', ch2 = 'd';
// used to display boundary on left and right of a screen
for(i=1;i<=MAXY;i++)
{
gotoxy(1,i);
cout<<"@";
gotoxy(MAXX,i);
cout<<"@";
}
// used to display boundary on top and bottom of a screen
for(i=1;i<=MAXX;i++)
{
gotoxy(i,1);
cout<<"@";
gotoxy(i,MAXY);
cout<<"@";
}
// initialization of position of snake in array
for (i=0; i<len; i++)
{
arr[i][0] = x++;
arr[i][1] = y;
}
while(1)
{
// used to check the boundary whether snake hit it or not
if(x==1 || x==MAXX || y==1 || y==MAXY)
break;
// check whether snake is going to hit itself
for(i=0;i<len-1;i++)
if(arr[i-1][0]==x && arr[i-1][1]==y)
{
j = 1;
break;
}
if(j==1)
break;
// keeping track of previous move
ch2 = ch1;
// removing the tail of snake
gotoxy(arr[0][0],arr[0][1]);
cout<<" ";
// shifting the position of snake
for(i=0; i<len-1; i++)
{
arr[i][0] = arr[i+1][0];
arr[i][1] = arr[i+1][1];
}
arr[i][0] = x;
arr[i][1] = y;
// displaying the head of snake
gotoxy(arr[len-1][0],arr[len-1][1]);
cout<<"*";
// checking the coordinates of food
if(xx==arr[len-1][0] && yy==arr[len-1][1])
flag = 1;
// if it is on snake change the coordinates of food
if(flag)
{
// using random number to find coordinates
xx = rand()%MAXX+1;
yy = rand()%MAXY+1;
for(i=0;i<len;i++)
{
if(xx==arr[i][0] && yy==arr[i][1])
{
// if again present on the snake reset to check again and change the coordinates
xx++;
yy++;
i=0;
}
else if(xx==1 || xx==MAXX || yy==1 || yy==MAXY)
{
// if present on boundaries change the coordinates
xx = rand()%MAXX+1;
yy = rand()%MAXY+1;
}
else
break;
}
// increment the length of snake
len++;
flag=0;
}
// display food
gotoxy(xx,yy);
cout<<"$";
arr[len][0] = xx;
arr[len][1] = yy;
// according to the length of snake , speed of snake is decided
delay(time);
if(len<10)
time = 200;
else if(len<15)
time = 100;
else if(len<20)
time = 50;
else
time = 20;
// accept input from player i.e.,keyboard was hit
if(kbhit())
ch1 = (char)getch();
// according to the charactar decide the move
switch(ch1)
{
// up
case 'w' : // check the previous movement of snake and there should not be any backward movement
if(ch2=='s')
{
ch1 = 's';
y++;
break;
}
y--;
break;
// left
case 'a' : // check the previous movement of snake and there should not be any backward movement
if(ch2=='d')
{
ch1 = 'd';
x++;
break;
}
x--;
break;
// down
case 's' : // check the previous movement of snake and there should not be any backward movement
if(ch2=='w')
{
ch1 = 'w';
y--;
break;
}
y++;
break;
// right
case 'd' : // check the previous movement of snake and there should not be any backward movement
if(ch2=='a')
{
ch1 = 'a';
x--;
break;
}
x++;
break;
default: // if valid charactar is not hit, snake should not change its motion
ch1 = ch2;
// according to the previous charactar it should continue its motion
switch(ch2)
{
case 'w' : y--;
break;
case 'a' : x--;
break;
case 's' : y++;
break;
case 'd' : x++;
break;
default : break;
}
}
}
// clear screen
clrscr();
cout<<"Your SCORE is: "<<len-1;
// wait for user to press any key and see his score
getch();
return 0;
}
int main()
{
char choice;
while(1)
{
// clear screen
clrscr();
//delay(10);
// display menu to player
cout<<"choose one option:\n\n";
cout<<"1.Start game\n";
cout<<"2.Quit\n\n";
cout<<"Enter choice:";
cin>>choice;
switch(choice)
{
case '1' : game();
break;
case '2' : return 0;
default : gotoxy(8,2);
cout<<"wrong choice\n\n";
break;
}
}
// wait for player
getch();
return 0;
}