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/playback_control.h"
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 == CREATIVEZVM_PAD
158 #define SNAKE_QUIT BUTTON_BACK
159 #define SNAKE_LEFT BUTTON_LEFT
160 #define SNAKE_RIGHT BUTTON_RIGHT
161 #define SNAKE_UP BUTTON_UP
162 #define SNAKE_DOWN BUTTON_DOWN
163 #define SNAKE_PLAYPAUSE BUTTON_PLAY
165 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
166 #define SNAKE_QUIT BUTTON_POWER
167 #define SNAKE_LEFT BUTTON_LEFT
168 #define SNAKE_RIGHT BUTTON_RIGHT
169 #define SNAKE_UP BUTTON_UP
170 #define SNAKE_DOWN BUTTON_DOWN
171 #define SNAKE_PLAYPAUSE BUTTON_MENU
173 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
174 #define SNAKE_QUIT BUTTON_REC
175 #define SNAKE_LEFT BUTTON_LEFT
176 #define SNAKE_RIGHT BUTTON_RIGHT
177 #define SNAKE_UP BUTTON_UP
178 #define SNAKE_DOWN BUTTON_DOWN
179 #define SNAKE_PLAYPAUSE BUTTON_PLAY
181 #elif CONFIG_KEYPAD == MROBE500_PAD
182 #define SNAKE_QUIT BUTTON_POWER
183 #define SNAKE_RC_QUIT BUTTON_RC_DOWN
185 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
186 #define SNAKE_QUIT BUTTON_POWER
188 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
189 #define SNAKE_QUIT BUTTON_POWER
191 #elif CONFIG_KEYPAD == COWOND2_PAD
192 #define SNAKE_QUIT BUTTON_POWER
195 #error No keymap defined!
198 #ifdef HAVE_TOUCHSCREEN
200 #define SNAKE_QUIT BUTTON_TOPLEFT
203 #define SNAKE_LEFT BUTTON_MIDLEFT
206 #define SNAKE_RIGHT BUTTON_MIDRIGHT
209 #define SNAKE_UP BUTTON_TOPMIDDLE
212 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
214 #ifndef SNAKE_PLAYPAUSE
215 #define SNAKE_PLAYPAUSE BUTTON_CENTER
219 #define BOARD_WIDTH (LCD_WIDTH/4)
220 #define BOARD_HEIGHT (LCD_HEIGHT/4)
222 static int board
[BOARD_WIDTH
][BOARD_HEIGHT
],snakelength
;
223 static unsigned int score
,hiscore
=0,level
=1;
225 static bool apple
, dead
;
227 #define CONFIG_FILE_NAME "snake.cfg"
228 static struct configdata config
[] = {
229 {TYPE_INT
, 0, 10, { .int_p
= &level
}, "level", NULL
},
230 {TYPE_INT
, 0, 10000, { .int_p
= &hiscore
}, "hiscore", NULL
},
233 static void snake_die (void)
236 rb
->lcd_clear_display();
237 rb
->snprintf(pscore
,sizeof(pscore
),"Your score: %d",score
);
238 rb
->lcd_puts(0,0,"Oops...");
239 rb
->lcd_puts(0,1, pscore
);
242 rb
->lcd_puts(0,2,"New High Score!");
245 rb
->snprintf(pscore
,sizeof(pscore
),"High Score: %d",hiscore
);
246 rb
->lcd_puts(0,2,pscore
);
253 static void snake_colission (short x
, short y
)
255 if (x
==BOARD_WIDTH
|| x
<0 || y
==BOARD_HEIGHT
|| y
<0) {
259 switch (board
[x
][y
]) {
273 static void snake_move_head (short x
, short y
)
289 snake_colission (x
,y
);
293 rb
->lcd_fillrect(x
*4,y
*4,4,4);
296 static void snake_redraw (void)
299 rb
->lcd_clear_display();
300 for (x
=0; x
<BOARD_WIDTH
; x
++) {
301 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
302 switch (board
[x
][y
]) {
304 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
305 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
310 rb
->lcd_fillrect(x
*4,y
*4,4,4);
318 static void snake_frame (void)
321 for (x
=0; x
<BOARD_WIDTH
; x
++) {
322 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
323 switch (board
[x
][y
]) {
326 snake_move_head(x
,y
);
338 if (board
[x
][y
]==snakelength
) {
340 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
341 rb
->lcd_fillrect(x
*4,y
*4,4,4);
342 rb
->lcd_set_drawmode(DRMODE_SOLID
);
353 static void snake_game_init(void) {
355 rb
->lcd_clear_display();
357 for (x
=0; x
<BOARD_WIDTH
; x
++) {
358 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
369 static void snake_choose_level(void)
371 rb
->set_int("Snake Speed", "", UNIT_INT
, &level
, NULL
, 1, 1, 9, NULL
);
375 static int snake_menu_cb(int action
, const struct menu_item_ex
*this_item
)
377 if(action
== ACTION_REQUEST_MENUITEM
378 && !_ingame
&& ((intptr_t)this_item
)==0)
379 return ACTION_EXIT_MENUITEM
;
383 static int snake_game_menu(bool ingame
)
385 rb
->button_clear_queue();
390 MENUITEM_STRINGLIST(main_menu
,"Snake Menu",snake_menu_cb
,
399 choice
= rb
->do_menu(&main_menu
, &choice
, NULL
, false);
408 snake_choose_level();
411 rb
->splashf(HZ
*2, "High Score: %d", hiscore
);
414 playback_control(NULL
);
418 case MENU_ATTACHED_USB
:
426 static int snake_game_loop (void) {
431 if (snake_game_menu(false)==1)
438 if (snake_game_menu(false)==1)
443 x
=rb
->rand() % BOARD_WIDTH
;
444 y
=rb
->rand() % BOARD_HEIGHT
;
445 } while (board
[x
][y
]);
448 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
449 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
455 button
=rb
->button_get(false);
457 #ifdef HAS_BUTTON_HOLD
458 if (rb
->button_hold()) {
460 rb
->splash (HZ
, "Paused");
476 case SNAKE_PLAYPAUSE
:
479 rb
->splash (HZ
, "Paused");
488 if (snake_game_menu(true)==1)
492 if (rb
->default_event_handler (button
) == SYS_USB_CONNECTED
)
493 return PLUGIN_USB_CONNECTED
;
499 enum plugin_status
plugin_start(const void* parameter
)
503 configfile_load(CONFIG_FILE_NAME
,config
,2,0);
504 rb
->lcd_clear_display();
506 configfile_save(CONFIG_FILE_NAME
,config
,2,0);