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/highscore.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 == 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
192 #error No keymap defined!
195 #ifdef HAVE_TOUCHSCREEN
197 #define SNAKE_QUIT BUTTON_TOPLEFT
200 #define SNAKE_LEFT BUTTON_MIDLEFT
203 #define SNAKE_RIGHT BUTTON_MIDRIGHT
206 #define SNAKE_UP BUTTON_TOPMIDDLE
209 #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE
211 #ifndef SNAKE_PLAYPAUSE
212 #define SNAKE_PLAYPAUSE BUTTON_CENTER
216 #define BOARD_WIDTH (LCD_WIDTH/4)
217 #define BOARD_HEIGHT (LCD_HEIGHT/4)
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;
226 static struct highscore highscores
[NUM_SCORES
];
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!");
239 rb
->snprintf(pscore
, sizeof(pscore
),
240 "High Score: %d", highscores
[0].score
);
241 rb
->lcd_puts(0,2,pscore
);
248 void colission (short x
, short y
)
250 if (x
==BOARD_WIDTH
|| x
<0 || y
==BOARD_HEIGHT
|| y
<0)
256 switch (board
[x
][y
]) {
270 void move_head (short x
, short y
)
290 rb
->lcd_fillrect(x
*4,y
*4,4,4);
296 for (x
=0; x
<BOARD_WIDTH
; x
++) {
297 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
298 switch (board
[x
][y
]) {
313 if (board
[x
][y
]==snakelength
) {
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
);
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
]) {
336 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
337 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
342 rb
->lcd_fillrect(x
*4,y
*4,4,4);
350 void game_pause (void) {
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");
359 rb
->lcd_putsxy(3,32,"[Off] to quit");
362 button
=rb
->button_get(true);
371 case SNAKE_PLAYPAUSE
:
376 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
) {
396 x
=rb
->rand() % BOARD_WIDTH
;
397 y
=rb
->rand() % BOARD_HEIGHT
;
398 } while (board
[x
][y
]);
401 rb
->lcd_fillrect((x
*4)+1,y
*4,2,4);
402 rb
->lcd_fillrect(x
*4,(y
*4)+1,4,2);
407 button
=rb
->button_get(false);
409 #ifdef HAS_BUTTON_HOLD
410 if (rb
->button_hold())
411 button
= SNAKE_PLAYPAUSE
;
433 case SNAKE_PLAYPAUSE
:
437 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
) {
446 void game_init(void) {
449 bool menu_quit
= false;
451 for (x
=0; x
<BOARD_WIDTH
; x
++) {
452 for (y
=0; y
<BOARD_HEIGHT
; y
++) {
463 fb_data
*backdrop
= rb
->lcd_get_backdrop();
466 MENUITEM_STRINGLIST(menu
, "Snake Menu", NULL
,
467 "Start New Game", "Starting Level",
469 "Playback Control", "Quit");
471 rb
->button_clear_queue();
474 switch(rb
->do_menu(&menu
, &selection
, NULL
, false))
477 menu_quit
= true; /* start playing */
481 rb
->set_int("Starting Level", "", UNIT_INT
, &level
, NULL
,
487 rb
->lcd_set_backdrop(NULL
);
489 highscore_show(NUM_SCORES
, highscores
, NUM_SCORES
, true);
491 rb
->lcd_setfont(FONT_UI
);
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
);
502 playback_control(NULL
);
505 case MENU_ATTACHED_USB
:
511 quit
= 1; /* quit program */
519 enum plugin_status
plugin_start(const void* parameter
)
523 highscore_load(SCORE_FILE
, highscores
, NUM_SCORES
);
529 rb
->lcd_clear_display();
532 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);
533 return (quit
==1)?PLUGIN_OK
:PLUGIN_USB_CONNECTED
;