Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc9e7f0

Browse files
Source FIle
1 parent 9ad0ae0 commit dc9e7f0

File tree

1 file changed

+304
-0
lines changed

1 file changed

+304
-0
lines changed

‎snakeGame.cpp‎

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
// header files
2+
#include<iostream>
3+
#include<time.h>
4+
#include"headerFile/getchInLinux.h" // for getch() in linux
5+
#include"headerFile/kbhit3.h" // for kbhit() in Linux
6+
#include<cstdlib> // for exit()
7+
8+
using namespace std;
9+
10+
// class
11+
class snake
12+
{
13+
// by default private
14+
15+
int borderX,borderY;
16+
int foodX,foodY;
17+
int x,y,score;
18+
int totalSegment;
19+
int snakeTail[2][100]; //
20+
string name;
21+
22+
public:
23+
snake(); // constructor
24+
void showBorder();
25+
void foodGenerate();
26+
int readEnteredKey();
27+
int changeDirectcion(int);
28+
int eatingCheck(); // if food eaten by snake return 1 , else return 0
29+
void increaseScore();
30+
void showScore();
31+
int checkBoundryCollision();
32+
void addSegment();
33+
int checkOverlap();
34+
void delay1();
35+
};
36+
37+
snake:: snake()
38+
{
39+
system("clear");
40+
cout<<"Enter Your Good Name: ";
41+
cin>>name;
42+
system("clear");
43+
cout<<"\n SNAKE .................GAME ! \n\n";
44+
45+
cout<<" RULE OF THIS GAME \n";
46+
cout<<"\n1.You can move snake in up,Down,Left or Right only by arrow key ";
47+
cout<<"\nMove Up : by Up arrow key ";
48+
cout<<"\nMove Down : by Down arrow key";
49+
cout<<"\nMove Left : by Left arrow key";
50+
cout<<"\nMove Right: by Right arrow key";
51+
52+
cout<<"\n\n2.You can exit the game at any time by pressing 'E' or 'e' ";
53+
cout<<"\n3.For each eaten food You will be awared a 5 point ";
54+
cout<<"\n\n Happy gaming , Good Luck\n";
55+
56+
cout<<"\nEnter any key to start..... : ";
57+
58+
score=0; // initial zero
59+
borderX=90;
60+
borderY=30;
61+
62+
x=borderX/2;
63+
y=borderY/2;
64+
65+
totalSegment=0; // snake segment
66+
67+
}
68+
void snake:: showScore()
69+
{
70+
cout<<"your score: "<<score<<endl<<endl;
71+
}
72+
void snake:: showBorder()
73+
{
74+
system("clear");
75+
cout<<" SNAKE GAME ";
76+
cout<<"\n\nWelcome "<<name<<" , ";
77+
showScore();
78+
79+
for(int i=1;i<=borderY;i++)
80+
{ for(int j=1;j<=borderX;j++)
81+
if((i==1 || i==borderY )|| (j==1 || j== borderX))
82+
cout<<".";
83+
else if(j==foodX && i==foodY)
84+
cout<<"F";
85+
else if(checkBoundryCollision())
86+
{}
87+
else if(j==x && i==y)
88+
cout<<"@";
89+
else if(eatingCheck())
90+
{
91+
foodGenerate(); // generate new food for snake
92+
increaseScore(); // increase score
93+
delay1();
94+
delay1();
95+
delay1();
96+
delay1();
97+
delay1();
98+
addSegment(); // to add segmments
99+
}
100+
else // printing snake segments
101+
{ int temp=0;
102+
for(int k=1;k<=totalSegment;k++)
103+
if(j==snakeTail[0][k] && i==snakeTail[1][k])
104+
{ cout<<"o";
105+
temp=1;
106+
}
107+
if(!temp)
108+
cout<<" ";
109+
}
110+
cout<<endl;
111+
}
112+
}
113+
void snake:: foodGenerate()
114+
{
115+
delay1();
116+
delay1();
117+
delay1();
118+
delay1();
119+
120+
srand(time(NULL));
121+
while(1)
122+
{ foodX=rand()%(borderX); // randomly
123+
foodY=rand()%(borderY);
124+
if(foodX>1 && foodY>1)
125+
break;
126+
}
127+
totalSegment++; // segments increased by one
128+
}
129+
// Reads the user input character and return ascii value of that
130+
int snake::readEnteredKey()
131+
{
132+
char c;
133+
c = getchInLinux();
134+
if(c==27 || c==91) // this is just for scan code
135+
{ c = getchInLinux();
136+
if(c==27 || c==91) // also for scan code
137+
c=getchInLinux();
138+
}
139+
return c;
140+
}
141+
int snake::changeDirectcion(int key)
142+
{
143+
switch(key)
144+
{
145+
case 69: // ascii of E
146+
147+
case 101: // ascii of e
148+
cout<<"\a\a\a\a\a\a\n Thanks for Playing ! \n\a";
149+
cout<<"\nHit 'Enter' to exit the game \n";
150+
key=readEnteredKey();
151+
exit(0);
152+
153+
154+
case 65: // arrow up
155+
y--;
156+
return 1;
157+
158+
case 66: // arrow down
159+
y++;
160+
return 1;
161+
162+
case 67: // arrow right
163+
x++;
164+
return 1;
165+
case 68: // arrow left
166+
x--;
167+
return 1;
168+
default:
169+
170+
// cout<<"\n\n \a\a❌ Not Allowed \a\a\a\a");
171+
return 0;
172+
}
173+
}
174+
175+
int snake::checkBoundryCollision()
176+
{
177+
if(x<=1 || x>=borderX || y<=1 || y>=borderY)
178+
{ system("clear");
179+
cout<<"\nGame Over ! ";
180+
showScore();
181+
delay1();
182+
delay1();
183+
exit(0);
184+
}
185+
return 0;
186+
}
187+
int snake::eatingCheck()
188+
{
189+
if(foodX==x && foodY==y)
190+
return 1;
191+
return 0;
192+
193+
}
194+
195+
void snake::increaseScore()
196+
{
197+
score+=5;
198+
}
199+
void snake:: addSegment()
200+
{
201+
// idea is that , each snake segments will follow its front snake.
202+
// i.e
203+
// first segment will follow snake's HEAD
204+
// 2nd segment " " 1st segment
205+
// 3rd segment " " 2nd segment
206+
207+
int prev1X=snakeTail[0][0];
208+
int prev1Y=snakeTail[1][0];
209+
210+
int current1X;
211+
int current1Y;
212+
213+
snakeTail[0][0]=x;
214+
snakeTail[1][0]=y;
215+
216+
for(int i=1;i<totalSegment;i++)
217+
{
218+
current1X=snakeTail[0][i];
219+
current1Y=snakeTail[1][i];
220+
221+
snakeTail[0][i]=prev1X;
222+
snakeTail[1][i]=prev1Y;
223+
224+
prev1X=current1X;
225+
prev1Y=current1Y;
226+
}
227+
}
228+
229+
int snake:: checkOverlap()
230+
{
231+
int i;
232+
for(i=0;i<totalSegment;i++)
233+
if(x==snakeTail[0][i] && y==snakeTail[1][i])
234+
break;
235+
236+
if(i<totalSegment)
237+
{
238+
system("clear");
239+
cout<<"\nGame Over ! ";
240+
showScore();
241+
delay1();
242+
delay1();
243+
exit(0);
244+
}
245+
return 0;
246+
}
247+
248+
void snake::delay1()
249+
{
250+
for(int i=1;i<=10000;i++)
251+
for(int j=1;j<=1000;j++);
252+
}
253+
254+
int main() // main function
255+
{
256+
int key,d;
257+
snake s1;
258+
int x=s1.readEnteredKey();
259+
x=s1.readEnteredKey();
260+
261+
system("clear");
262+
263+
s1.foodGenerate();
264+
s1.showBorder();
265+
key=s1.readEnteredKey();
266+
s1.delay1();
267+
s1.delay1();
268+
269+
while(1)
270+
{
271+
while(!kbhitInLinux())
272+
{
273+
s1.showBorder();
274+
d=s1.changeDirectcion(key);
275+
276+
s1.checkOverlap();
277+
278+
s1.addSegment(); // to add snake segmments
279+
280+
s1.delay1();
281+
s1.delay1();
282+
s1.delay1();
283+
s1.delay1();
284+
s1.delay1();
285+
s1.delay1();
286+
287+
system("clear");
288+
}
289+
s1.showBorder();
290+
key=s1.readEnteredKey();
291+
s1.delay1();
292+
s1.delay1();
293+
s1.delay1();
294+
s1.delay1();
295+
s1.delay1();
296+
297+
d=s1.changeDirectcion(key);
298+
299+
s1.checkOverlap();
300+
301+
s1.addSegment(); // to add segmments
302+
}
303+
return 0; // return from main
304+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /