-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
307 lines (267 loc) · 7.89 KB
/
Copy pathmain.cpp
File metadata and controls
307 lines (267 loc) · 7.89 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
bool GameOver;
const int WIDTH = 20;
const int HEIGHT = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
int speed = 500000;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
Mix_Chunk *deathSound = nullptr;
Mix_Chunk *scoreSound = nullptr;
Mix_Music *backgroundMusic = nullptr;
void setup() {
GameOver = false;
dir = STOP;
x = WIDTH / 2;
y = HEIGHT / 2;
fruitX = std::rand() % WIDTH;
fruitY = std::rand() % HEIGHT;
score = 0;
nTail = 0; // Reset tail length
speed = 500000; // Reset speed to initial value
}
bool initSDL() {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return false;
}
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
std::cerr << "Mix_Init failed: " << Mix_GetError() << std::endl;
return false;
}
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0) {
std::cerr << "Mix_OpenAudio failed: " << Mix_GetError() << std::endl;
return false;
}
return true;
}
void playMusic() {
if (backgroundMusic) {
Mix_FreeMusic(backgroundMusic);
backgroundMusic = nullptr;
}
backgroundMusic = Mix_LoadMUS("assets/music.wav");
if (!backgroundMusic) {
std::cerr << "Failed to load background music: " << Mix_GetError() << std::endl;
return;
}
Mix_PlayMusic(backgroundMusic, -1);
}
void stopMusic() {
if (backgroundMusic) {
Mix_HaltMusic(); // Stop the music when the game ends or restarts
}
}
void loadSoundEffects() {
deathSound = Mix_LoadWAV("assets/death.mp3");
if (!deathSound) {
std::cerr << "Error loading death sound: " << Mix_GetError() << std::endl;
}
scoreSound = Mix_LoadWAV("assets/score.mp3");
if (!scoreSound) {
std::cerr << "Error loading score sound: " << Mix_GetError() << std::endl;
}
}
void playDeathSound() {
if (deathSound) {
Mix_PlayChannel(-1, deathSound, 0); // Play the death sound
}
}
void playScoreSound() {
if (scoreSound) {
Mix_PlayChannel(-1, scoreSound, 0); // Play the score sound
}
}
void closeSDL() {
Mix_FreeChunk(deathSound);
Mix_FreeChunk(scoreSound);
Mix_FreeMusic(backgroundMusic);
Mix_CloseAudio();
Mix_Quit();
SDL_Quit();
}
void displayMenu() {
std::cout << "\033[2J\033[1;1H"; // Clear the screen, preserving colors
std::cout << "Snake Game!" << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Controls:" << std::endl;
std::cout << "W - Move Up" << std::endl;
std::cout << "A - Move Left" << std::endl;
std::cout << "S - Move Down" << std::endl;
std::cout << "D - Move Right" << std::endl;
std::cout << "Q - Quit" << std::endl;
std::cout << "Press Enter to Start" << std::endl;
}
void displayGameOverMenu() {
std::cout << "\033[2J\033[1;1H"; // Clear the screen, preserving colors
std::cout << "Game Over!" << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "----------------------" << std::endl;
std::cout << "Press R to restart or Q to quit" << std::endl;
}
void draw() {
std::cout << "\033[2J\033[1;1H"; // Clear screen but preserve color
for (int i = 0; i < WIDTH + 2; i++) std::cout << "-";
std::cout << std::endl;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == 0)
std::cout << "|";
if (i == y && j == x)
std::cout << "\033[1;32mÖ\033[0m"; // Snake head in green
else if (i == fruitY && j == fruitX)
std::cout << "\033[1;31mX\033[0m"; // Fruit in red
else {
bool print = false;
for (int k = 0; k < nTail; k++) {
if (tailX[k] == j && tailY[k] == i) {
std::cout << "\033[1;33mo\033[0m"; // Tail in yellow
print = true;
}
}
if (!print)
std::cout << " ";
}
if (j == WIDTH - 1)
std::cout << "|";
}
std::cout << std::endl;
}
for (int i = 0; i < WIDTH + 2; i++) std::cout << "-";
std::cout << std::endl;
std::cout << "Score: " << score << std::endl;
std::cout << "Speed: " << std::fixed << std::setprecision(2) << 1.0 / static_cast<float>(speed) * 500000 << std::endl;
}
int kbhit() {
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
void input() {
if (kbhit()) {
char ch = getchar();
if (ch == 'a') {
dir = LEFT;
} else if (ch == 'd') {
dir = RIGHT;
} else if (ch == 's') {
dir = DOWN;
} else if (ch == 'w') {
dir = UP;
} else if (ch == 'x') {
GameOver = true;
}
}
}
void logic() {
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x >= WIDTH || x < 0 || y >= HEIGHT || y < 0)
GameOver = true;
for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
GameOver = true;
if (x == fruitX && y == fruitY) {
score += 10;
speed = static_cast<int>(speed * 0.95);
fruitX = std::rand() % WIDTH;
fruitY = std::rand() % HEIGHT;
nTail++;
playScoreSound(); // Play score sound when eating fruit
}
}
int main() {
if (!initSDL()) {
return -1; // Exit if initialization fails
}
loadSoundEffects(); // Load sound effects
displayMenu();
// Wait for user input to start the game
while (true) {
if (kbhit()) {
char ch = getchar();
if (ch == '\n') break; // Start game when Enter is pressed
else if (ch == 'q') return 0; // Quit the game when Q is pressed
}
}
playMusic(); // Start playing background music after starting the game
setup(); // Initialize game variables
// Game loop
while (!GameOver) {
draw();
input();
logic();
if (GameOver) {
stopMusic(); // Stop the background music
playDeathSound(); // Play the death sound
displayGameOverMenu();
while (true) {
if (kbhit()) {
char ch = getchar();
if (ch == 'r') {
setup(); // Restart the game
playMusic(); // Restart music after reset
break; // Exit game over loop
} else if (ch == 'q') {
closeSDL(); // Quit the game
return 0;
}
}
}
}
usleep(speed); // Control the game speed
}
closeSDL(); // Clean up SDL resources
return 0;
}