-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSnake Colored.cpp
More file actions
275 lines (250 loc) · 6.07 KB
/
Snake Colored.cpp
File metadata and controls
275 lines (250 loc) · 6.07 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
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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <dos.h>
#include <time.h>
#define MAXSNAKESIZE 100
#define MAXFRAMEX 119
#define MAXFRAMEY 29
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoxy(int x, int y){
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
{
if(size == 0)
{
size = 20; // default cursor size Changing to numbers from 1 to 20, decreases cursor width
}
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(console,&lpCursor);
}
class Point{
private:
int x;
int y;
public:
Point(){
x = y = 10;
}
Point(int x, int y){
this -> x = x;
this -> y = y;
}
void SetPoint(int x, int y){
this -> x = x;
this -> y = y;
}
int GetX(){
return x;
}
int GetY(){
return y;
}
void MoveUp(){
y--;
if( y < 0 )
y = MAXFRAMEY;
}
void MoveDown(){
y++;
if( y > MAXFRAMEY )
y = 0;
}
void MoveLeft(){
x--;
if( x < 0 )
x = MAXFRAMEX;
}
void MoveRight(){
x++;
if( x > MAXFRAMEX )
x = 0;
}
void Draw(char ch='O'){
gotoxy(x,y);
cout<<ch;
}
void Erase(){
gotoxy(x,y);
cout<<" ";
}
void CopyPos(Point * p){
p->x = x;
p->y = y;
}
int IsEqual(Point * p){
if( p->x == x && p->y ==y )
return 1;
return 0;
}
void Debug(){
cout<<"("<<x<<","<<y<<") ";
}
};
class Snake{
private:
Point * cell[MAXSNAKESIZE]; // array of points to represent snake
int size; // current size of snake
char dir; // current direction of snake
Point fruit;
int state; // bool representing state of snake i.e. living, dead
int started;
int blink; // fruit blink
public:
Snake(){
size = 1; // default size
cell[0] = new Point(20,20); // 20,20 is default position
for( int i=1; i<MAXSNAKESIZE; i++){
cell[i] = NULL;
}
fruit.SetPoint(rand()%MAXFRAMEX, rand()%MAXFRAMEY);
state = 0;
started = 0;
}
void AddCell(int x, int y){
cell[size++] = new Point(x,y);
}
void TurnUp(){
if( dir!='s' )
dir = 'w'; // w is control key for turning upward
}
void TurnDown(){
if( dir!='w' )
dir = 's'; // w is control key for turning downward
}
void TurnLeft(){
if( dir!='d' )
dir = 'a'; // w is control key for turning left
}
void TurnRight(){
if( dir!='a' )
dir = 'd'; // w is control key for turning right
}
void WelcomeScreen(){
SetConsoleTextAttribute(console,15);
cout<<"\n /^\\/^\\ ";
cout<<"\n _|__| O| ";
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 ~-_ _-~ ~-_ _-~ - jurcy -";
cout<<"\n ~--______-~ ~-___-~ ";
}
void Move(){
// Clear screen
system("cls");
if( state == 0 ){
if( !started ){
WelcomeScreen();
cout<<"\n\nPress any key to start";
getch();
started = 1;
state = 1;
}else{
cout<<"\nGame Over";
cout<<"\nPress any key to start";
getch();
state = 1;
size = 1;
}
}
// making snake body follow its head
for(int i=size-1; i>0; i--){
cell[i-1]->CopyPos(cell[i]);
}
// turning snake's head
switch(dir){
case 'w':
cell[0]->MoveUp();
break;
case 's':
cell[0]->MoveDown();
break;
case 'a':
cell[0]->MoveLeft();
break;
case 'd':
cell[0]->MoveRight();
break;
}
if( SelfCollision() )
state = 0;
// Collision with fruit
if( fruit.GetX() == cell[0]->GetX() && fruit.GetY() == cell[0]->GetY()){
AddCell(0,0);
fruit.SetPoint(rand()%MAXFRAMEX, rand()%MAXFRAMEY);
}
//drawing snake
for(int i=0; i<size; i++)
cell[i]->Draw();
SetConsoleTextAttribute(console,242);
if( !blink )
fruit.Draw('#');
blink = !blink;
SetConsoleTextAttribute(console,252);
//Debug();
Sleep(100);
}
int SelfCollision(){
for(int i=1; i<size; i++)
if( cell[0]->IsEqual(cell[i]) )
return 1;
return 0;
}
void Debug(){
for( int i=0; i<size; i++){
cell[i]->Debug();
}
}
};
int main(){
//random no generation
setcursor(0,0);
srand( (unsigned)time(NULL));
// Testing snake
Snake snake;
char op = 'l';
do{
if(kbhit()){
op = getch();
}
switch(op){
case 'w':
case 'W':
snake.TurnUp();
break;
case 's':
case 'S':
snake.TurnDown();
break;
case 'a':
case 'A':
snake.TurnLeft();
break;
case 'd':
case 'D':
snake.TurnRight();
break;
}
snake.Move();
}while(op != 'e');
return 0;
}
// THE END