diff options
Diffstat (limited to 'apps/plugins/snake.c')
-rw-r--r-- | apps/plugins/snake.c | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/apps/plugins/snake.c b/apps/plugins/snake.c index 8cba920840..9491d56014 100644 --- a/apps/plugins/snake.c +++ b/apps/plugins/snake.c | |||
@@ -34,6 +34,7 @@ dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left; | |||
34 | 34 | ||
35 | #include "plugin.h" | 35 | #include "plugin.h" |
36 | #ifdef HAVE_LCD_BITMAP | 36 | #ifdef HAVE_LCD_BITMAP |
37 | #include "lib/highscore.h" | ||
37 | #include "lib/playback_control.h" | 38 | #include "lib/playback_control.h" |
38 | 39 | ||
39 | PLUGIN_HEADER | 40 | PLUGIN_HEADER |
@@ -214,12 +215,16 @@ PLUGIN_HEADER | |||
214 | 215 | ||
215 | #define BOARD_WIDTH (LCD_WIDTH/4) | 216 | #define BOARD_WIDTH (LCD_WIDTH/4) |
216 | #define BOARD_HEIGHT (LCD_HEIGHT/4) | 217 | #define BOARD_HEIGHT (LCD_HEIGHT/4) |
218 | #define NUM_SCORES 5 | ||
219 | #define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score" | ||
217 | 220 | ||
218 | static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength; | 221 | static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength; |
219 | static unsigned int score,hiscore=0,level=1; | 222 | static int score,level=1; |
220 | static int dir,dead=0; | 223 | static int dir,dead=0; |
221 | static bool apple; | 224 | static bool apple; |
222 | 225 | ||
226 | static struct highscore highscores[NUM_SCORES]; | ||
227 | |||
223 | void die (void) | 228 | void die (void) |
224 | { | 229 | { |
225 | char pscore[17]; | 230 | char pscore[17]; |
@@ -227,12 +232,12 @@ void die (void) | |||
227 | rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score); | 232 | rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score); |
228 | rb->lcd_puts(0,0,"Oops..."); | 233 | rb->lcd_puts(0,0,"Oops..."); |
229 | rb->lcd_puts(0,1, pscore); | 234 | rb->lcd_puts(0,1, pscore); |
230 | if (score>hiscore) { | 235 | if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) { |
231 | hiscore=score; | ||
232 | rb->lcd_puts(0,2,"New High Score!"); | 236 | rb->lcd_puts(0,2,"New High Score!"); |
233 | } | 237 | } |
234 | else { | 238 | else { |
235 | rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore); | 239 | rb->snprintf(pscore, sizeof(pscore), |
240 | "High Score: %d", highscores[0].score); | ||
236 | rb->lcd_puts(0,2,pscore); | 241 | rb->lcd_puts(0,2,pscore); |
237 | } | 242 | } |
238 | rb->lcd_update(); | 243 | rb->lcd_update(); |
@@ -244,7 +249,7 @@ void colission (short x, short y) | |||
244 | { | 249 | { |
245 | switch (board[x][y]) { | 250 | switch (board[x][y]) { |
246 | case 0: | 251 | case 0: |
247 | break; | 252 | break; |
248 | case -1: | 253 | case -1: |
249 | snakelength+=2; | 254 | snakelength+=2; |
250 | score+=level; | 255 | score+=level; |
@@ -254,7 +259,7 @@ void colission (short x, short y) | |||
254 | die(); | 259 | die(); |
255 | break; | 260 | break; |
256 | } | 261 | } |
257 | if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) | 262 | if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) |
258 | die(); | 263 | die(); |
259 | } | 264 | } |
260 | 265 | ||
@@ -265,7 +270,7 @@ void move_head (short x, short y) | |||
265 | y-=1; | 270 | y-=1; |
266 | break; | 271 | break; |
267 | case 1: | 272 | case 1: |
268 | x+=1; | 273 | x+=1; |
269 | break; | 274 | break; |
270 | case 2: | 275 | case 2: |
271 | y+=1; | 276 | y+=1; |
@@ -307,7 +312,7 @@ void frame (void) | |||
307 | rb->lcd_fillrect(x*4,y*4,4,4); | 312 | rb->lcd_fillrect(x*4,y*4,4,4); |
308 | rb->lcd_set_drawmode(DRMODE_SOLID); | 313 | rb->lcd_set_drawmode(DRMODE_SOLID); |
309 | } | 314 | } |
310 | else | 315 | else |
311 | board[x][y]++; | 316 | board[x][y]++; |
312 | break; | 317 | break; |
313 | } | 318 | } |
@@ -450,8 +455,11 @@ void game_init(void) { | |||
450 | 455 | ||
451 | MENUITEM_STRINGLIST(menu, "Snake Menu", NULL, | 456 | MENUITEM_STRINGLIST(menu, "Snake Menu", NULL, |
452 | "Start New Game", "Starting Level", | 457 | "Start New Game", "Starting Level", |
458 | "High Scores", | ||
453 | "Playback Control", "Quit"); | 459 | "Playback Control", "Quit"); |
454 | 460 | ||
461 | rb->button_clear_queue(); | ||
462 | |||
455 | while (!menu_quit) { | 463 | while (!menu_quit) { |
456 | switch(rb->do_menu(&menu, &selection, NULL, false)) | 464 | switch(rb->do_menu(&menu, &selection, NULL, false)) |
457 | { | 465 | { |
@@ -465,9 +473,18 @@ void game_init(void) { | |||
465 | break; | 473 | break; |
466 | 474 | ||
467 | case 2: | 475 | case 2: |
476 | highscore_show(NUM_SCORES, highscores, NUM_SCORES, true); | ||
477 | break; | ||
478 | |||
479 | case 3: | ||
468 | playback_control(NULL); | 480 | playback_control(NULL); |
469 | break; | 481 | break; |
470 | 482 | ||
483 | case MENU_ATTACHED_USB: | ||
484 | dead = 2; | ||
485 | menu_quit = true; | ||
486 | break; | ||
487 | |||
471 | default: | 488 | default: |
472 | dead=1; /* quit program */ | 489 | dead=1; /* quit program */ |
473 | menu_quit = true; | 490 | menu_quit = true; |
@@ -481,9 +498,16 @@ enum plugin_status plugin_start(const void* parameter) | |||
481 | { | 498 | { |
482 | (void)(parameter); | 499 | (void)(parameter); |
483 | 500 | ||
484 | game_init(); | 501 | highscore_load(SCORE_FILE, highscores, NUM_SCORES); |
485 | rb->lcd_clear_display(); | 502 | while(dead == 0) |
486 | game(); | 503 | { |
504 | game_init(); | ||
505 | if(dead) | ||
506 | break; | ||
507 | rb->lcd_clear_display(); | ||
508 | game(); | ||
509 | } | ||
510 | highscore_save(SCORE_FILE, highscores, NUM_SCORES); | ||
487 | return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED; | 511 | return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED; |
488 | } | 512 | } |
489 | 513 | ||