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
212 #error No keymap defined!
215 #ifdef HAVE_TOUCHSCREEN
217 #define SNAKE_QUIT BUTTON_TOPLEFT
220 #define SNAKE_LEFT BUTTON_MIDLEFT
223 #define SNAKE_RIGHT BUTTON_MIDRIGHT
226 #define SNAKE_UP BUTTON_TOPMIDDLE
229 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
231 #ifndef SNAKE_PLAYPAUSE
232 #define SNAKE_PLAYPAUSE BUTTON_CENTER
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"
243 static int board
[BOARD_WIDTH
][BOARD_HEIGHT
],snakelength
;
244 static unsigned int score
, level
= 1;
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)
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!");
264 rb
->snprintf(pscore
, sizeof(pscore
),
265 "High Score: %d", highscores
[0].score
);
266 rb
->lcd_puts(0,2,pscore
);
273 static void snake_colission (short x
, short y
)
275 if (x
==BOARD_WIDTH
|| x
<0 || y
==BOARD_HEIGHT
|| y
<0) {
279 switch (board
[x
][y
]) {
293 static void snake_move_head (short x
, short y
)
309 snake_colission (x
,y
);
313 rb
->lcd_fillrect(x
*4,y
*4,4,4);
316 static void snake_redraw (void)
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
]) {
324 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
325 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
330 rb
->lcd_fillrect(x
*4,y
*4,4,4);
338 static void snake_frame (void)
341 for (x
=0; x
<BOARD_WIDTH
; x
++) {
342 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
343 switch (board
[x
][y
]) {
346 snake_move_head(x
,y
);
358 if (board
[x
][y
]==snakelength
) {
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
);
373 static void snake_game_init(void) {
375 rb
->lcd_clear_display();
377 for (x
=0; x
<BOARD_WIDTH
; x
++) {
378 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
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
;
398 static int snake_game_menu(void)
400 MENUITEM_STRINGLIST(main_menu
,"Snake Menu",snake_menu_cb
,
409 rb
->button_clear_queue();
412 switch (rb
->do_menu(&main_menu
, &selected
, NULL
, false)) {
420 rb
->set_int("Snake Speed", "", UNIT_INT
, &level
,
421 NULL
, 1, 1, 9, NULL
);
424 highscore_show(-1, highscores
, NUM_SCORES
, true);
425 rb
->lcd_setfont(FONT_UI
);
428 playback_control(NULL
);
432 case MENU_ATTACHED_USB
:
440 static int snake_game_loop (void) {
445 if (snake_game_menu())
458 x
=rb
->rand() % BOARD_WIDTH
;
459 y
=rb
->rand() % BOARD_HEIGHT
;
460 } while (board
[x
][y
]);
463 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
464 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
470 button
=rb
->button_get(false);
472 #ifdef HAS_BUTTON_HOLD
473 if (rb
->button_hold() && !pause
)
474 button
= SNAKE_PLAYPAUSE
;
489 case SNAKE_PLAYPAUSE
:
492 rb
->splash (HZ
, "Paused");
503 if (rb
->default_event_handler (button
) == SYS_USB_CONNECTED
)
510 enum plugin_status
plugin_start(const void* parameter
)
514 configfile_load(CONFIG_FILE_NAME
, config
, 1, 0);
515 highscore_load(SCORE_FILE
, highscores
, NUM_SCORES
);
516 rb
->lcd_clear_display();
518 while(snake_game_loop() == 0)
520 configfile_save(CONFIG_FILE_NAME
, config
, 1, 0);
521 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);