1
0
Fork 0
forked from len0rd/rockbox

snake: use lib/highscore for highscore handling.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@24964 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Teruaki Kawashima 2010-02-28 11:27:05 +00:00
parent 0978b427ee
commit d4ebc9268e

View file

@ -35,6 +35,7 @@ dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
#include "plugin.h" #include "plugin.h"
#ifdef HAVE_LCD_BITMAP #ifdef HAVE_LCD_BITMAP
#include "lib/configfile.h" #include "lib/configfile.h"
#include "lib/highscore.h"
#include "lib/playback_control.h" #include "lib/playback_control.h"
PLUGIN_HEADER PLUGIN_HEADER
@ -235,15 +236,18 @@ PLUGIN_HEADER
#define BOARD_WIDTH (LCD_WIDTH/4) #define BOARD_WIDTH (LCD_WIDTH/4)
#define BOARD_HEIGHT (LCD_HEIGHT/4) #define BOARD_HEIGHT (LCD_HEIGHT/4)
static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
static unsigned int score,hiscore=0,level=1;
static int dir;
static bool apple, dead;
#define CONFIG_FILE_NAME "snake.cfg" #define CONFIG_FILE_NAME "snake.cfg"
#define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
#define NUM_SCORES 5
static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
static unsigned int score, level = 1;
static int dir;
static bool apple, dead, ingame;
static struct highscore highscores[NUM_SCORES];
static struct configdata config[] = { static struct configdata config[] = {
{TYPE_INT, 0, 10, { .int_p = &level }, "level", NULL}, {TYPE_INT, 1, 9, { .int_p = &level }, "level", NULL},
{TYPE_INT, 0, 10000, { .int_p = &hiscore }, "hiscore", NULL},
}; };
static void snake_die (void) static void snake_die (void)
@ -253,12 +257,12 @@ static void snake_die (void)
rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score); rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
rb->lcd_puts(0,0,"Oops..."); rb->lcd_puts(0,0,"Oops...");
rb->lcd_puts(0,1, pscore); rb->lcd_puts(0,1, pscore);
if (score>hiscore) { if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) {
hiscore=score;
rb->lcd_puts(0,2,"New High Score!"); rb->lcd_puts(0,2,"New High Score!");
} }
else { else {
rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore); rb->snprintf(pscore, sizeof(pscore),
"High Score: %d", highscores[0].score);
rb->lcd_puts(0,2,pscore); rb->lcd_puts(0,2,pscore);
} }
rb->lcd_update(); rb->lcd_update();
@ -379,30 +383,20 @@ static void snake_game_init(void) {
dead=false; dead=false;
snakelength=4; snakelength=4;
score=0; score=0;
dir=1;
board[11][7]=1; board[11][7]=1;
} }
static void snake_choose_level(void)
{
rb->set_int("Snake Speed", "", UNIT_INT, &level, NULL, 1, 1, 9, NULL);
}
static bool _ingame;
static int snake_menu_cb(int action, const struct menu_item_ex *this_item) static int snake_menu_cb(int action, const struct menu_item_ex *this_item)
{ {
if(action == ACTION_REQUEST_MENUITEM if(action == ACTION_REQUEST_MENUITEM
&& !_ingame && ((intptr_t)this_item)==0) && !ingame && ((intptr_t)this_item)==0)
return ACTION_EXIT_MENUITEM; return ACTION_EXIT_MENUITEM;
return action; return action;
} }
static int snake_game_menu(bool ingame) static int snake_game_menu(void)
{ {
rb->button_clear_queue();
int choice = 0;
_ingame = ingame;
MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb, MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb,
"Resume Game", "Resume Game",
"Start New Game", "Start New Game",
@ -410,10 +404,12 @@ static int snake_game_menu(bool ingame)
"High Score", "High Score",
"Playback Control", "Playback Control",
"Quit"); "Quit");
int selected = 0;
rb->button_clear_queue();
while (true) { while (true) {
choice = rb->do_menu(&main_menu, &choice, NULL, false); switch (rb->do_menu(&main_menu, &selected, NULL, false)) {
switch (choice) {
case 0: case 0:
snake_redraw(); snake_redraw();
return 0; return 0;
@ -421,10 +417,12 @@ static int snake_game_menu(bool ingame)
snake_game_init(); snake_game_init();
return 0; return 0;
case 2: case 2:
snake_choose_level(); rb->set_int("Snake Speed", "", UNIT_INT, &level,
NULL, 1, 1, 9, NULL);
break; break;
case 3: case 3:
rb->splashf(HZ*2, "High Score: %d", hiscore); highscore_show(-1, highscores, NUM_SCORES, true);
rb->lcd_setfont(FONT_UI);
break; break;
case 4: case 4:
playback_control(NULL); playback_control(NULL);
@ -444,15 +442,16 @@ static int snake_game_loop (void) {
short x,y; short x,y;
bool pause = false; bool pause = false;
if (snake_game_menu(false)==1) if (snake_game_menu())
return 1; return 1;
ingame = true;
while (true) { while (true) {
if (!pause) { if (!pause) {
snake_frame(); snake_frame();
if (dead) { if (dead) {
if (snake_game_menu(false)==1) ingame = false;
return 1; return 0;
} }
if (!apple) { if (!apple) {
do { do {
@ -471,10 +470,8 @@ static int snake_game_loop (void) {
button=rb->button_get(false); button=rb->button_get(false);
#ifdef HAS_BUTTON_HOLD #ifdef HAS_BUTTON_HOLD
if (rb->button_hold()) { if (rb->button_hold() && !pause)
pause = true; button = SNAKE_PLAYPAUSE;
rb->splash (HZ, "Paused");
}
#endif #endif
switch (button) { switch (button) {
case SNAKE_UP: case SNAKE_UP:
@ -500,13 +497,11 @@ static int snake_game_loop (void) {
case SNAKE_RC_QUIT: case SNAKE_RC_QUIT:
#endif #endif
case SNAKE_QUIT: case SNAKE_QUIT:
pause = false; return 0;
if (snake_game_menu(true)==1)
return 1;
break; break;
default: default:
if (rb->default_event_handler (button) == SYS_USB_CONNECTED) if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED; return 1;
break; break;
} }
} }
@ -516,10 +511,14 @@ enum plugin_status plugin_start(const void* parameter)
{ {
(void)parameter; (void)parameter;
configfile_load(CONFIG_FILE_NAME,config,2,0); configfile_load(CONFIG_FILE_NAME, config, 1, 0);
highscore_load(SCORE_FILE, highscores, NUM_SCORES);
rb->lcd_clear_display(); rb->lcd_clear_display();
snake_game_loop(); ingame = false;
configfile_save(CONFIG_FILE_NAME,config,2,0); while(snake_game_loop() == 0)
;
configfile_save(CONFIG_FILE_NAME, config, 1, 0);
highscore_save(SCORE_FILE, highscores, NUM_SCORES);
return PLUGIN_OK; return PLUGIN_OK;
} }
#endif #endif