-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.cpp
More file actions
218 lines (178 loc) · 5.22 KB
/
SnakeGame.cpp
File metadata and controls
218 lines (178 loc) · 5.22 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
#include <bits/stdc++.h>
#include <conio.h> // For kbhit and getch
#include <windows.h> // For console cursor control
using namespace std;
#define MAX_LENGTH 1000
// Directions
const char DIR_UP = 'U';
const char DIR_DOWN = 'D';
const char DIR_LEFT = 'L';
const char DIR_RIGHT = 'R';
int consoleWidth, consoleHeight;
// Function to initialize the screen dimensions
void initScreen() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
consoleHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
consoleWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
}
// Point structure to represent coordinates
struct Point {
int xCoord, yCoord;
Point() {}
Point(int x, int y) {
xCoord = x;
yCoord = y;
}
};
// Snake class
class Snake {
int length;
char direction;
public:
Point body[MAX_LENGTH];
Snake(int x, int y) {
length = 1;
body[0] = Point(x, y);
direction = DIR_RIGHT;
}
int getLength() {
return length;
}
void changeDirection(char newDirection) {
if (newDirection == DIR_UP && direction != DIR_DOWN) {
direction = newDirection;
} else if (newDirection == DIR_DOWN && direction != DIR_UP) {
direction = newDirection;
} else if (newDirection == DIR_LEFT && direction != DIR_RIGHT) {
direction = newDirection;
} else if (newDirection == DIR_RIGHT && direction != DIR_LEFT) {
direction = newDirection;
}
}
bool move(Point food) {
for (int i = length - 1; i > 0; i--) {
body[i] = body[i - 1];
}
switch (direction) {
case DIR_UP:
body[0].yCoord--;
break;
case DIR_DOWN:
body[0].yCoord++;
break;
case DIR_RIGHT:
body[0].xCoord++;
break;
case DIR_LEFT:
body[0].xCoord--;
break;
}
// Snake bites itself
for (int i = 1; i < length; i++) {
if (body[0].xCoord == body[i].xCoord && body[0].yCoord == body[i].yCoord) {
return false;
}
}
// Snake eats food
if (food.xCoord == body[0].xCoord && food.yCoord == body[0].yCoord) {
body[length] = Point(body[length - 1].xCoord, body[length - 1].yCoord);
length++;
}
return true;
}
};
// Board class
class Board {
Snake* snake;
const char SNAKE_BODY = 'O';
Point food;
const char FOOD = 'o';
int score;
public:
Board() {
spawnFood();
snake = new Snake(10, 10);
score = 0;
}
~Board() {
delete snake;
}
int getScore() {
return score;
}
void spawnFood() {
int x = rand() % consoleWidth;
int y = rand() % consoleHeight;
food = Point(x, y);
}
void displayCurrentScore() {
gotoxy(consoleWidth / 2, 0);
cout << "Current Score: " << score;
}
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void draw() {
system("cls");
for (int i = 0; i < snake->getLength(); i++) {
gotoxy(snake->body[i].xCoord, snake->body[i].yCoord);
cout << SNAKE_BODY;
}
gotoxy(food.xCoord, food.yCoord);
cout << FOOD;
displayCurrentScore();
}
bool update() {
bool isAlive = snake->move(food);
if (!isAlive) {
return false;
}
if (food.xCoord == snake->body[0].xCoord && food.yCoord == snake->body[0].yCoord) {
score++;
spawnFood();
}
return true;
}
void getInput() {
if (kbhit()) {
int key = getch();
if (key == 224) { // Special key prefix for arrow keys
key = getch(); // Get actual keycode
switch (key) {
case 72: // Up arrow
snake->changeDirection(DIR_UP);
break;
case 75: // Left arrow
snake->changeDirection(DIR_LEFT);
break;
case 80: // Down arrow
snake->changeDirection(DIR_DOWN);
break;
case 77: // Right arrow
snake->changeDirection(DIR_RIGHT);
break;
}
}
}
}
};
// Main function
int main() {
srand(time(0));
initScreen();
Board* board = new Board();
while (board->update()) {
board->getInput();
board->draw();
Sleep(100); // Delay for smoother gameplay
}
cout << "Game Over" << endl;
cout << "Final Score: " << board->getScore() << endl;
delete board;
return 0;
}