A couple more tweaks
[Rockbox.git] / apps / plugins / snake.c
blob2beeaa4c5ba892a807700f84c5b17f8a7e87f262
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 #else
100 #error "lacks keymapping"
101 #endif
103 #define BOARD_WIDTH (LCD_WIDTH/4)
104 #define BOARD_HEIGHT (LCD_HEIGHT/4)
106 static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength;
107 static unsigned int score,hiscore=0,level=1;
108 static short dir,frames,apple,dead=0;
109 static struct plugin_api* rb;
111 void die (void)
113 char pscore[17];
114 rb->lcd_clear_display();
115 rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score);
116 rb->lcd_puts(0,0,"Oops...");
117 rb->lcd_puts(0,1, pscore);
118 if (score>hiscore) {
119 hiscore=score;
120 rb->lcd_puts(0,2,"New High Score!");
122 else {
123 rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore);
124 rb->lcd_puts(0,2,pscore);
126 rb->lcd_update();
127 rb->sleep(3*HZ);
128 dead=1;
131 void colission (short x, short y)
133 switch (board[x][y]) {
134 case 0:
135 break;
136 case -1:
137 snakelength+=2;
138 score+=level;
139 apple=0;
140 break;
141 default:
142 die();
143 break;
145 if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0)
146 die();
149 void move_head (short x, short y)
151 switch (dir) {
152 case 0:
153 y-=1;
154 break;
155 case 1:
156 x+=1;
157 break;
158 case 2:
159 y+=1;
160 break;
161 case 3:
162 x-=1;
163 break;
165 colission (x,y);
166 if (dead)
167 return;
168 board[x][y]=1;
169 rb->lcd_fillrect(x*4,y*4,4,4);
172 void frame (void)
174 short x,y,head=0;
175 for (x=0; x<BOARD_WIDTH; x++) {
176 for (y=0; y<BOARD_HEIGHT; y++) {
177 switch (board[x][y]) {
178 case 1:
179 if (!head) {
180 move_head(x,y);
181 if (dead)
182 return;
183 board[x][y]++;
184 head=1;
186 break;
187 case 0:
188 break;
189 case -1:
190 break;
191 default:
192 if (board[x][y]==snakelength) {
193 board[x][y]=0;
194 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
195 rb->lcd_fillrect(x*4,y*4,4,4);
196 rb->lcd_set_drawmode(DRMODE_SOLID);
198 else
199 board[x][y]++;
200 break;
204 rb->lcd_update();
207 void redraw (void)
209 short x,y;
210 rb->lcd_clear_display();
211 for (x=0; x<BOARD_WIDTH; x++) {
212 for (y=0; y<BOARD_HEIGHT; y++) {
213 switch (board[x][y]) {
214 case -1:
215 rb->lcd_fillrect((x*4)+1,y*4,2,4);
216 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
217 break;
218 case 0:
219 break;
220 default:
221 rb->lcd_fillrect(x*4,y*4,4,4);
222 break;
226 rb->lcd_update();
229 void game_pause (void) {
230 int button;
231 rb->lcd_clear_display();
232 rb->lcd_putsxy(3,12,"Game Paused");
233 #if CONFIG_KEYPAD == RECORDER_PAD
234 rb->lcd_putsxy(3,22,"[Play] to resume");
235 #elif CONFIG_KEYPAD == ONDIO_PAD
236 rb->lcd_putsxy(3,22,"[Mode] to resume");
237 #endif
238 rb->lcd_putsxy(3,32,"[Off] to quit");
239 rb->lcd_update();
240 while (1) {
241 button=rb->button_get(true);
242 switch (button) {
243 #ifdef SNAKE_RC_QUIT
244 case SNAKE_RC_QUIT:
245 #endif
246 case SNAKE_QUIT:
247 dead=1;
248 return;
249 case SNAKE_PLAYPAUSE:
250 redraw();
251 rb->sleep(HZ/2);
252 return;
253 default:
254 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
255 dead=2;
256 return;
258 break;
264 void game (void) {
265 int button;
266 short x,y;
267 while (1) {
268 frame();
269 if (dead)
270 return;
271 frames++;
272 if (frames==10) {
273 frames=0;
274 if (!apple) {
275 do {
276 x=rb->rand() % BOARD_WIDTH;
277 y=rb->rand() % BOARD_HEIGHT;
278 } while (board[x][y]);
279 apple=1;
280 board[x][y]=-1;
281 rb->lcd_fillrect((x*4)+1,y*4,2,4);
282 rb->lcd_fillrect(x*4,(y*4)+1,4,2);
286 rb->sleep(HZ/level);
288 button=rb->button_get(false);
290 #ifdef HAS_BUTTON_HOLD
291 if (rb->button_hold())
292 button = SNAKE_PLAYPAUSE;
293 #endif
295 switch (button) {
296 case SNAKE_UP:
297 if (dir!=2) dir=0;
298 break;
299 case BUTTON_RIGHT:
300 if (dir!=3) dir=1;
301 break;
302 case SNAKE_DOWN:
303 if (dir!=0) dir=2;
304 break;
305 case BUTTON_LEFT:
306 if (dir!=1) dir=3;
307 break;
308 #ifdef SNAKE_RC_QUIT
309 case SNAKE_RC_QUIT:
310 #endif
311 case SNAKE_QUIT:
312 dead=1;
313 return;
314 case SNAKE_PLAYPAUSE:
315 game_pause();
316 break;
317 default:
318 if (rb->default_event_handler(button)==SYS_USB_CONNECTED) {
319 dead=2;
320 return;
322 break;
327 void game_init(void) {
328 int selection=0;
329 short x,y;
330 bool menu_quit = false;
332 for (x=0; x<BOARD_WIDTH; x++) {
333 for (y=0; y<BOARD_HEIGHT; y++) {
334 board[x][y]=0;
337 dead=0;
338 apple=0;
339 snakelength=4;
340 score=0;
341 board[11][7]=1;
343 MENUITEM_STRINGLIST(menu,"Snake Menu",NULL,"Start New Game","Starting Level",
344 "Quit");
346 while (!menu_quit) {
347 switch(rb->do_menu(&menu, &selection))
349 case 0:
350 menu_quit = true; /* start playing */
351 break;
353 case 1:
354 rb->set_int("Starting Level", "", UNIT_INT, &level, NULL,
355 1, 1, 9, NULL );
356 break;
358 default:
359 dead=1; /* quit program */
360 menu_quit = true;
361 break;
367 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
369 (void)(parameter);
370 rb = api;
372 game_init();
373 rb->lcd_clear_display();
374 game();
375 return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED;
378 #endif