-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhighscore.cpp
More file actions
92 lines (58 loc) · 1.8 KB
/
Copy pathhighscore.cpp
File metadata and controls
92 lines (58 loc) · 1.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
#include "highscore.h"
using namespace std;
void show_highscores(int rank, BITMAP* background) {
fstream file("HighScores");
string players[HIGHSCORE_TABLE];
int scores[HIGHSCORE_TABLE];
for (int i = 0; i < HIGHSCORE_TABLE; ++i) {
file >> players[i];
file >> scores[i];
}
file.close();
do {
clear(buffer);
draw_sprite(buffer, background, 0, 0);
textout_centre(buffer, font_big, "High Scores", SCREEN_W/2, 5, makecol(220, 0, 0));
for (int i = 0; i < HIGHSCORE_TABLE; ++i) {
int d = 255 - (i * 20);
int m = i * 50;
int col = makecol(d, d, d);
if (i == rank) {
col = makecol(0, 255, 0);
}
textprintf(buffer, font_interface, 130 + (i > 8 ? 20 : 0), 75 + m, col, "%s", players[i].c_str());
textprintf(buffer, font_big, 100, 70 + m, makecol(d, d, 0), "%d", i+1);
textprintf(buffer, font, 130 + (i > 8 ? 20 : 0), 105 + m, makecol(d, d, 0), "%d", scores[i]);
}
draw_sprite(buffer, mouse_sprite, mouse_x, mouse_y);
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
} while (!key[KEY_ESC] && !key[KEY_SPACE] && !key[KEY_ENTER] && mouse_b != 1);
}
int save_highscore(char* name, int score) {
fstream file("HighScores");
string players[HIGHSCORE_TABLE];
int scores[HIGHSCORE_TABLE];
int rank = 0;
for (int i = 0; i < HIGHSCORE_TABLE; ++i) {
file >> players[i];
file >> scores[i];
if (scores[i] >= score)
++rank;
}
file.close();
if (rank < HIGHSCORE_TABLE) {
for (int i = HIGHSCORE_TABLE - 1; i > rank; --i) {
players[i] = players[i-1];
scores[i] = scores[i-1];
}
players[rank] = name;
scores[rank] = score;
file.open("HighScores", ios::out);
for (int i = 0; i < HIGHSCORE_TABLE; ++i) {
file << players[i] << "\n";
file << scores[i] << "\n";
}
file.close();
}
return rank + 1;
}