Use albumart opt in sliding puzzle manual
[kugel-rb.git] / apps / plugins / snake.c
blob1d14efea69f880e42e2f54edfb9956b83a96a147
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Itai Shaked
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 Snake!
25 by Itai Shaked
27 ok, a little explanation -
28 board holds the snake and apple position - 1+ - snake body (the number
29 represents the age [1 is the snake's head]).
30 -1 is an apple, and 0 is a clear spot.
31 dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
35 #include "plugin.h"
36 #ifdef HAVE_LCD_BITMAP
37 #include "lib/configfile.h"
38 #include "lib/highscore.h"
39 #include "lib/playback_control.h"
41 PLUGIN_HEADER
43 /* variable button definitions */
44 #if CONFIG_KEYPAD == RECORDER_PAD
45 #define SNAKE_QUIT BUTTON_OFF
46 #define SNAKE_LEFT BUTTON_LEFT
47 #define SNAKE_RIGHT BUTTON_RIGHT
48 #define SNAKE_UP BUTTON_UP
49 #define SNAKE_DOWN BUTTON_DOWN
50 #define SNAKE_PLAYPAUSE BUTTON_PLAY
52 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
53 #define SNAKE_QUIT BUTTON_OFF
54 #define SNAKE_LEFT BUTTON_LEFT
55 #define SNAKE_RIGHT BUTTON_RIGHT
56 #define SNAKE_UP BUTTON_UP
57 #define SNAKE_DOWN BUTTON_DOWN
58 #define SNAKE_PLAYPAUSE BUTTON_SELECT
60 #elif CONFIG_KEYPAD == ONDIO_PAD
61 #define SNAKE_QUIT BUTTON_OFF
62 #define SNAKE_LEFT BUTTON_LEFT
63 #define SNAKE_RIGHT BUTTON_RIGHT
64 #define SNAKE_UP BUTTON_UP
65 #define SNAKE_DOWN BUTTON_DOWN
66 #define SNAKE_PLAYPAUSE BUTTON_MENU
68 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
69 (CONFIG_KEYPAD == IRIVER_H300_PAD)
70 #define SNAKE_QUIT BUTTON_OFF
71 #define SNAKE_LEFT BUTTON_LEFT
72 #define SNAKE_RIGHT BUTTON_RIGHT
73 #define SNAKE_UP BUTTON_UP
74 #define SNAKE_DOWN BUTTON_DOWN
75 #define SNAKE_PLAYPAUSE BUTTON_ON
77 #define SNAKE_RC_QUIT BUTTON_RC_STOP
79 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
80 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
81 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
82 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
83 #define SNAKE_LEFT BUTTON_LEFT
84 #define SNAKE_RIGHT BUTTON_RIGHT
85 #define SNAKE_UP BUTTON_MENU
86 #define SNAKE_DOWN BUTTON_PLAY
87 #define SNAKE_PLAYPAUSE BUTTON_SELECT
89 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
90 #define SNAKE_QUIT BUTTON_POWER
91 #define SNAKE_LEFT BUTTON_LEFT
92 #define SNAKE_RIGHT BUTTON_RIGHT
93 #define SNAKE_UP BUTTON_UP
94 #define SNAKE_DOWN BUTTON_DOWN
95 #define SNAKE_PLAYPAUSE BUTTON_PLAY
97 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
98 #define SNAKE_QUIT BUTTON_POWER
99 #define SNAKE_LEFT BUTTON_LEFT
100 #define SNAKE_RIGHT BUTTON_RIGHT
101 #define SNAKE_UP BUTTON_UP
102 #define SNAKE_DOWN BUTTON_DOWN
103 #define SNAKE_PLAYPAUSE BUTTON_SELECT
105 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
106 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
107 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
108 (CONFIG_KEYPAD == SANSA_M200_PAD)
109 #define SNAKE_QUIT BUTTON_POWER
110 #define SNAKE_LEFT BUTTON_LEFT
111 #define SNAKE_RIGHT BUTTON_RIGHT
112 #define SNAKE_UP BUTTON_UP
113 #define SNAKE_DOWN BUTTON_DOWN
114 #define SNAKE_PLAYPAUSE BUTTON_SELECT
116 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
117 #define SNAKE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
118 #define SNAKE_LEFT BUTTON_LEFT
119 #define SNAKE_RIGHT BUTTON_RIGHT
120 #define SNAKE_UP BUTTON_UP
121 #define SNAKE_DOWN BUTTON_DOWN
122 #define SNAKE_PLAYPAUSE BUTTON_SELECT
124 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
125 #define SNAKE_QUIT BUTTON_POWER
126 #define SNAKE_LEFT BUTTON_LEFT
127 #define SNAKE_RIGHT BUTTON_RIGHT
128 #define SNAKE_UP BUTTON_SCROLL_UP
129 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
130 #define SNAKE_PLAYPAUSE BUTTON_PLAY
132 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
133 #define SNAKE_QUIT BUTTON_BACK
134 #define SNAKE_LEFT BUTTON_LEFT
135 #define SNAKE_RIGHT BUTTON_RIGHT
136 #define SNAKE_UP BUTTON_UP
137 #define SNAKE_DOWN BUTTON_DOWN
138 #define SNAKE_PLAYPAUSE BUTTON_SELECT
140 #elif (CONFIG_KEYPAD == MROBE100_PAD)
141 #define SNAKE_QUIT BUTTON_POWER
142 #define SNAKE_LEFT BUTTON_LEFT
143 #define SNAKE_RIGHT BUTTON_RIGHT
144 #define SNAKE_UP BUTTON_UP
145 #define SNAKE_DOWN BUTTON_DOWN
146 #define SNAKE_PLAYPAUSE BUTTON_SELECT
148 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
149 #define SNAKE_QUIT BUTTON_RC_REC
150 #define SNAKE_LEFT BUTTON_RC_REW
151 #define SNAKE_RIGHT BUTTON_RC_FF
152 #define SNAKE_UP BUTTON_RC_VOL_UP
153 #define SNAKE_DOWN BUTTON_RC_VOL_DOWN
154 #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY
156 #define SNAKE_RC_QUIT BUTTON_REC
158 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
159 #define SNAKE_QUIT BUTTON_BACK
160 #define SNAKE_LEFT BUTTON_LEFT
161 #define SNAKE_RIGHT BUTTON_RIGHT
162 #define SNAKE_UP BUTTON_UP
163 #define SNAKE_DOWN BUTTON_DOWN
164 #define SNAKE_PLAYPAUSE BUTTON_PLAY
166 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
167 #define SNAKE_QUIT BUTTON_POWER
168 #define SNAKE_LEFT BUTTON_LEFT
169 #define SNAKE_RIGHT BUTTON_RIGHT
170 #define SNAKE_UP BUTTON_UP
171 #define SNAKE_DOWN BUTTON_DOWN
172 #define SNAKE_PLAYPAUSE BUTTON_MENU
174 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
175 #define SNAKE_QUIT BUTTON_POWER
176 #define SNAKE_LEFT BUTTON_PREV
177 #define SNAKE_RIGHT BUTTON_NEXT
178 #define SNAKE_UP BUTTON_UP
179 #define SNAKE_DOWN BUTTON_DOWN
180 #define SNAKE_PLAYPAUSE BUTTON_MENU
182 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
183 #define SNAKE_QUIT BUTTON_REC
184 #define SNAKE_LEFT BUTTON_LEFT
185 #define SNAKE_RIGHT BUTTON_RIGHT
186 #define SNAKE_UP BUTTON_UP
187 #define SNAKE_DOWN BUTTON_DOWN
188 #define SNAKE_PLAYPAUSE BUTTON_PLAY
190 #elif CONFIG_KEYPAD == MROBE500_PAD
191 #define SNAKE_QUIT BUTTON_POWER
192 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
194 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
195 #define SNAKE_QUIT BUTTON_POWER
197 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
198 #define SNAKE_QUIT BUTTON_POWER
200 #elif CONFIG_KEYPAD == COWON_D2_PAD
201 #define SNAKE_QUIT BUTTON_POWER
203 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
204 #define SNAKE_QUIT BUTTON_REC
205 #define SNAKE_LEFT BUTTON_PREV
206 #define SNAKE_RIGHT BUTTON_NEXT
207 #define SNAKE_UP BUTTON_UP
208 #define SNAKE_DOWN BUTTON_DOWN
209 #define SNAKE_PLAYPAUSE BUTTON_PLAY
211 #else
212 #error No keymap defined!
213 #endif
215 #ifdef HAVE_TOUCHSCREEN
216 #ifndef SNAKE_QUIT
217 #define SNAKE_QUIT BUTTON_TOPLEFT
218 #endif
219 #ifndef SNAKE_LEFT
220 #define SNAKE_LEFT BUTTON_MIDLEFT
221 #endif
222 #ifndef SNAKE_RIGHT
223 #define SNAKE_RIGHT BUTTON_MIDRIGHT
224 #endif
225 #ifndef SNAKE_UP
226 #define SNAKE_UP BUTTON_TOPMIDDLE
227 #endif
228 #ifndef SNAKE_DOWN
229 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
230 #endif
231 #ifndef SNAKE_PLAYPAUSE
232 #define SNAKE_PLAYPAUSE BUTTON_CENTER
233 #endif
234 #endif
236 #define BOARD_WIDTH (LCD_WIDTH/4)
237 #define BOARD_HEIGHT (LCD_HEIGHT/4)
239 #define CONFIG_FILE_NAME "snake.cfg"
240 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
241 #define NUM_SCORES 5
243 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
244 static unsigned int score, level = 1;
245 static int dir;
246 static bool apple, dead, ingame;
247 static struct highscore highscores[NUM_SCORES];
249 static struct configdata config[] = {
250 {TYPE_INT, 1, 9, { .int_p = &level }, "level", NULL},
253 static void snake_die (void)
255 char pscore[17];
256 rb->lcd_clear_display();
257 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
258 rb->lcd_puts(0,0,"Oops...");
259 rb->lcd_puts(0,1, pscore);
260 if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) {
261 rb->lcd_puts(0,2,"New High Score!");
263 else {
264 rb->snprintf(pscore, sizeof(pscore),
265 "High Score: %d", highscores[0].score);
266 rb->lcd_puts(0,2,pscore);
268 rb->lcd_update();
269 rb->sleep(3*HZ);
270 dead=true;
273 static void snake_colission (short x, short y)
275 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) {
276 snake_die();
277 return;
279 switch (board[x][y]) {
280 case 0:
281 break;
282 case -1:
283 snakelength+=2;
284 score+=level;
285 apple=false;
286 break;
287 default:
288 snake_die();
289 break;
293 static void snake_move_head (short x, short y)
295 switch (dir) {
296 case 0:
297 y-=1;
298 break;
299 case 1:
300 x+=1;
301 break;
302 case 2:
303 y+=1;
304 break;
305 case 3:
306 x-=1;
307 break;
309 snake_colission (x,y);
310 if (dead)
311 return;
312 board[x][y]=1;
313 rb->lcd_fillrect(x*4,y*4,4,4);
316 static void snake_redraw (void)
318 short x,y;
319 rb->lcd_clear_display();
320 for (x=0; x<BOARD_WIDTH; x++) {
321 for (y=0; y<BOARD_HEIGHT; y++) {
322 switch (board[x][y]) {
323 case -1:
324 rb->lcd_fillrect((x*4)+1,y*4,2,4);
325 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
326 break;
327 case 0:
328 break;
329 default:
330 rb->lcd_fillrect(x*4,y*4,4,4);
331 break;
335 rb->lcd_update();
338 static void snake_frame (void)
340 short x,y,head=0;
341 for (x=0; x<BOARD_WIDTH; x++) {
342 for (y=0; y<BOARD_HEIGHT; y++) {
343 switch (board[x][y]) {
344 case 1:
345 if (!head) {
346 snake_move_head(x,y);
347 if (dead)
348 return;
349 board[x][y]++;
350 head=1;
352 break;
353 case 0:
354 break;
355 case -1:
356 break;
357 default:
358 if (board[x][y]==snakelength) {
359 board[x][y]=0;
360 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
361 rb->lcd_fillrect(x*4,y*4,4,4);
362 rb->lcd_set_drawmode(DRMODE_SOLID);
364 else
365 board[x][y]++;
366 break;
370 rb->lcd_update();
373 static void snake_game_init(void) {
374 short x,y;
375 rb->lcd_clear_display();
377 for (x=0; x<BOARD_WIDTH; x++) {
378 for (y=0; y<BOARD_HEIGHT; y++) {
379 board[x][y]=0;
382 apple=false;
383 dead=false;
384 snakelength=4;
385 score=0;
386 dir=1;
387 board[11][7]=1;
390 static int snake_menu_cb(int action, const struct menu_item_ex *this_item)
392 if(action == ACTION_REQUEST_MENUITEM
393 && !ingame && ((intptr_t)this_item)==0)
394 return ACTION_EXIT_MENUITEM;
395 return action;
398 static int snake_game_menu(void)
400 MENUITEM_STRINGLIST(main_menu,"Snake Menu",snake_menu_cb,
401 "Resume Game",
402 "Start New Game",
403 "Snake Speed",
404 "High Score",
405 "Playback Control",
406 "Quit");
407 int selected = 0;
409 rb->button_clear_queue();
411 while (true) {
412 switch (rb->do_menu(&main_menu, &selected, NULL, false)) {
413 case 0:
414 snake_redraw();
415 return 0;
416 case 1:
417 snake_game_init();
418 return 0;
419 case 2:
420 rb->set_int("Snake Speed", "", UNIT_INT, &level,
421 NULL, 1, 1, 9, NULL);
422 break;
423 case 3:
424 highscore_show(-1, highscores, NUM_SCORES, true);
425 rb->lcd_setfont(FONT_UI);
426 break;
427 case 4:
428 playback_control(NULL);
429 break;
430 case 5:
431 return 1;
432 case MENU_ATTACHED_USB:
433 return 1;
434 default:
435 break;
440 static int snake_game_loop (void) {
441 int button;
442 short x,y;
443 bool pause = false;
445 if (snake_game_menu())
446 return 1;
448 ingame = true;
449 while (true) {
450 if (!pause) {
451 snake_frame();
452 if (dead) {
453 ingame = false;
454 return 0;
456 if (!apple) {
457 do {
458 x=rb->rand() % BOARD_WIDTH;
459 y=rb->rand() % BOARD_HEIGHT;
460 } while (board[x][y]);
461 apple=true;
462 board[x][y]=-1;
463 rb->lcd_fillrect((x*4)+1,y*4,2,4);
464 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
467 rb->sleep(HZ/level);
470 button=rb->button_get(false);
472 #ifdef HAS_BUTTON_HOLD
473 if (rb->button_hold() && !pause)
474 button = SNAKE_PLAYPAUSE;
475 #endif
476 switch (button) {
477 case SNAKE_UP:
478 if (dir!=2) dir=0;
479 break;
480 case SNAKE_RIGHT:
481 if (dir!=3) dir=1;
482 break;
483 case SNAKE_DOWN:
484 if (dir!=0) dir=2;
485 break;
486 case SNAKE_LEFT:
487 if (dir!=1) dir=3;
488 break;
489 case SNAKE_PLAYPAUSE:
490 pause = !pause;
491 if (pause)
492 rb->splash (HZ, "Paused");
493 else
494 snake_redraw();
495 break;
496 #ifdef SNAKE_RC_QUIT
497 case SNAKE_RC_QUIT:
498 #endif
499 case SNAKE_QUIT:
500 return 0;
501 break;
502 default:
503 if (rb->default_event_handler (button) == SYS_USB_CONNECTED)
504 return 1;
505 break;
510 enum plugin_status plugin_start(const void* parameter)
512 (void)parameter;
514 configfile_load(CONFIG_FILE_NAME, config, 1, 0);
515 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
516 rb->lcd_clear_display();
517 ingame = false;
518 while(snake_game_loop() == 0)
520 configfile_save(CONFIG_FILE_NAME, config, 1, 0);
521 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
522 return PLUGIN_OK;
524 #endif