-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_interlude.c
221 lines (187 loc) · 7.91 KB
/
game_interlude.c
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
/*
* Module to print game interlude (leaderboard and adding people)
* Author: Aditi ([email protected])
*
* user abilities:
* - store and show leaderboard
* - to add yourself to leaderboard, select 2-letter initials (using button presses)
* - play game again
*/
#include "console.h"
#include "game_interlude.h"
#include "malloc.h"
#include "remote.h"
#include "LSD6DS33.h"
#include "timer.h"
#include "printf.h"
static interlude_contents_t contents ;
#define BLINK_DELAY 100
#define FRANCIS_DEMO 1 // if this == 1, then francis goes to the top of the score chart :)
/* 'game_interlude_init'
* initializes game screen to nrows and ncols wide, where every row/col size (in pixels) is determined by the character size
* takes colors text and bg for the text and background colors respectively
*/
void game_interlude_init(int nrows, int ncols, color_t text, color_t bg) {
// console info
console_init(nrows, ncols, text, bg);
contents._ncols = ncols ;
contents._nrows = nrows ;
contents._leaderboard = malloc(LEADERBOARD_SIZE*sizeof(leaderboard_character_t)) ; // 3 chars per each leaderboard thing
for(int i = 0; i < LEADERBOARD_SIZE; i++) {
contents._leaderboard[i]._initials[0] = '*' ;
contents._leaderboard[i]._initials[1] = '*' ;
contents._leaderboard[i]._initials[2] = '\0' ;
contents._leaderboard[i]._score = 0 ;
}
if (FRANCIS_DEMO == 1) { // just for fun: francis (FR) gets top score :)
contents._leaderboard[0]._initials[0] = 'F' ;
contents._leaderboard[0]._initials[1] = 'R' ;
contents._leaderboard[0]._initials[2] = '\0' ;
contents._leaderboard[0]._score = 99999 ;
}
}
// display instructions
static void game_interlude_operations(void) {
console_clear() ;
console_printf("\nLEADERBOARD!\n\n Down:\n Set / Next\n \n Button:\n Change\n\n\n") ;
int pitch = 0; int roll = 0 ;
remote_get_x_y_status(&pitch, &roll) ;
timer_delay(2) ;
while (pitch != X_FAST) { remote_get_x_y_status(&pitch, &roll) ; } // wait for tilt
}
/* game_interlude_print_leaderboard
* @param score from most recent game
* @param num rows cleared from most recent game
* @functionality prints useful info about the most recent game to screen
* @exit tilt remote down
*/
static void game_interlude_display_game_stats(unsigned int score, unsigned int lines_cleared) {
console_clear() ;
console_printf("\n score:\n %d\n lines cleared:\n %d", score, lines_cleared) ;
int pitch = 0; int roll = 0 ;
remote_get_x_y_status(&pitch, &roll) ;
timer_delay(2) ;
while (pitch != X_FAST) { remote_get_x_y_status(&pitch, &roll) ; } // wait for tilt
}
/* game_interlude_get_user_initials
* @functionality uses a simple state machine to prompt and read 2 letters in a user's initials
* @returns a char* of the user's initials
* how to use: Click button to iterate thru characters. Tilt remote down to confirm character. Characters loop from A-Z
*/
static char* game_interlude_get_user_initials(void) {
char *initials = malloc(3) ;
initials[0] = '\0' ;
initials[1] = '\0' ;
initials[2] = '\0' ;
// ask for input (user initials) from user
// (flickering effect)
for (int i = 0; i < 5; i++) {
console_clear() ;
console_printf("Your Initials:\n **\n\n(Click Button)") ;
timer_delay_ms(BLINK_DELAY) ;
console_clear() ;
console_printf("Your Initials:\n *\n\n(Click Button)") ;
timer_delay_ms(BLINK_DELAY) ;
}
console_clear() ;
console_printf("Your Initials:\n **\n\n(Click Button)") ;
// gather 1st initial
int first_letter = 25 ; // Z
int pitch = 0; int roll = 0 ;
remote_get_x_y_status(&pitch, &roll) ;
while (pitch != X_FAST) {
if(remote_is_button_press()) {
first_letter ++ ;
console_clear() ;
console_printf("Your Initials:\n %c* \n\ntilt down \n to continue", ('A'+first_letter%26)) ;
}
remote_get_x_y_status(&pitch, &roll) ;
}
// (flickering effect)
for (int i = 0; i < 5; i++) {
console_clear() ;
console_printf("Your Initials:\n %c*", ('A'+first_letter%26)) ;
timer_delay_ms(BLINK_DELAY) ;
console_clear() ;
console_printf("Your Initials:\n %c ", ('A'+first_letter%26)) ;
timer_delay_ms(BLINK_DELAY) ;
}
console_clear() ;
console_printf("Your Initials:\n %c*", ('A'+first_letter%26)) ;
// gather 2nd initial
int second_letter = 25 ; // Z
remote_get_x_y_status(&pitch, &roll) ;
while (pitch != X_FAST) {
if(remote_is_button_press()) {
second_letter ++ ;
console_clear() ;
console_printf("Your Initials:\n %c%c", ('A'+first_letter%26), ('A'+second_letter%26)) ;
}
remote_get_x_y_status(&pitch, &roll) ;
}
timer_delay(1) ;
// set it into user's initials to return
initials[0] = ('A'+first_letter%26) ;
initials[1] = ('A'+second_letter%26) ;
return initials ;
}
/* game_interlude_update_leaderboard
* @param takes a score and adds it to the leaderboard if the score is high enough
*/
static void game_interlude_update_leaderboard(unsigned int score) {
if (score >= contents._leaderboard[LEADERBOARD_SIZE-1]._score) { // if the score is high enough for leaderboard
char* initials = game_interlude_get_user_initials() ;
for(int i = LEADERBOARD_SIZE-1; i >= 0; i--) {
if (score < contents._leaderboard[i]._score || i == 0) { // most recent tie will be at the top
if(i==0 && (score >= contents._leaderboard[0]._score)) {i=-1;} // in case we have a new high-score
// shift the scores down
for (int j = LEADERBOARD_SIZE-1; j > i+1; j--) {
contents._leaderboard[j]._initials[0] = contents._leaderboard[j-1]._initials[0] ;
contents._leaderboard[j]._initials[1] = contents._leaderboard[j-1]._initials[1] ;
contents._leaderboard[j]._initials[2] = '\0' ; // just bc; why not! :)
contents._leaderboard[j]._score = contents._leaderboard[j-1]._score;
}
// replace cur score with the person
contents._leaderboard[i+1]._initials[0] = initials[0] ;
contents._leaderboard[i+1]._initials[1] = initials[1] ;
contents._leaderboard[i+1]._initials[2] = '\0' ; // just bc; why not! :)
contents._leaderboard[i+1]._score = score ;
free(initials) ;
break ;
}
}
}
}
/* game_interlude_print_leaderboard
* @param score from most recent game
* @param num rows cleared from most recent game
* @functionality updates leaderboard with most recent score (prompts player for initials if necessary) and prints leaderboard to screen
* @exit tilt remote down
*/
void game_interlude_print_leaderboard(unsigned int score, unsigned int lines_cleared) {
// pre-leaderboard stuff
game_interlude_operations() ;
game_interlude_display_game_stats(score, lines_cleared) ; // tell the player how they did!
// now, we get to the leaderboard
game_interlude_update_leaderboard(score) ; // need to update leaderboard first! (if worthy player)
console_clear() ;
console_printf("*LEADERBOARD*\n") ;
console_printf("<#>\t\t\b<n>\tSCORE\n") ;
for (int i = 0; i < LEADERBOARD_SIZE; i++) {
console_printf(" %d \t\t\b%s \t%d\n", i, contents._leaderboard[i]._initials, contents._leaderboard[i]._score) ;
}
timer_delay(1) ;
console_printf("\n\nTilt down to \n play again!") ;
int pitch = 0; int roll = 0 ;
remote_get_x_y_status(&pitch, &roll) ;
while (pitch != X_FAST) {remote_get_x_y_status(&pitch, &roll) ;}
console_clear() ;
}
// returns num rows in console
int game_interlude_get_rows(void) {
return contents._nrows ;
}
// returns num cols in console
int game_interlude_get_cols(void) {
return contents._ncols ;
}