-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHighscore.ino
136 lines (127 loc) · 4.22 KB
/
Highscore.ino
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
#include <Gamebuino-Meta.h>
#include "declare.h"
#define NUM_HIGHSCORE 5
#define NAMELENGTH 12
int highscore[NUM_HIGHSCORE];
char name[NUM_HIGHSCORE][NAMELENGTH+1];
const uint16_t highscore_sound[] PROGMEM = {
0x0005,0x140,0x150,0x15C,0x170,0x180,0x16C,0x154,0x160,0x174,0x184,0x14C,0x15C,0x168,0x17C,0x18C,0x0000};
void initHighscore(){
for(byte thisScore = 0; thisScore < NUM_HIGHSCORE; thisScore++){
highscore[thisScore] = gb.save.get(2*thisScore);
gb.save.get(2*thisScore + 1, name[thisScore], NAMELENGTH+1);
}
}
void saveHighscore(unsigned int test_score){
if(test_score > highscore[NUM_HIGHSCORE-1]){//if it's a highscore
if(drawNewHighscore(test_score)){
gb.getDefaultName(name[NUM_HIGHSCORE-1]);
gb.gui.keyboard("Name", name[NUM_HIGHSCORE-1]);
highscore[NUM_HIGHSCORE-1] = test_score;
for(byte i=NUM_HIGHSCORE-1; i>0; i--){ //bubble sorting FTW
if(highscore[i-1] < highscore[i]){
char tempName[NAMELENGTH];
strcpy(tempName, name[i-1]);
strcpy(name[i-1], name[i]);
strcpy(name[i], tempName);
unsigned int tempScore;
tempScore = highscore[i-1];
highscore[i-1] = highscore[i];
highscore[i] = tempScore;
}
else{
break;
}
}
for(byte thisScore = 0; thisScore < NUM_HIGHSCORE; thisScore++){
gb.save.set(2*thisScore, highscore[thisScore]);
gb.save.set(2*thisScore + 1, name[thisScore]);
}
}
}
drawHighScores();
}
void drawHighScores(){
while(gamestatus!="title"){
if(gb.update()){
gb.display.clear();
// gb.display.setFont(font5x7);
if (gamestatus=="gameover") {
gb.display.setColor(RED);
gb.display.cursorX=20;
gb.display.cursorY=3;
gb.display.print("GAME OVER");
}
gb.display.setColor(BLUE);
gb.display.cursorX = 15+random(0,2);
gb.display.cursorY = 20+random(0,2);
gb.display.println("BEST SCORES");
gb.display.textWrap = false;
gb.display.cursorX = 0;
gb.display.cursorY = gb.display.fontHeight*2 + 20;
for(byte thisScore=0; thisScore<NUM_HIGHSCORE; thisScore++){
// gb.display.setColor(int(255 - (12*thisScore)), int(20*thisScore), int(255 - (15 *thisScore)) );
gb.display.setColor(YELLOW);
if(highscore[thisScore]==0)
gb.display.print('-');
else {
gb.display.print(name[thisScore]);
gb.display.cursorX = gb.display.width()-4*gb.display.fontWidth;
gb.display.cursorY = gb.display.fontHeight*2 + gb.display.fontHeight*thisScore + 20;
gb.display.println(highscore[thisScore]);
}
if(gb.buttons.pressed(BUTTON_A) || gb.buttons.pressed(BUTTON_B) || gb.buttons.pressed(BUTTON_MENU)){
gb.sound.playOK();
gamestatus="title";
break;
}
}
}
}
}
boolean drawNewHighscore(unsigned int newscore){
//gb.sound.playPattern(highscore_sound, 0);
gb.sound.playOK();
while(1){
if(gb.update()){
gb.display.clear();
if (gamestatus=="gameover") {
gb.display.setColor(RED);
gb.display.cursorX=20;
gb.display.cursorY=3;
gb.display.print("GAME OVER");
}
gb.display.setColor(BLUE);
gb.display.cursorX = 10+random(0,2);
gb.display.cursorY = 15+random(0,2);
gb.display.print("NEW HIGHSCORE");
gb.display.setColor(YELLOW);
gb.display.cursorX = 10;
gb.display.cursorY = 32;
gb.display.print("You ");
gb.display.print(newscore);
gb.display.cursorX = 10;
gb.display.cursorY = 38;
gb.display.print("Best ");
gb.display.print(highscore[0]);
gb.display.cursorX = 10;
gb.display.cursorY = 44;
gb.display.print("Worst ");
gb.display.print(highscore[NUM_HIGHSCORE-1]);
gb.display.setColor(BLUE);
gb.display.cursorX = 10;
gb.display.cursorY = 56;
gb.display.print("\25:Save \26:Exit");
if(gb.buttons.pressed(BUTTON_A)){
gb.sound.playOK();
gamestatus="title";
return true;
}
if(gb.buttons.pressed(BUTTON_B)){
gb.sound.playCancel();
gamestatus="title";
return false;
}
}
}
}