when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / plugins / snake.c
blobc68b924da676e9d709a53e28d93ac53327bd3e0f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Itai Shaked
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 Snake!
23 by Itai Shaked
25 ok, a little explanation -
26 board holds the snake and apple position - 1+ - snake body (the number
27 represents the age [1 is the snake's head]).
28 -1 is an apple, and 0 is a clear spot.
29 dir is the current direction of the snake - 0=up, 1=right, 2=down, 3=left;
33 #include "plugin.h"
34 #ifdef HAVE_LCD_BITMAP
36 PLUGIN_HEADER
38 /* variable button definitions */
39 #if CONFIG_KEYPAD == RECORDER_PAD
40 #define SNAKE_QUIT BUTTON_OFF
41 #define SNAKE_UP BUTTON_UP
42 #define SNAKE_DOWN BUTTON_DOWN
43 #define SNAKE_PLAYPAUSE BUTTON_PLAY
45 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
46 #define SNAKE_QUIT BUTTON_OFF
47 #define SNAKE_UP BUTTON_UP
48 #define SNAKE_DOWN BUTTON_DOWN
49 #define SNAKE_PLAYPAUSE BUTTON_SELECT
51 #elif CONFIG_KEYPAD == ONDIO_PAD
52 #define SNAKE_QUIT BUTTON_OFF
53 #define SNAKE_UP BUTTON_UP
54 #define SNAKE_DOWN BUTTON_DOWN
55 #define SNAKE_PLAYPAUSE BUTTON_MENU
57 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
58 (CONFIG_KEYPAD == IRIVER_H300_PAD)
59 #define SNAKE_QUIT BUTTON_OFF
60 #define SNAKE_UP BUTTON_UP
61 #define SNAKE_DOWN BUTTON_DOWN
62 #define SNAKE_PLAYPAUSE BUTTON_ON
64 #define SNAKE_RC_QUIT BUTTON_RC_STOP
66 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
67 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
68 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
69 #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU)
70 #define SNAKE_UP BUTTON_MENU
71 #define SNAKE_DOWN BUTTON_PLAY
72 #define SNAKE_PLAYPAUSE BUTTON_SELECT
74 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
75 #define SNAKE_QUIT BUTTON_POWER
76 #define SNAKE_UP BUTTON_UP
77 #define SNAKE_DOWN BUTTON_DOWN
78 #define SNAKE_PLAYPAUSE BUTTON_PLAY
80 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
81 #define SNAKE_QUIT BUTTON_POWER
82 #define SNAKE_UP BUTTON_UP
83 #define SNAKE_DOWN BUTTON_DOWN
84 #define SNAKE_PLAYPAUSE BUTTON_SELECT
86 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
87 (CONFIG_KEYPAD == SANSA_C200_PAD)
88 #define SNAKE_QUIT BUTTON_POWER
89 #define SNAKE_UP BUTTON_UP
90 #define SNAKE_DOWN BUTTON_DOWN
91 #define SNAKE_PLAYPAUSE BUTTON_SELECT
93 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
94 #define SNAKE_QUIT BUTTON_POWER
95 #define SNAKE_UP BUTTON_SCROLL_UP
96 #define SNAKE_DOWN BUTTON_SCROLL_DOWN
97 #define SNAKE_PLAYPAUSE BUTTON_PLAY
99 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
100 #define SNAKE_QUIT BUTTON_BACK
101 #define SNAKE_UP BUTTON_UP
102 #define SNAKE_DOWN BUTTON_DOWN
103 #define SNAKE_PLAYPAUSE BUTTON_SELECT
105 #elif (CONFIG_KEYPAD == MROBE100_PAD)
106 #define SNAKE_QUIT BUTTON_POWER
107 #define SNAKE_UP BUTTON_UP
108 #define SNAKE_DOWN BUTTON_DOWN
109 #define SNAKE_PLAYPAUSE BUTTON_SELECT
111 #else
112 #error No keymap defined!
113 #endif
115 #define BOARD_WIDTH (LCD_WIDTH/4)
116 #define BOARD_HEIGHT (LCD_HEIGHT/4)
118 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
119 static unsigned int score,hiscore=0,level=1;
120 static short dir,frames,apple,dead=0;
121 static struct plugin_api* rb;
123 void die (void)
125 char pscore[17];
126 rb->lcd_clear_display();
127 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
128 rb->lcd_puts(0,0,"Oops...");
129 rb->lcd_puts(0,1, pscore);
130 if (score>hiscore) {
131 hiscore=score;
132 rb->lcd_puts(0,2,"New High Score!");
134 else {
135 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore);
136 rb->lcd_puts(0,2,pscore);
138 rb->lcd_update();
139 rb->sleep(3*HZ);
140 dead=1;
143 void colission (short x, short y)
145 switch (board[x][y]) {
146 case 0:
147 break;
148 case -1:
149 snakelength+=2;
150 score+=level;
151 apple=0;
152 break;
153 default:
154 die();
155 break;
157 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0)
158 die();
161 void move_head (short x, short y)
163 switch (dir) {
164 case 0:
165 y-=1;
166 break;
167 case 1:
168 x+=1;
169 break;
170 case 2:
171 y+=1;
172 break;
173 case 3:
174 x-=1;
175 break;
177 colission (x,y);
178 if (dead)
179 return;
180 board[x][y]=1;
181 rb->lcd_fillrect(x*4,y*4,4,4);
184 void frame (void)
186 short x,y,head=0;
187 for (x=0; x<BOARD_WIDTH; x++) {
188 for (y=0; y<BOARD_HEIGHT; y++) {
189 switch (board[x][y]) {
190 case 1:
191 if (!head) {
192 move_head(x,y);
193 if (dead)
194 return;
195 board[x][y]++;
196 head=1;
198 break;
199 case 0:
200 break;
201 case -1:
202 break;
203 default:
204 if (board[x][y]==snakelength) {
205 board[x][y]=0;
206 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
207 rb->lcd_fillrect(x*4,y*4,4,4);
208 rb->lcd_set_drawmode(DRMODE_SOLID);
210 else
211 board[x][y]++;
212 break;
216 rb->lcd_update();
219 void redraw (void)
221 short x,y;
222 rb->lcd_clear_display();
223 for (x=0; x<BOARD_WIDTH; x++) {
224 for (y=0; y<BOARD_HEIGHT; y++) {
225 switch (board[x][y]) {
226 case -1:
227 rb->lcd_fillrect((x*4)+1,y*4,2,4);
228 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
229 break;
230 case 0:
231 break;
232 default:
233 rb->lcd_fillrect(x*4,y*4,4,4);
234 break;
238 rb->lcd_update();
241 void game_pause (void) {
242 int button;
243 rb->lcd_clear_display();
244 rb->lcd_putsxy(3,12,"Game Paused");
245 #if CONFIG_KEYPAD == RECORDER_PAD
246 rb->lcd_putsxy(3,22,"[Play] to resume");
247 #elif CONFIG_KEYPAD == ONDIO_PAD
248 rb->lcd_putsxy(3,22,"[Mode] to resume");
249 #endif
250 rb->lcd_putsxy(3,32,"[Off] to quit");
251 rb->lcd_update();
252 while (1) {
253 button=rb->button_get(true);
254 switch (button) {
255 #ifdef SNAKE_RC_QUIT
256 case SNAKE_RC_QUIT:
257 #endif
258 case SNAKE_QUIT:
259 dead=1;
260 return;
261 case SNAKE_PLAYPAUSE:
262 redraw();
263 rb->sleep(HZ/2);
264 return;
265 default:
266 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
267 dead=2;
268 return;
270 break;
276 void game (void) {
277 int button;
278 short x,y;
279 while (1) {
280 frame();
281 if (dead)
282 return;
283 frames++;
284 if (frames==10) {
285 frames=0;
286 if (!apple) {
287 do {
288 x=rb->rand() % BOARD_WIDTH;
289 y=rb->rand() % BOARD_HEIGHT;
290 } while (board[x][y]);
291 apple=1;
292 board[x][y]=-1;
293 rb->lcd_fillrect((x*4)+1,y*4,2,4);
294 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
298 rb->sleep(HZ/level);
300 button=rb->button_get(false);
302 #ifdef HAS_BUTTON_HOLD
303 if (rb->button_hold())
304 button = SNAKE_PLAYPAUSE;
305 #endif
307 switch (button) {
308 case SNAKE_UP:
309 if (dir!=2) dir=0;
310 break;
311 case BUTTON_RIGHT:
312 if (dir!=3) dir=1;
313 break;
314 case SNAKE_DOWN:
315 if (dir!=0) dir=2;
316 break;
317 case BUTTON_LEFT:
318 if (dir!=1) dir=3;
319 break;
320 #ifdef SNAKE_RC_QUIT
321 case SNAKE_RC_QUIT:
322 #endif
323 case SNAKE_QUIT:
324 dead=1;
325 return;
326 case SNAKE_PLAYPAUSE:
327 game_pause();
328 break;
329 default:
330 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
331 dead=2;
332 return;
334 break;
339 void game_init(void) {
340 int selection=0;
341 short x,y;
342 bool menu_quit = false;
344 for (x=0; x<BOARD_WIDTH; x++) {
345 for (y=0; y<BOARD_HEIGHT; y++) {
346 board[x][y]=0;
349 dead=0;
350 apple=0;
351 snakelength=4;
352 score=0;
353 board[11][7]=1;
355 MENUITEM_STRINGLIST(menu,"Snake Menu",NULL,"Start New Game","Starting Level",
356 "Quit");
358 while (!menu_quit) {
359 switch(rb->do_menu(&menu, &selection))
361 case 0:
362 menu_quit = true; /* start playing */
363 break;
365 case 1:
366 rb->set_int("Starting Level", "", UNIT_INT, &level, NULL,
367 1, 1, 9, NULL );
368 break;
370 default:
371 dead=1; /* quit program */
372 menu_quit = true;
373 break;
379 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
381 (void)(parameter);
382 rb = api;
384 game_init();
385 rb->lcd_clear_display();
386 game();
387 return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED;
390 #endif