fix red.
[kugel-rb/myfork.git] / apps / plugins / snake.c
blobe2e34e3f06e22867b858888c8e7a8f7b529bc014
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/highscore.h"
38 #include "lib/playback_control.h"
40 PLUGIN_HEADER
42 /* variable button definitions */
43 #if CONFIG_KEYPAD == RECORDER_PAD
44 #define SNAKE_QUIT BUTTON_OFF
45 #define SNAKE_LEFT BUTTON_LEFT
46 #define SNAKE_RIGHT BUTTON_RIGHT
47 #define SNAKE_UP BUTTON_UP
48 #define SNAKE_DOWN BUTTON_DOWN
49 #define SNAKE_PLAYPAUSE BUTTON_PLAY
51 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
52 #define SNAKE_QUIT BUTTON_OFF
53 #define SNAKE_LEFT BUTTON_LEFT
54 #define SNAKE_RIGHT BUTTON_RIGHT
55 #define SNAKE_UP BUTTON_UP
56 #define SNAKE_DOWN BUTTON_DOWN
57 #define SNAKE_PLAYPAUSE BUTTON_SELECT
59 #elif CONFIG_KEYPAD == ONDIO_PAD
60 #define SNAKE_QUIT BUTTON_OFF
61 #define SNAKE_LEFT BUTTON_LEFT
62 #define SNAKE_RIGHT BUTTON_RIGHT
63 #define SNAKE_UP BUTTON_UP
64 #define SNAKE_DOWN BUTTON_DOWN
65 #define SNAKE_PLAYPAUSE BUTTON_MENU
67 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
68 (CONFIG_KEYPAD == IRIVER_H300_PAD)
69 #define SNAKE_QUIT BUTTON_OFF
70 #define SNAKE_LEFT BUTTON_LEFT
71 #define SNAKE_RIGHT BUTTON_RIGHT
72 #define SNAKE_UP BUTTON_UP
73 #define SNAKE_DOWN BUTTON_DOWN
74 #define SNAKE_PLAYPAUSE BUTTON_ON
76 #define SNAKE_RC_QUIT BUTTON_RC_STOP
78 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
79 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
80 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
81 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
82 #define SNAKE_LEFT BUTTON_LEFT
83 #define SNAKE_RIGHT BUTTON_RIGHT
84 #define SNAKE_UP BUTTON_MENU
85 #define SNAKE_DOWN BUTTON_PLAY
86 #define SNAKE_PLAYPAUSE BUTTON_SELECT
88 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
89 #define SNAKE_QUIT BUTTON_POWER
90 #define SNAKE_LEFT BUTTON_LEFT
91 #define SNAKE_RIGHT BUTTON_RIGHT
92 #define SNAKE_UP BUTTON_UP
93 #define SNAKE_DOWN BUTTON_DOWN
94 #define SNAKE_PLAYPAUSE BUTTON_PLAY
96 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
97 #define SNAKE_QUIT BUTTON_POWER
98 #define SNAKE_LEFT BUTTON_LEFT
99 #define SNAKE_RIGHT BUTTON_RIGHT
100 #define SNAKE_UP BUTTON_UP
101 #define SNAKE_DOWN BUTTON_DOWN
102 #define SNAKE_PLAYPAUSE BUTTON_SELECT
104 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
105 (CONFIG_KEYPAD == SANSA_C200_PAD) || \
106 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
107 (CONFIG_KEYPAD == SANSA_M200_PAD)
108 #define SNAKE_QUIT BUTTON_POWER
109 #define SNAKE_LEFT BUTTON_LEFT
110 #define SNAKE_RIGHT BUTTON_RIGHT
111 #define SNAKE_UP BUTTON_UP
112 #define SNAKE_DOWN BUTTON_DOWN
113 #define SNAKE_PLAYPAUSE BUTTON_SELECT
115 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
116 #define SNAKE_QUIT (BUTTON_HOME|BUTTON_REPEAT)
117 #define SNAKE_LEFT BUTTON_LEFT
118 #define SNAKE_RIGHT BUTTON_RIGHT
119 #define SNAKE_UP BUTTON_UP
120 #define SNAKE_DOWN BUTTON_DOWN
121 #define SNAKE_PLAYPAUSE BUTTON_SELECT
123 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
124 #define SNAKE_QUIT BUTTON_POWER
125 #define SNAKE_LEFT BUTTON_LEFT
126 #define SNAKE_RIGHT BUTTON_RIGHT
127 #define SNAKE_UP BUTTON_SCROLL_UP
128 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
129 #define SNAKE_PLAYPAUSE BUTTON_PLAY
131 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
132 #define SNAKE_QUIT BUTTON_BACK
133 #define SNAKE_LEFT BUTTON_LEFT
134 #define SNAKE_RIGHT BUTTON_RIGHT
135 #define SNAKE_UP BUTTON_UP
136 #define SNAKE_DOWN BUTTON_DOWN
137 #define SNAKE_PLAYPAUSE BUTTON_SELECT
139 #elif (CONFIG_KEYPAD == MROBE100_PAD)
140 #define SNAKE_QUIT BUTTON_POWER
141 #define SNAKE_LEFT BUTTON_LEFT
142 #define SNAKE_RIGHT BUTTON_RIGHT
143 #define SNAKE_UP BUTTON_UP
144 #define SNAKE_DOWN BUTTON_DOWN
145 #define SNAKE_PLAYPAUSE BUTTON_SELECT
147 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
148 #define SNAKE_QUIT BUTTON_RC_REC
149 #define SNAKE_LEFT BUTTON_RC_REW
150 #define SNAKE_RIGHT BUTTON_RC_FF
151 #define SNAKE_UP BUTTON_RC_VOL_UP
152 #define SNAKE_DOWN BUTTON_RC_VOL_DOWN
153 #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY
155 #define SNAKE_RC_QUIT BUTTON_REC
157 #elif (CONFIG_KEYPAD == COWOND2_PAD)
158 #define SNAKE_QUIT BUTTON_POWER
160 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
161 #define SNAKE_QUIT BUTTON_BACK
162 #define SNAKE_LEFT BUTTON_LEFT
163 #define SNAKE_RIGHT BUTTON_RIGHT
164 #define SNAKE_UP BUTTON_UP
165 #define SNAKE_DOWN BUTTON_DOWN
166 #define SNAKE_PLAYPAUSE BUTTON_PLAY
168 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
169 #define SNAKE_QUIT BUTTON_POWER
170 #define SNAKE_LEFT BUTTON_LEFT
171 #define SNAKE_RIGHT BUTTON_RIGHT
172 #define SNAKE_UP BUTTON_UP
173 #define SNAKE_DOWN BUTTON_DOWN
174 #define SNAKE_PLAYPAUSE BUTTON_MENU
176 #elif CONFIG_KEYPAD == MROBE500_PAD
177 #define SNAKE_QUIT BUTTON_POWER
178 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
180 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
181 #define SNAKE_QUIT BUTTON_POWER
183 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
184 #define SNAKE_QUIT BUTTON_REC
185 #define SNAKE_LEFT BUTTON_LEFT
186 #define SNAKE_RIGHT BUTTON_RIGHT
187 #define SNAKE_UP BUTTON_UP
188 #define SNAKE_DOWN BUTTON_DOWN
189 #define SNAKE_PLAYPAUSE BUTTON_PLAY
191 #else
192 #error No keymap defined!
193 #endif
195 #ifdef HAVE_TOUCHSCREEN
196 #ifndef SNAKE_QUIT
197 #define SNAKE_QUIT BUTTON_TOPLEFT
198 #endif
199 #ifndef SNAKE_LEFT
200 #define SNAKE_LEFT BUTTON_MIDLEFT
201 #endif
202 #ifndef SNAKE_RIGHT
203 #define SNAKE_RIGHT BUTTON_MIDRIGHT
204 #endif
205 #ifndef SNAKE_UP
206 #define SNAKE_UP BUTTON_TOPMIDDLE
207 #endif
208 #ifndef SNAKE_DOWN
209 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
210 #endif
211 #ifndef SNAKE_PLAYPAUSE
212 #define SNAKE_PLAYPAUSE BUTTON_CENTER
213 #endif
214 #endif
216 #define BOARD_WIDTH (LCD_WIDTH/4)
217 #define BOARD_HEIGHT (LCD_HEIGHT/4)
218 #define NUM_SCORES 5
219 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
221 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
222 static int score,level=1;
223 static int dir,dead=0,quit=0;
224 static bool apple;
226 static struct highscore highscores[NUM_SCORES];
228 void die (void)
230 char pscore[17];
231 rb->lcd_clear_display();
232 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
233 rb->lcd_puts(0,0,"Oops...");
234 rb->lcd_puts(0,1, pscore);
235 if (highscore_update(score, level, "", highscores, NUM_SCORES) == 0) {
236 rb->lcd_puts(0,2,"New High Score!");
238 else {
239 rb->snprintf(pscore, sizeof(pscore),
240 "High Score: %d", highscores[0].score);
241 rb->lcd_puts(0,2,pscore);
243 rb->lcd_update();
244 rb->sleep(3*HZ);
245 dead=1;
248 void colission (short x, short y)
250 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0)
252 die();
253 return;
256 switch (board[x][y]) {
257 case 0:
258 break;
259 case -1:
260 snakelength+=2;
261 score+=level;
262 apple=false;
263 break;
264 default:
265 die();
266 break;
270 void move_head (short x, short y)
272 switch (dir) {
273 case 0:
274 y-=1;
275 break;
276 case 1:
277 x+=1;
278 break;
279 case 2:
280 y+=1;
281 break;
282 case 3:
283 x-=1;
284 break;
286 colission (x,y);
287 if (dead)
288 return;
289 board[x][y]=1;
290 rb->lcd_fillrect(x*4,y*4,4,4);
293 void frame (void)
295 short x,y,head=0;
296 for (x=0; x<BOARD_WIDTH; x++) {
297 for (y=0; y<BOARD_HEIGHT; y++) {
298 switch (board[x][y]) {
299 case 1:
300 if (!head) {
301 move_head(x,y);
302 if (dead)
303 return;
304 board[x][y]++;
305 head=1;
307 break;
308 case 0:
309 break;
310 case -1:
311 break;
312 default:
313 if (board[x][y]==snakelength) {
314 board[x][y]=0;
315 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
316 rb->lcd_fillrect(x*4,y*4,4,4);
317 rb->lcd_set_drawmode(DRMODE_SOLID);
319 else
320 board[x][y]++;
321 break;
325 rb->lcd_update();
328 void redraw (void)
330 short x,y;
331 rb->lcd_clear_display();
332 for (x=0; x<BOARD_WIDTH; x++) {
333 for (y=0; y<BOARD_HEIGHT; y++) {
334 switch (board[x][y]) {
335 case -1:
336 rb->lcd_fillrect((x*4)+1,y*4,2,4);
337 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
338 break;
339 case 0:
340 break;
341 default:
342 rb->lcd_fillrect(x*4,y*4,4,4);
343 break;
347 rb->lcd_update();
350 void game_pause (void) {
351 int button;
352 rb->lcd_clear_display();
353 rb->lcd_putsxy(3,12,"Game Paused");
354 #if CONFIG_KEYPAD == RECORDER_PAD
355 rb->lcd_putsxy(3,22,"[Play] to resume");
356 #elif CONFIG_KEYPAD == ONDIO_PAD
357 rb->lcd_putsxy(3,22,"[Mode] to resume");
358 #endif
359 rb->lcd_putsxy(3,32,"[Off] to quit");
360 rb->lcd_update();
361 while (1) {
362 button=rb->button_get(true);
363 switch (button) {
364 #ifdef SNAKE_RC_QUIT
365 case SNAKE_RC_QUIT:
366 #endif
367 case SNAKE_QUIT:
368 dead=1;
369 quit=1;
370 return;
371 case SNAKE_PLAYPAUSE:
372 redraw();
373 rb->sleep(HZ/2);
374 return;
375 default:
376 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
377 dead = 1;
378 quit = 2;
379 return;
381 break;
387 void game (void) {
388 int button;
389 short x,y;
390 while (1) {
391 frame();
392 if (dead)
393 return;
394 if (!apple) {
395 do {
396 x=rb->rand() % BOARD_WIDTH;
397 y=rb->rand() % BOARD_HEIGHT;
398 } while (board[x][y]);
399 apple=true;
400 board[x][y]=-1;
401 rb->lcd_fillrect((x*4)+1,y*4,2,4);
402 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
405 rb->sleep(HZ/level);
407 button=rb->button_get(false);
409 #ifdef HAS_BUTTON_HOLD
410 if (rb->button_hold())
411 button = SNAKE_PLAYPAUSE;
412 #endif
414 switch (button) {
415 case SNAKE_UP:
416 if (dir!=2) dir=0;
417 break;
418 case SNAKE_RIGHT:
419 if (dir!=3) dir=1;
420 break;
421 case SNAKE_DOWN:
422 if (dir!=0) dir=2;
423 break;
424 case SNAKE_LEFT:
425 if (dir!=1) dir=3;
426 break;
427 #ifdef SNAKE_RC_QUIT
428 case SNAKE_RC_QUIT:
429 #endif
430 case SNAKE_QUIT:
431 quit = 1;
432 return;
433 case SNAKE_PLAYPAUSE:
434 game_pause();
435 break;
436 default:
437 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
438 quit = 2;
439 return;
441 break;
446 void game_init(void) {
447 int selection=0;
448 short x,y;
449 bool menu_quit = false;
451 for (x=0; x<BOARD_WIDTH; x++) {
452 for (y=0; y<BOARD_HEIGHT; y++) {
453 board[x][y]=0;
456 dead=0;
457 apple=false;
458 snakelength=4;
459 score=0;
460 board[11][7]=1;
462 #if LCD_DEPTH > 1
463 fb_data *backdrop = rb->lcd_get_backdrop();
464 #endif
466 MENUITEM_STRINGLIST(menu, "Snake Menu", NULL,
467 "Start New Game", "Starting Level",
468 "High Scores",
469 "Playback Control", "Quit");
471 rb->button_clear_queue();
473 while (!menu_quit) {
474 switch(rb->do_menu(&menu, &selection, NULL, false))
476 case 0:
477 menu_quit = true; /* start playing */
478 break;
480 case 1:
481 rb->set_int("Starting Level", "", UNIT_INT, &level, NULL,
482 1, 1, 9, NULL );
483 break;
485 case 2:
486 #if LCD_DEPTH > 1
487 rb->lcd_set_backdrop(NULL);
488 #endif
489 highscore_show(NUM_SCORES, highscores, NUM_SCORES, true);
491 rb->lcd_setfont(FONT_UI);
492 #if LCD_DEPTH > 1
493 rb->lcd_set_backdrop(backdrop);
494 #ifdef HAVE_LCD_COLOR
495 rb->lcd_set_background(rb->global_settings->bg_color);
496 rb->lcd_set_foreground(rb->global_settings->fg_color);
497 #endif
498 #endif
499 break;
501 case 3:
502 playback_control(NULL);
503 break;
505 case MENU_ATTACHED_USB:
506 quit = 2;
507 menu_quit = true;
508 break;
510 default:
511 quit = 1; /* quit program */
512 menu_quit = true;
513 break;
519 enum plugin_status plugin_start(const void* parameter)
521 (void)(parameter);
523 highscore_load(SCORE_FILE, highscores, NUM_SCORES);
524 while(!quit)
526 game_init();
527 if(quit)
528 break;
529 rb->lcd_clear_display();
530 game();
532 highscore_save(SCORE_FILE, highscores, NUM_SCORES);
533 return (quit==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED;
536 #endif