-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
611 lines (538 loc) · 17.1 KB
/
Copy pathmain.cpp
File metadata and controls
611 lines (538 loc) · 17.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#define _CRT_SECURE_NO_WARNINGS
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include <GL/glut.h>
#include <vector>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <string>
int powerUpsSpawned = 0;
float powerUpSpawnTimer = 0.0f;
struct Bullet { float x, y; };
enum EnemyType { SMALL, MEDIUM, LARGE, BOSS };
struct Enemy {
float x = 0.0f, y = 0.0f;
bool alive = true;
EnemyType type = SMALL;
float speed = 0.01f;
int health = 1;
float movementTimer = 0.0f;
float direction = 1.0f; // For boss movement pattern
};
enum PowerUpType { RAPID_FIRE, SHIELD, EXTRA_LIFE };
struct PowerUp {
float x, y;
PowerUpType type;
bool active = true;
};
struct Explosion { float x, y; float radius = 0.0f; bool active = true; };
struct Star { float x, y; };
std::vector<Bullet> bullets;
std::vector<Enemy> enemies;
std::vector<Explosion> explosions;
std::vector<Star> stars;
std::vector<PowerUp> powerUps;
float shipX = 0.0f;
float shipVelocity = 0.0f;
float maxShipSpeed = 0.02f;
int score = 0, lives = 3, level = 1;
int highScore = 0;
bool gameOver = false, gameStarted = false, isPaused = false;
bool hasShield = false;
bool hasRapidFire = false;
float powerUpTimer = 0.0f;
float rapidFireTimer = 0.0f;
bool bossActive = false;
int nextBossScore = 200;
float gameTime = 0.0f;
// Load high score from file
void loadHighScore() {
std::ifstream inFile("highscore.dat");
if (inFile.is_open()) {
inFile >> highScore;
inFile.close();
}
}
// Save high score to file
void saveHighScore() {
if (score > highScore) {
highScore = score;
std::ofstream outFile("highscore.dat");
if (outFile.is_open()) {
outFile << highScore;
outFile.close();
}
}
}
void drawText(const char* str, float x, float y, void* font = GLUT_BITMAP_HELVETICA_18) {
glRasterPos2f(x, y);
for (int i = 0; str[i] != '\0'; i++)
glutBitmapCharacter(font, str[i]);
}
void drawShip() {
// Draw shield if active
if (hasShield) {
glColor4f(0.4f, 0.8f, 1.0f, 0.5f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(shipX, -0.9f);
for (int i = 0; i <= 20; i++) {
float angle = i * 2.0f * M_PI / 20;
glVertex2f(shipX + cos(angle) * 0.08f, -0.9f + sin(angle) * 0.08f);
}
glEnd();
}
// Draw ship
glColor3f(0.0f, 0.9f, 1.0f);
glBegin(GL_POLYGON);
glVertex2f(shipX, -0.85f);
glVertex2f(shipX - 0.03f, -0.95f);
glVertex2f(shipX - 0.015f, -1.0f);
glVertex2f(shipX + 0.015f, -1.0f);
glVertex2f(shipX + 0.03f, -0.95f);
glEnd();
glColor3f(1.0f, 0.3f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(shipX - 0.015f, -1.0f);
glVertex2f(shipX + 0.015f, -1.0f);
glVertex2f(shipX, -1.05f);
glEnd();
// Draw rapid fire indicator
if (hasRapidFire) {
glColor3f(1.0f, 0.8f, 0.0f);
glBegin(GL_QUADS);
glVertex2f(shipX - 0.025f, -0.85f);
glVertex2f(shipX + 0.025f, -0.85f);
glVertex2f(shipX + 0.02f, -0.82f);
glVertex2f(shipX - 0.02f, -0.82f);
glEnd();
}
}
void drawBullet(float x, float y) {
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(x - 0.01f, y);
glVertex2f(x + 0.01f, y);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex2f(x + 0.01f, y + 0.05f);
glVertex2f(x - 0.01f, y + 0.05f);
glEnd();
}
void drawEnemy(Enemy& e) {
if (e.type == BOSS) {
// Draw boss enemy
glColor3f(0.8f, 0.1f, 0.5f);
// Draw boss main body
glBegin(GL_POLYGON);
float radius = 0.12f;
for (int i = 0; i < 20; ++i) {
float angle = 2 * M_PI * i / 20;
glVertex2f(e.x + radius * cos(angle), e.y + radius * sin(angle));
}
glEnd();
// Draw boss details
glColor3f(1.0f, 0.3f, 0.3f);
glBegin(GL_TRIANGLES);
glVertex2f(e.x - 0.12f, e.y);
glVertex2f(e.x - 0.15f, e.y - 0.05f);
glVertex2f(e.x - 0.15f, e.y + 0.05f);
glVertex2f(e.x + 0.12f, e.y);
glVertex2f(e.x + 0.15f, e.y - 0.05f);
glVertex2f(e.x + 0.15f, e.y + 0.05f);
glEnd();
// Health bar
float healthPercent = e.health / 10.0f;
glColor3f(1.0f - healthPercent, healthPercent, 0.0f);
glBegin(GL_QUADS);
glVertex2f(e.x - 0.1f, e.y + 0.15f);
glVertex2f(e.x - 0.1f + 0.2f * healthPercent, e.y + 0.15f);
glVertex2f(e.x - 0.1f + 0.2f * healthPercent, e.y + 0.17f);
glVertex2f(e.x - 0.1f, e.y + 0.17f);
glEnd();
}
else {
// Draw regular enemies
switch (e.type) {
case SMALL: glColor3f(0.9f, 0.1f, 0.1f); break;
case MEDIUM: glColor3f(0.7f, 0.3f, 0.3f); break;
case LARGE: glColor3f(0.5f, 0.1f, 0.1f); break;
default: break;
}
glBegin(GL_POLYGON);
float radius = (e.type == SMALL ? 0.04f : e.type == MEDIUM ? 0.06f : 0.08f);
for (int i = 0; i < 20; ++i) {
float angle = 2 * M_PI * i / 20;
glVertex2f(e.x + radius * cos(angle), e.y + radius * sin(angle));
}
glEnd();
}
}
void drawPowerUp(PowerUp& p) {
glBegin(GL_QUADS);
switch (p.type) {
case RAPID_FIRE:
glColor3f(1.0f, 0.8f, 0.0f);
break;
case SHIELD:
glColor3f(0.0f, 0.8f, 1.0f);
break;
case EXTRA_LIFE:
glColor3f(0.0f, 1.0f, 0.0f);
break;
}
glVertex2f(p.x - 0.03f, p.y - 0.03f);
glVertex2f(p.x + 0.03f, p.y - 0.03f);
glVertex2f(p.x + 0.03f, p.y + 0.03f);
glVertex2f(p.x - 0.03f, p.y + 0.03f);
glEnd();
// Draw symbol inside power-up
glBegin(GL_LINES);
glColor3f(1.0f, 1.0f, 1.0f);
switch (p.type) {
case RAPID_FIRE:
// Draw "F" for rapid fire
glVertex2f(p.x - 0.015f, p.y - 0.015f);
glVertex2f(p.x - 0.015f, p.y + 0.015f);
glVertex2f(p.x - 0.015f, p.y);
glVertex2f(p.x + 0.01f, p.y);
break;
case SHIELD:
// Draw "S" for shield
glVertex2f(p.x + 0.01f, p.y + 0.015f);
glVertex2f(p.x - 0.01f, p.y + 0.015f);
glVertex2f(p.x - 0.01f, p.y + 0.015f);
glVertex2f(p.x - 0.01f, p.y);
glVertex2f(p.x - 0.01f, p.y);
glVertex2f(p.x + 0.01f, p.y);
glVertex2f(p.x + 0.01f, p.y);
glVertex2f(p.x + 0.01f, p.y - 0.015f);
glVertex2f(p.x + 0.01f, p.y - 0.015f);
glVertex2f(p.x - 0.01f, p.y - 0.015f);
break;
case EXTRA_LIFE:
// Draw "+" for extra life
glVertex2f(p.x - 0.015f, p.y);
glVertex2f(p.x + 0.015f, p.y);
glVertex2f(p.x, p.y - 0.015f);
glVertex2f(p.x, p.y + 0.015f);
break;
}
glEnd();
}
void drawExplosion(Explosion& ex) {
if (!ex.active) return;
float alpha = 1.0f - ex.radius / 0.2f;
glBegin(GL_TRIANGLE_FAN);
glColor4f(1.0f, 1.0f, 0.0f, alpha);
glVertex2f(ex.x, ex.y);
for (int i = 0; i <= 20; i++) {
float angle = i * 2.0f * M_PI / 20;
float dx = cos(angle) * ex.radius;
float dy = sin(angle) * ex.radius;
glColor4f(1.0f, 0.4f + 0.6f * ((rand() % 100) / 100.0f), 0.0f, alpha);
glVertex2f(ex.x + dx, ex.y + dy);
}
glEnd();
}
void drawStars() {
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POINTS);
for (auto& star : stars) glVertex2f(star.x, star.y);
glEnd();
}
void spawnEnemy() {
if (!bossActive) {
if (score >= nextBossScore && !std::any_of(enemies.begin(), enemies.end(), [](const Enemy& e) { return e.type == BOSS; })) {
// Spawn boss
Enemy boss;
boss.x = 0.0f;
boss.y = 1.0f;
boss.type = BOSS;
boss.speed = 0.005f;
boss.health = 10;
enemies.push_back(boss);
bossActive = true;
nextBossScore += 200; // Next boss appears 200 points later
}
else {
// Spawn regular enemy
EnemyType type = static_cast<EnemyType>(rand() % 3);
float speed = 0.01f + 0.002f * type + 0.0005f * level;
enemies.push_back({ (rand() % 200 - 100) / 100.0f, 1.0f, true, type, speed, 1 });
}
}
}
void spawnPowerUp() {
if (rand() % 150 <1 ) { // 2% chance per frame to spawn power-up
PowerUpType type = static_cast<PowerUpType>(rand() % 3);
powerUps.push_back({ (rand() % 180 - 90) / 100.0f, 1.0f, type, true });
}
}
void fireBullet() {
bullets.push_back({ shipX, -0.9f });
}
void update(int) {
if (!gameStarted || gameOver || isPaused) {
glutTimerFunc(16, update, 0);
return;
}
gameTime += 0.016f;
// Update ship position
shipX += shipVelocity;
if (shipX < -0.95f) shipX = -0.95f;
if (shipX > 0.95f) shipX = 0.95f;
shipVelocity *= 0.9f;
// Update power-up effects
if (hasRapidFire) {
rapidFireTimer += 0.016f;
if (rapidFireTimer >= 0.1f) { // Fire every 0.1 seconds
fireBullet();
rapidFireTimer = 0.0f;
}
powerUpTimer += 0.016f;
if (powerUpTimer >= 5.0f) { // Rapid fire lasts 5 seconds
hasRapidFire = false;
powerUpTimer = 0.0f;
}
}
// Update game objects
for (auto& b : bullets) b.y += 0.05f;
for (auto& e : enemies) {
if (e.type == BOSS) {
// Boss movement pattern - sway left and right
e.movementTimer += 0.016f;
if (e.y > 0.7f) {
e.y -= e.speed;
}
else {
e.x += e.direction * e.speed * 0.7f;
if (e.x > 0.7f || e.x < -0.7f) {
e.direction *= -1;
}
// Periodically fire bullets at player
if (fmod(e.movementTimer, 2.0f) < 0.05f) {
Enemy bullet;
bullet.x = e.x;
bullet.y = e.y - 0.1f;
bullet.type = SMALL;
bullet.speed = 0.02f;
enemies.push_back(bullet);
}
}
}
else {
e.y -= e.speed;
}
}
for (auto& p : powerUps) p.y -= 0.01f;
for (auto& ex : explosions) {
ex.radius += 0.01f;
if (ex.radius > 0.2f) ex.active = false;
}
for (auto& star : stars) {
star.y -= 0.002f;
if (star.y < -1.0f) star.y = 1.0f;
}
// Check bullet collisions with enemies
for (auto& e : enemies) {
float radius = 0.0f;
if (e.type == BOSS) radius = 0.12f;
else radius = (e.type == SMALL ? 0.04f : e.type == MEDIUM ? 0.06f : 0.08f);
for (auto it = bullets.begin(); it != bullets.end(); ) {
bool hitDetected = false;
if (e.alive && fabs(it->x - e.x) < radius && fabs(it->y - e.y) < radius) {
e.health--;
hitDetected = true;
if (e.health <= 0) {
e.alive = false;
explosions.push_back({ e.x, e.y });
if (e.type == BOSS) {
// Bigger explosion for boss
for (int i = 0; i < 5; i++) {
float offsetX = (rand() % 20 - 10) / 100.0f;
float offsetY = (rand() % 20 - 10) / 100.0f;
explosions.push_back({ e.x + offsetX, e.y + offsetY });
}
score += 50;
bossActive = false;
}
else {
score += (e.type + 1) * 10;
}
}
it = bullets.erase(it);
}
else {
++it;
}
}
}
// Check powerup collisions with ship
for (auto it = powerUps.begin(); it != powerUps.end(); ) {
if (it->active && fabs(it->x - shipX) < 0.07f && fabs(it->y + 0.9f) < 0.07f) {
switch (it->type) {
case RAPID_FIRE:
hasRapidFire = true;
powerUpTimer = 0.0f;
break;
case SHIELD:
hasShield = true;
break;
case EXTRA_LIFE:
lives++;
break;
}
it = powerUps.erase(it);
}
else if (it->y < -1.0f) {
it = powerUps.erase(it);
}
else {
++it;
}
}
// Remove objects that are no longer active
enemies.erase(std::remove_if(enemies.begin(), enemies.end(), [](Enemy& e) {
return !e.alive || e.y < -1.0f;
}), enemies.end());
bullets.erase(std::remove_if(bullets.begin(), bullets.end(), [](Bullet& b) {
return b.y > 1.0f;
}), bullets.end());
explosions.erase(std::remove_if(explosions.begin(), explosions.end(), [](Explosion& e) {
return !e.active;
}), explosions.end());
// Check ship collisions with enemies
for (auto& e : enemies) {
float radius = (e.type == BOSS) ? 0.12f :
(e.type == SMALL ? 0.04f : e.type == MEDIUM ? 0.06f : 0.08f);
if (e.alive && e.y < -0.85f && fabs(e.x - shipX) < (radius + 0.03f)) {
if (hasShield) {
// Shield absorbs the hit
hasShield = false;
e.alive = false;
explosions.push_back({ e.x, e.y });
}
else {
lives--;
e.alive = false;
explosions.push_back({ e.x, e.y });
if (lives <= 0) {
gameOver = true;
saveHighScore();
}
}
}
}
// Spawn enemies and power-ups
if (rand() % (50 - level) == 0) spawnEnemy();
spawnPowerUp();
// Update level based on score
level = 1 + score / 100;
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawStars();
if (!gameStarted) {
glColor3f(1, 1, 1);
drawText("\xF0\x9F\x9A\x80 SPACE FIGHTER \xF0\x9F\x9A\x80", -0.35f, 0.3f);
drawText("Press ENTER to Start", -0.3f, -0.1f);
char highScoreText[64];
sprintf(highScoreText, "High Score: %d", highScore);
drawText(highScoreText, -0.2f, -0.3f);
glutSwapBuffers();
return;
}
drawShip();
for (auto& b : bullets) drawBullet(b.x, b.y);
for (auto& e : enemies) drawEnemy(e);
for (auto& p : powerUps) drawPowerUp(p);
for (auto& ex : explosions) drawExplosion(ex);
// Draw HUD
char hud[64];
sprintf(hud, "Score: %d Lives: %d Level: %d", score, lives, level);
glColor3f(1, 1, 1);
drawText(hud, -0.95f, 0.9f);
if (gameOver) {
char gameOverText[64];
sprintf(gameOverText, "GAME OVER! Final Score: %d", score);
glColor3f(1, 0.3f, 0.3f);
drawText(gameOverText, -0.4f, 0.1f);
char highScoreText[64];
sprintf(highScoreText, "High Score: %d", highScore);
glColor3f(1, 1, 0.5f);
drawText(highScoreText, -0.2f, 0.0f);
glColor3f(1, 1, 1);
drawText("Press R to Restart", -0.25f, -0.1f);
}
if (isPaused && !gameOver) {
glColor3f(1, 1, 0.5f);
drawText("PAUSED", -0.1f, 0.0f);
drawText("Press P to Resume", -0.25f, -0.1f);
}
glutSwapBuffers();
}
void keyboard(unsigned char key, int, int) {
if (!gameStarted && key == 13) { // Enter key
gameStarted = true;
glutTimerFunc(0, update, 0);
}
if ((key == ' ' || key == 'z') && !gameOver && gameStarted && !isPaused) {
fireBullet();
}
if (key == 'r' || key == 'R') {
gameOver = false;
score = 0;
lives = 3;
level = 1;
bullets.clear();
enemies.clear();
explosions.clear();
powerUps.clear();
hasShield = false;
hasRapidFire = false;
isPaused = false;
bossActive = false;
nextBossScore = 200;
glutTimerFunc(0, update, 0);
}
if ((key == 'p' || key == 'P') && gameStarted && !gameOver) {
isPaused = !isPaused;
if (!isPaused) glutTimerFunc(0, update, 0);
}
}
void special(int key, int, int) {
if (!gameStarted || isPaused) return;
if (key == GLUT_KEY_LEFT) shipVelocity -= 0.002f;
if (key == GLUT_KEY_RIGHT) shipVelocity += 0.002f;
if (shipVelocity > maxShipSpeed) shipVelocity = maxShipSpeed;
if (shipVelocity < -maxShipSpeed) shipVelocity = -maxShipSpeed;
}
void init() {
glClearColor(0.0, 0.0, 0.05, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gluOrtho2D(-1, 1, -1, 1);
srand(static_cast<unsigned>(time(0)));
// Generate stars
for (int i = 0; i < 100; i++)
stars.push_back({ (rand() % 200 - 100) / 100.0f, (rand() % 200 - 100) / 100.0f });
// Load high score
loadHighScore();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA);
glutInitWindowSize(800, 800);
glutCreateWindow("\xF0\x9F\x9A\x80 Space Fighter: Bullet & Blast Game");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMainLoop();
return 0;
}