1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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;
36 #ifdef HAVE_LCD_BITMAP
37 #include "lib/configfile.h"
38 #include "lib/highscore.h"
39 #include "lib/playback_control.h"
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 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
212 #define SNAKE_QUIT (BUTTON_REC|BUTTON_PLAY)
213 #define SNAKE_LEFT BUTTON_VOL_DOWN
214 #define SNAKE_RIGHT BUTTON_VOL_UP
215 #define SNAKE_UP BUTTON_PREV
216 #define SNAKE_DOWN BUTTON_NEXT
217 #define SNAKE_PLAYPAUSE BUTTON_PLAY
220 #error No keymap defined!
223 #ifdef HAVE_TOUCHSCREEN
225 #define SNAKE_QUIT BUTTON_TOPLEFT
228 #define SNAKE_LEFT BUTTON_MIDLEFT
231 #define SNAKE_RIGHT BUTTON_MIDRIGHT
234 #define SNAKE_UP BUTTON_TOPMIDDLE
237 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
239 #ifndef SNAKE_PLAYPAUSE
240 #define SNAKE_PLAYPAUSE BUTTON_CENTER
244 #define BOARD_WIDTH (LCD_WIDTH/4)
245 #define BOARD_HEIGHT (LCD_HEIGHT/4)
247 #define CONFIG_FILE_NAME "snake.cfg"
248 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake.score"
251 static int board
[BOARD_WIDTH
][BOARD_HEIGHT
],snakelength
;
252 static unsigned int score
, level
= 1;
254 static bool apple
, dead
, ingame
;
255 static struct highscore highscores
[NUM_SCORES
];
257 static struct configdata config
[] = {
258 {TYPE_INT
, 1, 9, { .int_p
= &level
}, "level", NULL
},
261 static void snake_die (void)
264 rb
->lcd_clear_display();
265 rb
->snprintf(pscore
,sizeof(pscore
),"Your score: %d",score
);
266 rb
->lcd_puts(0,0,"Oops...");
267 rb
->lcd_puts(0,1, pscore
);
268 if (highscore_update(score
, level
, "", highscores
, NUM_SCORES
) == 0) {
269 rb
->lcd_puts(0,2,"New High Score!");
272 rb
->snprintf(pscore
, sizeof(pscore
),
273 "High Score: %d", highscores
[0].score
);
274 rb
->lcd_puts(0,2,pscore
);
281 static void snake_colission (short x
, short y
)
283 if (x
==BOARD_WIDTH
|| x
<0 || y
==BOARD_HEIGHT
|| y
<0) {
287 switch (board
[x
][y
]) {
301 static void snake_move_head (short x
, short y
)
317 snake_colission (x
,y
);
321 rb
->lcd_fillrect(x
*4,y
*4,4,4);
324 static void snake_redraw (void)
327 rb
->lcd_clear_display();
328 for (x
=0; x
<BOARD_WIDTH
; x
++) {
329 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
330 switch (board
[x
][y
]) {
332 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
333 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
338 rb
->lcd_fillrect(x
*4,y
*4,4,4);
346 static void snake_frame (void)
349 for (x
=0; x
<BOARD_WIDTH
; x
++) {
350 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
351 switch (board
[x
][y
]) {
354 snake_move_head(x
,y
);
366 if (board
[x
][y
]==snakelength
) {
368 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
369 rb
->lcd_fillrect(x
*4,y
*4,4,4);
370 rb
->lcd_set_drawmode(DRMODE_SOLID
);
381 static void snake_game_init(void) {
383 rb
->lcd_clear_display();
385 for (x
=0; x
<BOARD_WIDTH
; x
++) {
386 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
398 static int snake_menu_cb(int action
, const struct menu_item_ex
*this_item
)
400 if(action
== ACTION_REQUEST_MENUITEM
401 && !ingame
&& ((intptr_t)this_item
)==0)
402 return ACTION_EXIT_MENUITEM
;
406 static int snake_game_menu(void)
408 MENUITEM_STRINGLIST(main_menu
,"Snake Menu",snake_menu_cb
,
417 rb
->button_clear_queue();
420 switch (rb
->do_menu(&main_menu
, &selected
, NULL
, false)) {
428 rb
->set_int("Snake Speed", "", UNIT_INT
, &level
,
429 NULL
, 1, 1, 9, NULL
);
432 highscore_show(-1, highscores
, NUM_SCORES
, true);
433 rb
->lcd_setfont(FONT_UI
);
436 playback_control(NULL
);
440 case MENU_ATTACHED_USB
:
448 static int snake_game_loop (void) {
453 if (snake_game_menu())
466 x
=rb
->rand() % BOARD_WIDTH
;
467 y
=rb
->rand() % BOARD_HEIGHT
;
468 } while (board
[x
][y
]);
471 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
472 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
478 button
=rb
->button_get(false);
480 #ifdef HAS_BUTTON_HOLD
481 if (rb
->button_hold() && !pause
)
482 button
= SNAKE_PLAYPAUSE
;
497 case SNAKE_PLAYPAUSE
:
500 rb
->splash (HZ
, "Paused");
511 if (rb
->default_event_handler (button
) == SYS_USB_CONNECTED
)
518 enum plugin_status
plugin_start(const void* parameter
)
522 configfile_load(CONFIG_FILE_NAME
, config
, 1, 0);
523 highscore_load(SCORE_FILE
, highscores
, NUM_SCORES
);
524 rb
->lcd_clear_display();
526 while(snake_game_loop() == 0)
528 configfile_save(CONFIG_FILE_NAME
, config
, 1, 0);
529 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);