Repository navigation
Expand file tree
/
Copy path2D-Racing-Game.cpp
More file actions
314 lines (272 loc) · 9.17 KB
/
Copy path2D-Racing-Game.cpp
File metadata and controls
314 lines (272 loc) · 9.17 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
308
309
310
311
312
313
314
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#else
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#endif
using namespace std;
const int roadWidth = 17; // Inner road width
const int screenHeight = 22;
// Coordinates
int playerX;
const int playerY = screenHeight - 4; // Fixed near the bottom
// Enemy settings
const int maxEnemies = 3;
int enemyX[maxEnemies];
int enemyY[maxEnemies];
bool enemyActive[maxEnemies];
// Game State
bool gameOver = false;
int score = 0;
int roadScroll = 0;
int spawnTimer = 0;
// ANSI Colors
const string RESET = "\033[0m";
const string RED = "\033[1;31m";
const string GREEN = "\033[1;32m";
const string YELLOW = "\033[1;33m";
const string BLUE = "\033[1;34m";
const string WHITE = "\033[1;37m";
const string GRAY = "\033[1;30m";
void HideCursor() {
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hOut, &cursorInfo);
cursorInfo.bVisible = false;
SetConsoleCursorInfo(hOut, &cursorInfo);
#else
cout << "\033[?25l";
#endif
}
void ShowCursor() {
#ifdef _WIN32
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hOut, &cursorInfo);
cursorInfo.bVisible = true;
SetConsoleCursorInfo(hOut, &cursorInfo);
#else
cout << "\033[?25h";
#endif
}
void Setup() {
playerX = roadWidth / 2 + 1; // Start in middle lane
for (int i = 0; i < maxEnemies; i++) {
enemyActive[i] = false;
}
HideCursor();
}
void SpawnEnemy() {
for (int i = 0; i < maxEnemies; i++) {
if (!enemyActive[i]) {
// Spawn in one of three lanes: Left (3), Middle (8), Right (13)
int lane = rand() % 3;
if (lane == 0) enemyX[i] = 3;
else if (lane == 1) enemyX[i] = 8;
else enemyX[i] = 13;
enemyY[i] = -3; // Start just above the screen boundary
enemyActive[i] = true;
break;
}
}
}
void Draw() {
string frame = "";
#ifdef _WIN32
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
#else
frame += "\033[H";
#endif
// Top Boundary
frame += YELLOW + "=========================\n" + RESET;
for (int y = 0; y < screenHeight; y++) {
// Red and White striped road borders that scroll
if ((y + roadScroll) % 4 < 2) {
frame += RED + "║ " + RESET; // Left Border stripe 1
} else {
frame += WHITE + "║ " + RESET; // Left Border stripe 2
}
for (int x = 1; x <= roadWidth; x++) {
bool pixelDrawn = false;
// 1. Draw Player Car (Blue body, White wheels)
// Center is playerX, occupying y to y+2
if (y >= playerY && y <= playerY + 2 && x >= playerX - 1 && x <= playerX + 1) {
int localY = y - playerY;
int localX = x - (playerX - 1);
if (localY == 0) { // Front wheels
if (localX == 0 || localX == 2) frame += WHITE + "o" + RESET;
else frame += " ";
} else if (localY == 1) { // Engine & Cabin
if (localX == 0) frame += BLUE + "[" + RESET;
else if (localX == 1) frame += BLUE + "A" + RESET;
else frame += BLUE + "]" + RESET;
} else if (localY == 2) { // Rear wheels
if (localX == 0 || localX == 2) frame += WHITE + "o" + RESET;
else frame += BLUE + "-" + RESET;
}
pixelDrawn = true;
}
// 2. Draw Enemy Cars (Red body, Yellow wheels)
if (!pixelDrawn) {
for (int i = 0; i < maxEnemies; i++) {
if (enemyActive[i]) {
if (y >= enemyY[i] && y <= enemyY[i] + 2 && x >= enemyX[i] - 1 && x <= enemyX[i] + 1) {
int localY = y - enemyY[i];
int localX = x - (enemyX[i] - 1);
if (localY == 0) { // Front wheels
if (localX == 0 || localX == 2) frame += YELLOW + "o" + RESET;
else frame += RED + "-" + RESET;
} else if (localY == 1) { // Engine / Cabin
if (localX == 0) frame += RED + "[" + RESET;
else if (localX == 1) frame += RED + "V" + RESET;
else frame += RED + "]" + RESET;
} else if (localY == 2) { // Rear wheels
if (localX == 0 || localX == 2) frame += YELLOW + "o" + RESET;
else frame += " ";
}
pixelDrawn = true;
break;
}
}
}
}
// 3. Draw Road Lane Markings
if (!pixelDrawn) {
// Dashed lines dividing the 3 lanes at X = 6 and X = 12
if (x == 6 || x == 12) {
if ((y + roadScroll) % 4 < 2) {
frame += WHITE + "|" + RESET;
} else {
frame += " ";
}
} else {
frame += " "; // Empty asphalt road space
}
}
}
// Right Border
if ((y + roadScroll) % 4 < 2) {
frame += RED + " ║\n" + RESET;
} else {
frame += WHITE + " ║\n" + RESET;
}
}
// Bottom Boundary
frame += YELLOW + "=========================\n" + RESET;
// HUD Display
frame += " Score: " + GREEN + to_string(score) + RESET + " pts \n";
frame += " Controls: " + WHITE + "A" + RESET + " = Left, " + WHITE + "D" + RESET + " = Right, " + RED + "Esc" + RESET + " = Exit\n";
cout << frame << flush;
}
void Input() {
#ifdef _WIN32
if (_kbhit()) {
switch (_getch()) {
case 'a':
case 'A':
if (playerX > 2) playerX -= 5; // Move exactly one lane left
break;
case 'd':
case 'D':
if (playerX < roadWidth - 2) playerX += 5; // Move exactly one lane right
break;
case 27: // Escape key
gameOver = true;
break;
}
}
#else
struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
struct timeval tv = {0, 0};
if (select(1, &rfds, NULL, NULL, &tv) > 0) {
char ch = getchar();
switch (ch) {
case 'a':
case 'A':
if (playerX > 2) playerX -= 5;
break;
case 'd':
case 'D':
if (playerX < roadWidth - 2) playerX += 5;
break;
case 27:
gameOver = true;
break;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#endif
}
void Logic() {
// 1. Scroll the road
roadScroll++;
score++; // Survival points
// 2. Spawn incoming enemies
spawnTimer++;
if (spawnTimer >= 12) { // Adjust spawn frequency
spawnTimer = 0;
SpawnEnemy();
}
// 3. Update Enemy Positions and check collisions
for (int i = 0; i < maxEnemies; i++) {
if (enemyActive[i]) {
enemyY[i]++; // Move downward
// Deactivate enemy if it goes off screen
if (enemyY[i] > screenHeight) {
enemyActive[i] = false;
}
// Bounding box collision checking (Both cars are 3x3)
if (abs(playerX - enemyX[i]) < 3 && abs(playerY - enemyY[i]) < 3) {
gameOver = true;
}
}
}
}
int main() {
srand(time(nullptr));
#ifdef _WIN32
// Setup Virtual Terminal Escapes for CMD/PowerShell Colors
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
system("cls");
#else
cout << "\033[2J";
#endif
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
#ifdef _WIN32
Sleep(50); // Frame delay (approx 20 FPS)
#else
usleep(50000);
#endif
}
ShowCursor();
// Game Over Crash screen
cout << "\n" << RED << " CRASH!!! GAME OVER! " << RESET << "\n";
cout << " Final Score: " << GREEN << score << RESET << " pts\n\n";
return 0;
}