-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSnake.cpp
More file actions
186 lines (167 loc) · 3.14 KB
/
Snake.cpp
File metadata and controls
186 lines (167 loc) · 3.14 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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <dos.h>
#include <time.h>
#define MAXSNAKESIZE 100
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);
}
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--;
}
void MoveDown(){
y++;
}
void MoveLeft(){
x--;
}
void MoveRight(){
x++;
}
void Draw(){
gotoxy(x,y);
cout<<"*";
}
void Erase(){
gotoxy(x,y);
cout<<" ";
}
void CopyPos(Point * p){
p->x = x;
p->y = y;
}
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;
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()%50, rand()%25);
}
void AddCell(int x, int y){
cell[size++] = new Point(x,y);
}
void TurnUp(){
dir = 'w'; // w is control key for turning upward
}
void TurnDown(){
dir = 's'; // w is control key for turning downward
}
void TurnLeft(){
dir = 'a'; // w is control key for turning left
}
void TurnRight(){
dir = 'd'; // w is control key for turning right
}
void Move(){
// Clear screen
system("cls");
// 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;
}
// Collision with fruit
if( fruit.GetX() == cell[0]->GetX() && fruit.GetY() == cell[0]->GetY()){
AddCell(0,0);
fruit.SetPoint(rand()%50, rand()%25);
}
//drawing snake
for(int i=0; i<size; i++)
cell[i]->Draw();
fruit.Draw();
//Debug();
Sleep(100);
}
void Debug(){
for( int i=0; i<size; i++){
cell[i]->Debug();
}
}
};
int main(){
//random no generation
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