-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
785 lines (697 loc) · 17.8 KB
/
main.cpp
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#include <iostream>
#include <cstdio>
#include <ncurses.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
using namespace std;
string currentplayerName;
const int screen_width = 80;
const int screen_height = 30;
const char rocket_symbol = '>';
const char meteor1_symbol = '#';
const char powerup_symbol = 'o';
const char drops = 'v';
const int max_leaderboard_size = 10;
class Player
{
private:
string name;
int score;
public:
Player(const string &playerName, int playerScore)
{
name = playerName;
score = playerScore;
}
string getName() const
{
return name;
}
int getScore() const
{
return score;
}
void setscore(int x){
score=x;
}
};
bool compare(Player a,Player b){
return a.getScore()<b.getScore();
}
int searchinLeaderBoard(Player temp,vector<Player>Leaderboard){
for(int i=0;i<Leaderboard.size();i++){
if(temp.getName()==Leaderboard[i].getName()){
// Leaderboard[i].setscore(temp.getScore());
return i;
}
}
return -1;
}
class Rocket
{
private:
int x, y;
bool isFiring;
friend class Game;
public:
Rocket(int startX, int startY)
{
x = startX;
y = startY;
}
void draw()
{
mvprintw(y, x, ">-<%c", rocket_symbol);
}
void draw2()
{
if (isFiring)
{
mvprintw(y, x, ">>-<%c)", rocket_symbol);
}
else
{
mvprintw(y, x, ">>-<%c", rocket_symbol);
}
}
void clear()
{
mvprintw(y, x, " ");
}
void clear2()
{
mvprintw(y, x, " ");
}
void moveUp1()
{
clear();
y--;
if (y <= 1)
y = 1;
draw();
}
void moveUp2()
{
clear2();
y--;
if (y <= 1)
y = 1;
draw2();
}
void moveDown1()
{
clear();
y++;
if (y >= screen_height - 2)
y = screen_height - 2;
draw();
}
void moveDown2()
{
clear2();
y++;
if (y >= screen_height - 1)
y = screen_height - 2;
draw2();
}
void moveRight()
{
clear();
x++;
if (x >= screen_width - 1)
x = screen_width - 1;
draw();
}
void moveLeft()
{
clear();
x--;
if (x < 0)
x = 0;
draw();
}
int getX() const
{
return x;
}
int getY() const
{
return y;
}
};
class Meteor
{
private:
int x, y;
public:
Meteor(int startX, int startY)
{
x = startX;
y = startY;
}
void draw()
{
mvprintw(y, x, "%c", meteor1_symbol);
}
void clear()
{
mvprintw(y, x, " ");
}
void moveLeft()
{
clear();
x--;
if (x < 0)
{
x = screen_width - 1;
y = rand() % screen_height;
}
draw();
}
int getX() const
{
return x;
}
int getY() const
{
return y;
}
void setX(int a)
{
x = a;
}
void setY(int b)
{
y = b;
}
};
class Drop
{
private:
int x, y;
public:
Drop(int startX, int startY)
{
x = startX;
y = startY;
}
void draw()
{
mvprintw(y, x, "%c", drops);
}
void clear()
{
mvprintw(y, x, " ");
}
void moveDown()
{
clear();
y++;
if (y > screen_height - 1)
{
y = 1;
}
draw();
}
void moveLeft()
{
clear();
x--;
if (x < 0)
{
y = 1;
x = rand() % screen_width;
}
draw();
}
int getX() const
{
return x;
}
int getY() const
{
return y;
}
void setX(int a)
{
x = a;
}
void setY(int b)
{
y = b;
}
};
class Powerups
{
private:
int x;
int y;
public:
Powerups(int startX, int startY)
{
x = startX;
y = startY;
}
void draw()
{
mvprintw(y, x, "%c", powerup_symbol);
}
void clear()
{
mvprintw(y, x, " ");
}
void moveleft()
{
clear();
x--;
if (x < 0)
{
x = screen_width - 1;
y = rand() % screen_height;
}
draw();
}
int getx() const
{
return x;
}
int gety() const
{
return y;
}
void setx(int a)
{
x = a;
}
void sety(int b)
{
y = b;
}
};
class Game
{
private:
Rocket rocket;
Meteor meteor1;
Meteor meteor2;
Powerups powerup;
Drop drop;
int score;
bool gameOver;
int level;
bool isFiring;
public:
Game() : rocket(screen_width / 4, screen_height / 2), meteor1(screen_width - 1, rand() % screen_height), meteor2(screen_width - 1, rand() % screen_height),
score(0), gameOver(false), powerup(rand() % screen_width, rand() % screen_height), level(1), drop(rand() % screen_width, 1) {}
int getlevel()
{
return level;
}
int getScore()
{
return score;
}
void sortLeaderboard(vector<Player> &leaderboard)
{
int n = leaderboard.size();
for (int i = 0; i < n - 1; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if (leaderboard[j].getScore() < leaderboard[j + 1].getScore())
{
swap(leaderboard[j], leaderboard[j + 1]);
}
}
}
}
void swap(Player &a, Player &b)
{
Player temp = a;
a = b;
b = temp;
}
void savePlayerInfo(const Player &player)
{
FILE *file = fopen("leaderboard.txt", "a");
if (file != NULL)
{
fprintf(file, "%s %d\n", player.getName().c_str(), player.getScore());
fclose(file);
}
else
{
cout << "Unable to save Player data";
}
}
vector<Player> Leaderboard;
void updateLeaderboard()
{
Leaderboard.clear();
FILE *file = fopen("leaderboard.txt", "r");
if (file != NULL)
{
char line[100];
while (fgets(line, sizeof(line), file) != NULL)
{
istringstream iss(line);
string name;
int score;
if (iss >> name >> score)
{
Player random(name, score);
int index=searchinLeaderBoard(random,Leaderboard);
if(index!=-1){
if(random.getScore()>Leaderboard[index].getScore()){
Leaderboard[index].setscore(random.getScore());
}
}
else{
Leaderboard.push_back(random);
}
sort(Leaderboard.begin(),Leaderboard.end(),compare);
}
}
fclose(file);
}
else
{
cout << "Unable to update leaderboard" << endl;
}
}
void displayLeaderboard()
{
string LeaderboardArt = "\n * .--.\n"
" / / `\n"
" + | |\n"
" ' \\ \\__,\n"
" * + '--' *\n"
" + /\n"
" + .' '. *\n"
" * /======\\ +\n"
" ;:. _ ;\n"
" |:. (_) |\n"
" |:. _ |\n"
" + |:. (_) | *\n"
" ;:. ;\n"
" .' \\:. / `.\n"
" / .-'':._.'`-. \n"
" |/ /||\\ \\|\n"
" _..--\"\"\"````\"\"\"--.._\n"
" _.-'`` ``'-._\n"
" -' '-\n";
cout << LeaderboardArt;
cout << " LEADERBOARD" << endl;
cout << " ==========================" << endl;
for (size_t i = 0; i < Leaderboard.size() && i < max_leaderboard_size; ++i)
{
cout << i + 1 << ". " << Leaderboard[i].getName() << " " << Leaderboard[i].getScore() << endl;
}
}
void run()
{
initscr();
noecho();
curs_set(0);
keypad(stdscr, true);
nodelay(stdscr, true);
srand(time(NULL));
while (!gameOver)
{
char key = getch();
if (key == 'w' || key == 'W')
{
rocket.moveUp1();
}
else if (key == 's' || key == 'S')
{
rocket.moveDown1();
}
else if (key == 'q' || key == 'Q')
gameOver = true;
if (level == 3)
{
if (key == 'f' || key == 'F')
{
isFiring = true;
rocket.isFiring = true;
}
else
{
isFiring = false;
rocket.isFiring = false;
}
if (key == 'd' || key == 'D')
{
rocket.moveRight();
}
else if (key == 'a' || key == 'A')
{
rocket.moveLeft();
}
}
meteor1.moveLeft();
meteor2.moveLeft();
powerup.moveleft();
if (level == 3)
{
drop.moveDown();
drop.moveLeft();
}
if ((rocket.getX() == meteor1.getX() && rocket.getY() == meteor1.getY()) || ((rocket.getX() == meteor2.getX() && rocket.getY() == meteor2.getY())))
{
gameOver = true;
break;
}
if (rocket.getX() == powerup.getx() && rocket.getY() == powerup.gety())
{
powerup.clear();
mvprintw(rocket.getY() - 1, rocket.getX(), "+50");
score = score + 100;
powerup.setx(screen_width - 1);
powerup.sety(rand() % screen_height);
}
if (rocket.getY() == meteor1.getY() && isFiring == true && meteor1.getX() > rocket.getX())
{
meteor1.clear();
meteor1.setX(screen_width - 1);
meteor1.setY(rand() % screen_height);
}
if (rocket.getY() == meteor2.getY() && isFiring == true && meteor2.getX() > rocket.getX())
{
meteor2.clear();
meteor2.setX(screen_width - 1);
meteor2.setY(rand() % screen_height);
}
if (rocket.getY() == drop.getY() && level == 3 && rocket.getX() == drop.getX())
{
gameOver = true;
break;
}
score++;
if (score < 500)
{
drawGame1();
napms(100);
}
if (score >= 500 && score < 1000)
{
level = 2;
drawGame2();
napms(50);
}
if (score >= 1000)
{
level = 3;
drawGame3();
napms(30);
}
}
endwin();
Player currentplayer(currentplayerName, score);
savePlayerInfo(currentplayer);
cout << endl
<< endl
<< "Game Over!" << endl
<< "Score: " << score << endl;
updateLeaderboard();
sortLeaderboard(Leaderboard);
displayLeaderboard();
}
void drawGame1()
{
clear();
for (int i = 0; i < screen_height; i++)
{
for (int j = 0; j < screen_width; j++)
{
if (i == 0 || i == screen_height - 1)
printw("-");
else if (i == rocket.getY() && j == rocket.getX())
rocket.draw();
else if (i == meteor1.getY() && j == meteor1.getX())
{
meteor1.draw();
}
else if (i == meteor2.getY() && j == meteor2.getX())
{
meteor2.draw();
}
else if (i == powerup.gety() && j == powerup.getx())
{
powerup.draw();
}
else if (i == 1 && j == 1)
{
mvprintw(1, 1, "%d", level);
}
else
printw(" ");
}
printw("\n");
}
printw("Score : %d\n", score);
refresh();
}
void drawGame2()
{
clear();
for (int i = 0; i < screen_height; i++)
{
for (int j = 0; j < screen_width; j++)
{
if (i == 0 || i == screen_height - 1)
printw("-");
else if (i == rocket.getY() && j == rocket.getX())
rocket.draw2();
else if (i == meteor1.getY() && j == meteor1.getX())
{
meteor1.draw();
}
else if (i == meteor2.getY() && j == meteor2.getX())
{
meteor2.draw();
}
else if (i == powerup.gety() && j == powerup.getx())
{
powerup.draw();
}
else if (i == 1 && j == 1)
{
mvprintw(1, 1, "%d", level);
}
else
printw(" ");
}
printw("\n");
}
printw("Score : %d\n", score);
refresh();
}
void drawGame3()
{
clear();
for (int i = 0; i < screen_height; i++)
{
for (int j = 0; j < screen_width; j++)
{
if (i == 0 || i == screen_height - 1)
printw("-");
else if (i == rocket.getY() && j == rocket.getX())
rocket.draw2();
else if (i == meteor1.getY() && j == meteor1.getX())
{
meteor1.draw();
}
else if (i == meteor2.getY() && j == meteor2.getX())
{
meteor2.draw();
}
else if (i == powerup.gety() && j == powerup.getx())
{
powerup.draw();
}
else if (i == 1 && j == 1)
{
mvprintw(1, 1, "%d", level);
}
else if (isFiring && i == rocket.getY() && j > rocket.getX() + 3 && j < screen_width - 1)
{
mvprintw(rocket.getY(), rocket.getX() + 4, ")");
mvprintw(rocket.getY(), j, "-");
}
else if (i == drop.getY() && j == drop.getX())
{
drop.draw();
}
else
printw(" ");
}
printw("\n");
}
printw("Score : %d\n", score);
refresh();
}
};
int main()
{
string rocketAsciiArt =
" ^\n"
" o / \\\n"
" /___\\\n"
" | | o Rules: \n"
" o | R | 1. Control the Rocket with 'W' and 'S' keys to move it Up and Down. \n"
" | O | 2. Avoid the Meteors[#] in the path of the Rocket.\n"
" | C | o 3. Collect as many power-ups[o] as you can to boost your score. \n"
" | K | 4. The GAME is divided into 3 levels:-\n"
" | E | Level 1: Speed slow | Level 2: Speed Medium | Level 3: Speed Fast+Drops[^]\n"
" | T | 5. Press 'Q' key to Quit the Game while playing.\n"
" | |\n"
" | |\n"
" /|##!##|\\\n"
" / |##!##| \\\n";
// Print the rocket ASCII art
cout << rocketAsciiArt << endl;
int n;
Game game;
cout << "ENTER your NICKNAME (only characters and numbers): ";
cin >> currentplayerName;
cout << endl
<< "Press 1 to Enter the game " << endl
<< "Press 0 to exit the program" << endl
<< endl;
cin >> n;
bool invalid = true;
while (invalid)
{
switch (n)
{
case 0:
{
invalid = false;
return 0;
break;
}
case 1:
{
invalid = false;
game.run();
break;
}
default:
{
cout << endl
<< "Invalid input !!" << endl
<< endl;
invalid = true;
cout << "Press '1'+return_key to Enter the game " << endl
<< "Press '0' to exit the program" << endl;
cin >> n;
}
}
}
}