-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.cpp
More file actions
215 lines (174 loc) · 5.8 KB
/
SnakeGame.cpp
File metadata and controls
215 lines (174 loc) · 5.8 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
#include <iostream>
#include <vector>
#include <cstdio>
#include <locale>
#include <clocale>
// #include <conio.h>
// #include <windows.h>
// #include <io.h>
// #include <fcntl.h>
// #include <thread>
// #include <chrono>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#else
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#endif
#include "Snake.h"
#include "GameGrid.h"
#include "Food.h"
using namespace std;
// Cross-platform sleep function (milliseconds)
void sleep_ms(int ms) {
#ifdef _WIN32
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
void showCursor() {
// HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
// CONSOLE_CURSOR_INFO info;
// info.dwSize = 100;
// info.bVisible = TRUE;
// SetConsoleCursorInfo(consoleHandle, &info);
#ifdef _WIN32
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = TRUE;
SetConsoleCursorInfo(consoleHandle, &info);
#else
cout << "\e[?25h" << flush; // ANSI escape to show cursor
#endif
}
// Cross-platform kbhit() and getch()
#ifdef _WIN32
// Use _kbhit() and _getch() directly
#define kbhit _kbhit
#define getch _getch
#else
int kbhit() {
static const int STDIN = 0;
static bool initialized = false;
if (!initialized) {
termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON; // Disable line buffering
term.c_lflag &= ~ECHO; // Turn off echo
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initialized = true;
}
int bytesWaiting;
ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting > 0;
}
int getch() {
return getchar();
}
#endif
// Cross-platform clear screen
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
// system("clear");
std::cout << "\033[H\033[2J\033[3J" << std::flush;
#endif
}
// void clearScreen() {
// system("cls");
// }
int main() {
// system("chcp 65001");
// Sleep(5000);
// system("cls");
clearScreen();
#ifdef _WIN32
system("chcp 65001"); // Enable UTF-8 console on Windows
#else
setlocale(LC_ALL, "");
std::locale::global(std::locale(""));
#endif
// Cross-platform clear screen
// clearScreen();
bool playAgain = true;
while(playAgain){
GameGrid grid(20, 20);
Snake snake(grid.getWidth() / 2, grid.getHeight() / 2, 2, grid.getHeight(), grid.getWidth());
Food food(grid.getHeight(), grid.getWidth());
food.randomFoodPosition(snake.getSnakeBody());
char direction = 'D';
bool gameOver = false;
int score = 0;
while (!gameOver) {
// Draw
// grid.clearScreen();
grid.drawGameGrid(snake, food, score);
// Input
if (kbhit()) {
char input = getch();
if (input == 'x' || input == 'X'){
break;
}
if (input == 'w' || input == 'W' || input == 'a' || input == 'A' ||
input == 's' || input == 'S' || input == 'd' || input == 'D') {
direction = toupper(input);
}
}
snake.move(direction, false);
// auto head = snake.getSnakeBody()[0];
// cout << "Snake head at (" << head.x << "," << head.y << ")" << endl;
if(snake.isCollision()){
gameOver = true;
break;
}
// Check food collision
if (snake.getSnakeBody()[0].x == food.getPosition().x && snake.getSnakeBody()[0].y == food.getPosition().y) {
score += 10;
snake.move(direction, true);
food.randomFoodPosition(snake.getSnakeBody());
}
// Sleep(200); // Game speed
sleep_ms(200);
}
if (playAgain) { // Only show if not quit with 'X'
grid.showGameOver(score);
// Wait for user choice
bool validInput = false;
while (!validInput) {
if (kbhit()) {
char choice = getch();
if (choice == '\r' || choice == '\n') { // ENTER key
playAgain = true;
validInput = true;
// system("cls");
clearScreen();
} else if (choice == 'q' || choice == 'Q') { // Q key
playAgain = false;
validInput = true;
}
}
// Sleep(50); // Small delay to prevent CPU overuse
sleep_ms(50);
}
}
// cout << "Game Over! Final score: " << score << "\n";
// system("pause");
}
// system("cls");
clearScreen();
cout << "\n\n";
cout << " ╔══════════════════════════════════════════════════════╗\n";
cout << " ║ ║\n";
cout << " ║ Thanks for playing Snake Game by SnakeByte++! 🐍 ║\n";
cout << " ║ ║\n";
cout << " ╚══════════════════════════════════════════════════════╝\n";
cout << "\n";
return 0;
}