-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
245 lines (236 loc) · 5.64 KB
/
snake.cpp
File metadata and controls
245 lines (236 loc) · 5.64 KB
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
#include<bits/stdc++.h>
#include<conio.h>
#include <windows.h>
//#include <stdio.h>
//#include <iostream>
//#include <deque>
//#include<stdlib.h>
using namespace std;
COORD coord={0,0};
int X=10;
int Y=10;
int a;
int b;
struct point
{
int x;
int y;
}p,temp;
deque<point> q;
void gotoxy(int x,int y) //gotoxy Function definition.
{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void show_bounds() //Output the bounds of game
{
for(int i=0;i<X+2;i++)
{
gotoxy(i,Y+1);
cout<<"~";
}
for(int j=0;j<Y+1;j++)
{
gotoxy(X+1,j);
cout<<"|";
}
}
int random() //Generate random values for program
{
return(rand()%X+1);
}
bool bound(int x,int y) //Checking out of bounds condition
{
if( x<0 || y<0 || x>X || y>Y)
{
return true;
}
else
return false;
}
void show_snake() //Show the snake
{
deque<point>::iterator it;
for (it=q.begin();it!=q.end();it++ )
{
temp=*it;
gotoxy(temp.x,temp.y);
if(it==q.begin())
{
cout<<"="; //Head of snake
}
else if((it+1)==q.end()) //output cursor
{
cout<<"+";
gotoxy(X+2,Y+2);
cout<<" ";
}
else //rest body of snake
{
cout<<"+";
}
}
}
/*void showstatus() //show dequeue status used for snake
{
cout<<"\n The Queue status is : \n";
deque<point>::iterator it;
for (it=q.begin();it!=q.end();it++ )
{
temp=*it;
cout<<temp.x<<" "<<temp.y<<endl;
}
}*/
bool selfkill(int x,int y) //check for collisions
{
deque<point>::iterator it;
for (it=q.begin();it!=q.end();it++ )
{
temp=*it;
if (temp.x==x && temp.y==y)
{
return true;
}
}
return false;
}
bool dead(point p) //Game over check
{
if(bound(p.x,p.y))
{
system("cls");
cout<<"Game over ..Out of bounds ";
return true;
}
else if(selfkill(p.x,p.y))
{
system("cls");
cout<<"Game over ..";
return true;
}
else
return false;
}
void food() //place food in bounds
{
a=random();
b=random();
if(selfkill(a,b))
{
food();
}
}
void movement(point p) //Snake movement
{
if(p.x==a && p.y==b) //if at food
{
//cout<<"\n pushing";
//cout<<"\n P.x = "<<p.x<<" P.y = "<<p.y;
q.push_front(p);
//showstatus();
food();
/*a=random();
b=random();
cout<<"\n Food is at (x,y) = "<<a<<" "<<b;
*/}
else //if not at food
{
q.push_front(p);
q.pop_back();
}
}
void steps()
{
DWORD start_time, check_time;
int dflag=0;
char n;
food();
/*a=random();
b=random();
cout<<"\n Food is at (x,y) = "<<a<<" "<<b;
*/
while(n!='e' && dflag!=1 ) //press e to quit in between
{
system("cls");
show_bounds();
gotoxy(a,b);
cout<<"%";
show_snake();
start_time=GetTickCount();
check_time=start_time+700;
temp=q.front();
//cout<<"\n Front (x,y) = "<<temp.x<<" "<<temp.y<<endl;
while(check_time>GetTickCount()) //input within time period
{
if (kbhit())
{
n=getch();
break;
}
}
//cout<<"pressed "<<n<<endl;
switch (n) //movement according key pressed
{
case 'w':
p.x=temp.x;
p.y=temp.y-1;
if(dead(p))
{
dflag=1;
}
else
{//cout<<"\nup";
movement(p);
}
break;
case 'a':
p.x=temp.x-1;
p.y=temp.y;
if(dead(p))
{
dflag=1;
}
else
{//cout<<"\nleft";
movement(p);
}
break;
case 's':
p.x=temp.x;
p.y=temp.y+1;
if(dead(p))
{
dflag=1;
}
else
{//cout<<"\ndown";
movement(p);
}
break;
case 'd':
p.x=temp.x+1;
p.y=temp.y;
if(dead(p))
{
dflag=1;
}
else
{//cout<<"\nright";
movement(p);
}
break;
}
}
}
int main()
{
p.x=0; //initial position
p.y=0;
cout<<"Welcome to snake game..\nRules :-\nUse w,a,s,d to move snake..\n= represent head of snake\n+ represent rest part of snake\n% represent food of snake\nPress enter to continue..";
while(!kbhit()){}
q.push_front(p);
steps();
getch();
return 0;
}