1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2003 Mat Holton
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 ****************************************************************************/
25 Board consists of a WIDTHxHEIGHT grid. If board element is 0 then nothing is
26 there otherwise it is part of the snake or a wall.
28 Head and Tail are stored
33 #ifdef HAVE_LCD_BITMAP
34 #include "lib/highscore.h"
35 #include "lib/playback_control.h"
42 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
43 #include "pluginbitmaps/snake2_header1.h"
44 #include "pluginbitmaps/snake2_header2.h"
45 #include "pluginbitmaps/snake2_left.h"
46 #include "pluginbitmaps/snake2_right.h"
47 #include "pluginbitmaps/snake2_bottom.h"
48 #define BMPHEIGHT_snake2_header BMPHEIGHT_snake2_header1
49 #define BMPWIDTH_snake2_header BMPWIDTH_snake2_header1
52 #if (LCD_WIDTH >= 640) && (LCD_HEIGHT >= 480)
53 #define MULTIPLIER 20 /*Modifier for porting on other screens*/
58 #define TOP_X1 68 /* x-coord of the upperleft item (game type) */
59 #define TOP_X2 562 /* x-coord of the upperright item (maze type) */
60 #define TOP_X3 84 /* x-coord of the lowerleft item (speed) */
61 #define TOP_X4 548 /* x-coord of the lowerright item (hi-score) */
62 #define TOP_Y1 8 /* y-coord of the top row of items */
63 #define TOP_Y2 50 /* y-coord of the bottom row of items */
64 #elif (LCD_WIDTH >= 320) && (LCD_HEIGHT >= 240)
65 #define MULTIPLIER 10 /*Modifier for porting on other screens*/
70 #define TOP_X1 34 /* x-coord of the upperleft item (game type) */
71 #define TOP_X2 281 /* x-coord of the upperright item (maze type) */
72 #define TOP_X3 42 /* x-coord of the lowerleft item (speed) */
73 #define TOP_X4 274 /* x-coord of the lowerright item (hi-score) */
74 #define TOP_Y1 4 /* y-coord of the top row of items */
75 #define TOP_Y2 25 /* y-coord of the bottom row of items */
76 #elif (LCD_WIDTH >= 240) && (LCD_HEIGHT >= 168)
88 #elif (LCD_WIDTH >= 220) && (LCD_HEIGHT >= 176)
100 #elif (LCD_WIDTH >= 176) && (LCD_HEIGHT >= 132)
112 #elif (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
133 /* variable button definitions */
134 #if CONFIG_KEYPAD == RECORDER_PAD
135 #define SNAKE2_LEFT BUTTON_LEFT
136 #define SNAKE2_RIGHT BUTTON_RIGHT
137 #define SNAKE2_UP BUTTON_UP
138 #define SNAKE2_DOWN BUTTON_DOWN
139 #define SNAKE2_QUIT BUTTON_OFF
140 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
141 #define SNAKE2_PLAYPAUSE_TEXT "Play"
143 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
144 #define SNAKE2_LEFT BUTTON_LEFT
145 #define SNAKE2_RIGHT BUTTON_RIGHT
146 #define SNAKE2_UP BUTTON_UP
147 #define SNAKE2_DOWN BUTTON_DOWN
148 #define SNAKE2_QUIT BUTTON_OFF
149 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
150 #define SNAKE2_PLAYPAUSE_TEXT "Select"
152 #elif CONFIG_KEYPAD == ONDIO_PAD
153 #define SNAKE2_LEFT BUTTON_LEFT
154 #define SNAKE2_RIGHT BUTTON_RIGHT
155 #define SNAKE2_UP BUTTON_UP
156 #define SNAKE2_DOWN BUTTON_DOWN
157 #define SNAKE2_QUIT BUTTON_OFF
158 #define SNAKE2_PLAYPAUSE BUTTON_MENU
159 #define SNAKE2_PLAYPAUSE_TEXT "Menu"
161 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
162 (CONFIG_KEYPAD == IRIVER_H300_PAD)
163 #define SNAKE2_LEFT BUTTON_LEFT
164 #define SNAKE2_RIGHT BUTTON_RIGHT
165 #define SNAKE2_UP BUTTON_UP
166 #define SNAKE2_DOWN BUTTON_DOWN
167 #define SNAKE2_QUIT BUTTON_OFF
168 #define SNAKE2_PLAYPAUSE BUTTON_ON
169 #define SNAKE2_PLAYPAUSE_TEXT "Play"
171 #define SNAKE2_RC_QUIT BUTTON_RC_STOP
173 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
174 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
175 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
176 #define SNAKE2_LEFT BUTTON_LEFT
177 #define SNAKE2_RIGHT BUTTON_RIGHT
178 #define SNAKE2_UP BUTTON_MENU
179 #define SNAKE2_DOWN BUTTON_PLAY
180 #define SNAKE2_QUIT (BUTTON_SELECT | BUTTON_MENU)
181 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
182 #define SNAKE2_PLAYPAUSE_TEXT "Select"
184 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
185 #define SNAKE2_LEFT BUTTON_LEFT
186 #define SNAKE2_RIGHT BUTTON_RIGHT
187 #define SNAKE2_UP BUTTON_UP
188 #define SNAKE2_DOWN BUTTON_DOWN
189 #define SNAKE2_QUIT BUTTON_POWER
190 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
191 #define SNAKE2_PLAYPAUSE_TEXT "Select"
193 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
194 #define SNAKE2_LEFT BUTTON_LEFT
195 #define SNAKE2_RIGHT BUTTON_RIGHT
196 #define SNAKE2_UP BUTTON_UP
197 #define SNAKE2_DOWN BUTTON_DOWN
198 #define SNAKE2_QUIT BUTTON_POWER
199 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
200 #define SNAKE2_PLAYPAUSE_TEXT "Select"
202 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
203 (CONFIG_KEYPAD == SANSA_C200_PAD)
204 #define SNAKE2_LEFT BUTTON_LEFT
205 #define SNAKE2_RIGHT BUTTON_RIGHT
206 #define SNAKE2_UP BUTTON_UP
207 #define SNAKE2_DOWN BUTTON_DOWN
208 #define SNAKE2_QUIT BUTTON_POWER
209 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
210 #define SNAKE2_PLAYPAUSE_TEXT "Select"
212 #elif (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
213 (CONFIG_KEYPAD == SANSA_M200_PAD)
214 #define SNAKE2_LEFT BUTTON_LEFT
215 #define SNAKE2_RIGHT BUTTON_RIGHT
216 #define SNAKE2_UP BUTTON_UP
217 #define SNAKE2_DOWN BUTTON_DOWN
218 #define SNAKE2_QUIT BUTTON_POWER
219 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
220 #define SNAKE2_PLAYPAUSE_TEXT "Select"
222 #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
223 #define SNAKE2_LEFT BUTTON_LEFT
224 #define SNAKE2_RIGHT BUTTON_RIGHT
225 #define SNAKE2_UP BUTTON_UP
226 #define SNAKE2_DOWN BUTTON_DOWN
227 #define SNAKE2_QUIT (BUTTON_HOME|BUTTON_REPEAT)
228 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
229 #define SNAKE2_PLAYPAUSE_TEXT "Select"
231 #elif (CONFIG_KEYPAD == IRIVER_H10_PAD)
232 #define SNAKE2_LEFT BUTTON_LEFT
233 #define SNAKE2_RIGHT BUTTON_RIGHT
234 #define SNAKE2_UP BUTTON_SCROLL_UP
235 #define SNAKE2_DOWN BUTTON_SCROLL_DOWN
236 #define SNAKE2_QUIT BUTTON_POWER
237 #define SNAKE2_PLAYPAUSE BUTTON_FF
238 #define SNAKE2_PLAYPAUSE_TEXT "FF"
240 #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
241 #define SNAKE2_LEFT BUTTON_LEFT
242 #define SNAKE2_RIGHT BUTTON_RIGHT
243 #define SNAKE2_UP BUTTON_UP
244 #define SNAKE2_DOWN BUTTON_DOWN
245 #define SNAKE2_QUIT BUTTON_BACK
246 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
247 #define SNAKE2_PLAYPAUSE_TEXT "Select"
249 #elif (CONFIG_KEYPAD == MROBE100_PAD)
250 #define SNAKE2_LEFT BUTTON_LEFT
251 #define SNAKE2_RIGHT BUTTON_RIGHT
252 #define SNAKE2_UP BUTTON_UP
253 #define SNAKE2_DOWN BUTTON_DOWN
254 #define SNAKE2_QUIT BUTTON_POWER
255 #define SNAKE2_PLAYPAUSE BUTTON_SELECT
256 #define SNAKE2_PLAYPAUSE_TEXT "Select"
258 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
259 #define SNAKE2_LEFT BUTTON_RC_REW
260 #define SNAKE2_RIGHT BUTTON_RC_FF
261 #define SNAKE2_UP BUTTON_RC_VOL_UP
262 #define SNAKE2_DOWN BUTTON_RC_VOL_DOWN
263 #define SNAKE2_QUIT BUTTON_RC_REC
264 #define SNAKE2_PLAYPAUSE BUTTON_RC_PLAY
265 #define SNAKE2_PLAYPAUSE_TEXT "Play"
267 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
268 #define SNAKE2_QUIT BUTTON_POWER
270 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
271 #define SNAKE2_LEFT BUTTON_LEFT
272 #define SNAKE2_RIGHT BUTTON_RIGHT
273 #define SNAKE2_UP BUTTON_UP
274 #define SNAKE2_DOWN BUTTON_DOWN
275 #define SNAKE2_QUIT BUTTON_BACK
276 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
277 #define SNAKE2_PLAYPAUSE_TEXT "Play"
279 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
280 #define SNAKE2_LEFT BUTTON_LEFT
281 #define SNAKE2_RIGHT BUTTON_RIGHT
282 #define SNAKE2_UP BUTTON_UP
283 #define SNAKE2_DOWN BUTTON_DOWN
284 #define SNAKE2_QUIT BUTTON_POWER
285 #define SNAKE2_PLAYPAUSE BUTTON_VIEW
286 #define SNAKE2_PLAYPAUSE_TEXT "View"
288 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
289 #define SNAKE2_LEFT BUTTON_PREV
290 #define SNAKE2_RIGHT BUTTON_NEXT
291 #define SNAKE2_UP BUTTON_UP
292 #define SNAKE2_DOWN BUTTON_DOWN
293 #define SNAKE2_QUIT BUTTON_POWER
294 #define SNAKE2_PLAYPAUSE BUTTON_RIGHT
295 #define SNAKE2_PLAYPAUSE_TEXT "Right"
297 #elif (CONFIG_KEYPAD == ONDAVX747_PAD) || \
298 (CONFIG_KEYPAD == ONDAVX777_PAD) || \
299 CONFIG_KEYPAD == MROBE500_PAD
300 #define SNAKE2_QUIT BUTTON_POWER
302 #elif (CONFIG_KEYPAD == SAMSUNG_YH_PAD)
303 #define SNAKE2_LEFT BUTTON_LEFT
304 #define SNAKE2_RIGHT BUTTON_RIGHT
305 #define SNAKE2_UP BUTTON_UP
306 #define SNAKE2_DOWN BUTTON_DOWN
307 #define SNAKE2_QUIT BUTTON_REC
308 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
309 #define SNAKE2_PLAYPAUSE_TEXT "Play"
311 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
312 #define SNAKE2_LEFT BUTTON_PREV
313 #define SNAKE2_RIGHT BUTTON_NEXT
314 #define SNAKE2_UP BUTTON_UP
315 #define SNAKE2_DOWN BUTTON_DOWN
316 #define SNAKE2_QUIT BUTTON_REC
317 #define SNAKE2_PLAYPAUSE BUTTON_PLAY
318 #define SNAKE2_PLAYPAUSE_TEXT "Play"
321 #error No keymap defined!
324 #ifdef HAVE_TOUCHSCREEN
326 #define SNAKE2_LEFT BUTTON_MIDLEFT
329 #define SNAKE2_RIGHT BUTTON_MIDRIGHT
332 #define SNAKE2_UP BUTTON_TOPMIDDLE
335 #define SNAKE2_DOWN BUTTON_BOTTOMMIDDLE
338 #define SNAKE2_QUIT BUTTON_TOPLEFT
340 #ifndef SNAKE2_PLAYPAUSE
341 #define SNAKE2_PLAYPAUSE BUTTON_CENTER
343 #ifndef SNAKE2_PLAYPAUSE_TEXT
344 #define SNAKE2_PLAYPAUSE_TEXT "CENTER"
348 static int max_levels
= 0;
349 static char (*level_cache
)[HEIGHT
][WIDTH
];
351 /*Board itself - 2D int array*/
352 static int board
[WIDTH
][HEIGHT
];
354 Buffer for sorting movement (in case user presses two movements during a
357 static int ardirectionbuffer
[2];
364 static int level
= 4, speed
= 5,dead
= 0, quit
= 0;
365 static int sillydir
= 0, num_levels
= 0;
366 static int level_from_file
= 0;
367 static int headx
, heady
, tailx
, taily
, applecountdown
= 5;
368 static int game_type
= 0;
369 static int num_apples_to_get
=1;
370 static int num_apples_to_got
=0;
371 static int game_b_level
=0;
372 static int applecount
=0;
373 /* used for string width, height for orientation purposes */
374 static int strwdt
, strhgt
;
375 static char strbuf
[32];
378 static struct highscore highscores
[NUM_SCORES
];
386 #define EAST_NORTH 32
387 #define EAST_SOUTH 64
388 #define WEST_NORTH 128
389 #define WEST_SOUTH 256
391 #define NORTH_EAST 512
392 #define NORTH_WEST 1024
393 #define SOUTH_EAST 2048
394 #define SOUTH_WEST 4096
396 #define LEVELS_FILE PLUGIN_GAMES_DIR "/snake2.levels"
397 #define SCORE_FILE PLUGIN_GAMES_DIR "/snake2.score"
399 int load_all_levels(void)
404 char buf
[64]; /* Larger than WIDTH, to allow for whitespace after the
407 /* Init the level_cache pointer and
408 calculate how many levels that will fit */
409 level_cache
= rb
->plugin_get_buffer((size_t *)&size
);
410 max_levels
= size
/ (HEIGHT
*WIDTH
);
415 if ((fd
= rb
->open(LEVELS_FILE
, O_RDONLY
)) < 0)
420 while(rb
->read_line(fd
, buf
, 64) > 0)
422 if(rb
->strlen(buf
) == 0) /* Separator? */
425 if(num_levels
> max_levels
)
427 rb
->splash(HZ
, "Too many levels in file");
433 rb
->memcpy(level_cache
[num_levels
][linecnt
], buf
, WIDTH
);
435 if(linecnt
== HEIGHT
)
446 ** Completely clear the board of walls and/or snake
449 void clear_board( void)
453 for (x
= 0; x
< WIDTH
; x
++)
455 for (y
= 0; y
< HEIGHT
; y
++)
462 int load_level( int level_number
)
466 for(y
= 0;y
< HEIGHT
;y
++)
468 for(x
= 0;x
< WIDTH
;x
++)
470 switch(level_cache
[level_number
][y
][x
])
490 ** Gets the currently chosen direction from the first place
491 ** in the direction buffer. If there is something in the
492 ** next part of the buffer then that is moved to the first place
494 void get_direction( void )
496 /*if 1st place is empty*/
497 if(ardirectionbuffer
[0] != -1)
499 /*return this direction*/
500 dir
= ardirectionbuffer
[0];
501 ardirectionbuffer
[0]=-1;
502 /*now see if one needs moving:*/
503 if(ardirectionbuffer
[1] != -1)
505 /*there's a move waiting to be done
506 so move it into the space:*/
507 ardirectionbuffer
[0] = ardirectionbuffer
[1];
508 ardirectionbuffer
[1] = -1;
514 ** Sets the direction
516 void set_direction(int newdir
)
518 if(ardirectionbuffer
[0] != newdir
)
520 /*if 1st place is empty*/
521 if(ardirectionbuffer
[0] == -1)
524 ardirectionbuffer
[0] = newdir
;
529 if(ardirectionbuffer
[0] != newdir
) ardirectionbuffer
[1] = newdir
;
532 if(frames
< 0) ardirectionbuffer
[0] = newdir
;
536 void new_level(int level
)
540 ardirectionbuffer
[0] = -1;
541 ardirectionbuffer
[1] = -1;
548 /*Create a small snake to start off with*/
549 board
[headx
][heady
] = dir
;
550 board
[headx
-1][heady
] = dir
;
551 board
[headx
-2][heady
] = dir
;
552 board
[headx
-3][heady
] = dir
;
553 board
[headx
-4][heady
] = dir
;
557 void init_snake(void)
563 new_level(level_from_file
);
566 #if (LCD_WIDTH >= 160) && (LCD_HEIGHT >= 128)
567 void draw_frame_bitmap(int header_type
)
569 rb
->lcd_bitmap(header_type
==1? snake2_header1
: snake2_header2
, 0, 0,
570 BMPWIDTH_snake2_header
, BMPHEIGHT_snake2_header
);
571 rb
->lcd_bitmap(snake2_left
, 0, BMPHEIGHT_snake2_header
,
572 BMPWIDTH_snake2_left
, BMPHEIGHT_snake2_left
);
573 rb
->lcd_bitmap(snake2_right
,
574 LCD_WIDTH
- BMPWIDTH_snake2_right
, BMPHEIGHT_snake2_header
,
575 BMPWIDTH_snake2_right
, BMPHEIGHT_snake2_right
);
576 rb
->lcd_bitmap(snake2_bottom
,
577 0, BMPHEIGHT_snake2_header
+ BMPHEIGHT_snake2_left
,
578 BMPWIDTH_snake2_bottom
, BMPHEIGHT_snake2_bottom
);
583 ** Draws the apple. If it doesn't exist then
584 ** a new one get's created.
586 void draw_apple_bit(int x
, int y
)
588 rb
->lcd_fillrect((CENTER_X
+x
*MULTIPLIER
)+1, CENTER_Y
+y
*MULTIPLIER
,
589 MODIFIER_2
, MODIFIER_1
);
590 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
, (CENTER_Y
+y
*MULTIPLIER
)+1,
591 MODIFIER_1
, MODIFIER_2
);
594 void draw_apple( void )
598 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
599 draw_frame_bitmap(2);
601 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", applecount
);
602 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
603 rb
->lcd_putsxy(TOP_X3
- strwdt
/2, TOP_Y2
, strbuf
);
605 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", score
);
606 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
607 rb
->lcd_putsxy(TOP_X4
- strwdt
/2, TOP_Y2
, strbuf
);
614 x
= (rb
->rand() % (WIDTH
-1))+1;
615 y
= (rb
->rand() % (HEIGHT
-1))+1;
616 } while (board
[x
][y
]);
619 applex
= x
;appley
= y
;
621 draw_apple_bit(applex
, appley
);
630 void draw_vertical_bit(int x
, int y
)
632 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+1, CENTER_Y
+y
*MULTIPLIER
,
633 MODIFIER_2
, MODIFIER_1
);
642 void draw_horizontal_bit(int x
, int y
)
644 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
, CENTER_Y
+y
*MULTIPLIER
+1,
645 MODIFIER_1
, MODIFIER_2
);
654 void draw_n_to_e_bit(int x
, int y
)
656 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+1, CENTER_Y
+y
*MULTIPLIER
+2,
657 MODIFIER_2
, MODIFIER_2
);
658 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+2, CENTER_Y
+y
*MULTIPLIER
+1,
659 MODIFIER_2
, MODIFIER_2
);
668 void draw_w_to_s_bit(int x
, int y
)
670 draw_n_to_e_bit(x
,y
);
679 void draw_n_to_w_bit(int x
, int y
)
681 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
, CENTER_Y
+y
*MULTIPLIER
+1,
682 MODIFIER_2
, MODIFIER_2
);
683 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+1, CENTER_Y
+y
*MULTIPLIER
+2,
684 MODIFIER_2
, MODIFIER_2
);
693 void draw_e_to_s_bit(int x
, int y
)
695 draw_n_to_w_bit(x
, y
);
704 void draw_s_to_e_bit(int x
, int y
)
706 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+1, CENTER_Y
+y
*MULTIPLIER
,
707 MODIFIER_2
, MODIFIER_2
);
708 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+2, CENTER_Y
+y
*MULTIPLIER
+1,
709 MODIFIER_2
, MODIFIER_2
);
718 void draw_w_to_n_bit(int x
, int y
)
720 draw_s_to_e_bit(x
,y
);
729 void draw_e_to_n_bit(int x
, int y
)
731 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
+1, CENTER_Y
+y
*MULTIPLIER
,
732 MODIFIER_2
, MODIFIER_2
);
733 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
, CENTER_Y
+y
*MULTIPLIER
+1,
734 MODIFIER_2
, MODIFIER_2
);
743 void draw_s_to_w_bit(int x
, int y
)
745 draw_e_to_n_bit(x
, y
);
748 void draw_head_bit(int x
, int y
)
750 rb
->lcd_fillrect(CENTER_X
+x
*MULTIPLIER
, CENTER_Y
+y
*MULTIPLIER
,
751 MODIFIER_1
, MODIFIER_1
);
755 ** Draws a wall/obsticals
757 void draw_boundary ( void )
761 /*TODO: Load levels from file!*/
763 /*top and bottom line*/
764 for(x
=0; x
< WIDTH
; x
++)
767 board
[x
][HEIGHT
-1] = WEST
;
770 /*left and right lines*/
771 for(y
=0; y
< HEIGHT
; y
++)
774 board
[WIDTH
-1][y
] = SOUTH
;
778 board
[0][0] = NORTH_EAST
;
779 board
[WIDTH
-1][0] = EAST_SOUTH
;
780 board
[0][HEIGHT
-1] = SOUTH_EAST
;
781 board
[WIDTH
-1][HEIGHT
-1] = EAST_NORTH
;
785 ** Redraw the entire board
791 #ifdef HAVE_LCD_COLOR
792 rb
->lcd_set_foreground(LCD_BLACK
);
793 rb
->lcd_set_background(LCD_WHITE
);
796 rb
->lcd_clear_display();
798 for (x
= 0; x
< WIDTH
; x
++)
800 for (y
= 0; y
< HEIGHT
; y
++)
805 draw_apple_bit(x
, y
);
812 draw_vertical_bit(x
,y
);
817 draw_horizontal_bit(x
,y
);
827 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
828 draw_frame_bitmap(2);
830 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", applecount
);
831 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
832 rb
->lcd_putsxy(TOP_X3
- strwdt
/2, TOP_Y2
, strbuf
);
834 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", score
);
835 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
836 rb
->lcd_putsxy(TOP_X4
- strwdt
/2, TOP_Y2
, strbuf
);
841 ** Draws the snake bit described by nCurrentBit at position x/y
842 ** deciding whether it's a corner bit by examing the nPrevious bit
844 void draw_snake_bit(int currentbit
, int previousbit
, int x
, int y
)
846 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
848 rb
->lcd_set_drawmode(DRMODE_SOLID
);
857 draw_vertical_bit(x
,y
);
861 draw_e_to_n_bit(x
,y
);
865 draw_w_to_n_bit(x
,y
);
875 draw_horizontal_bit(x
,y
);
879 draw_n_to_e_bit(x
,y
);
883 draw_s_to_e_bit(x
,y
);
893 draw_vertical_bit(x
,y
);
897 draw_e_to_s_bit(x
,y
);
901 draw_w_to_s_bit(x
,y
);
911 draw_horizontal_bit(x
,y
);
915 draw_s_to_w_bit(x
,y
);
919 draw_n_to_w_bit(x
,y
);
926 void redraw_snake(void)
928 int x
= tailx
, y
= taily
;
929 int olddir
, newdir
= board
[x
][y
];
931 while (x
!= headx
|| y
!= heady
)
964 newdir
= board
[x
][y
];
966 draw_snake_bit(newdir
, olddir
, x
, y
);
971 ** Death 'sequence' and end game stuff.
978 rb
->splash(HZ
*2, "Oops!");
980 rb
->lcd_clear_display();
984 rb
->lcd_getstringsize("You died!",&strwdt
,&strhgt
);
985 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2,strhgt
,"You died!");
987 rb
->snprintf(strbuf
, sizeof(strbuf
), "Your score: %d", score
);
988 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
989 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2, strhgt
* 2 + 2, strbuf
);
991 if (highscore_update(score
, level_from_file
, game_type
==0?"Type A":"Type B",
992 highscores
, NUM_SCORES
) == 0)
994 rb
->lcd_getstringsize("New high score!",&strwdt
,&strhgt
);
995 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2,strhgt
* 4 + 2,"New high score!");
999 rb
->snprintf(strbuf
, sizeof(strbuf
), "High score: %d", highscores
[0].score
);
1000 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1001 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2, strhgt
* 5, strbuf
);
1004 rb
->snprintf(strbuf
, sizeof(strbuf
), "Press %s...", SNAKE2_PLAYPAUSE_TEXT
);
1005 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1006 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2, strhgt
* 7, strbuf
);
1012 button
=rb
->button_get(true);
1015 case SNAKE2_PLAYPAUSE
:
1025 ** Check for collision. TODO: Currently this
1026 ** sets of the death sequence. What we want is it to only return a true/false
1027 ** depending on whether a collision occured.
1029 void collision ( int x
, int y
)
1034 switch (board
[x
][y
])
1040 score
= score
+ (1 * level
);
1047 if(num_apples_to_get
== num_apples_to_got
)
1050 if(level_from_file
>= num_levels
)
1052 level_from_file
= 1;
1053 /*and increase the number of apples to pick up
1054 before level changes*/
1055 num_apples_to_get
+=2;
1058 rb
->splash(HZ
, "Level Completed!");
1059 new_level(level_from_file
);
1064 num_apples_to_got
++;
1083 /*this actually sets the dir variable.*/
1089 board
[headx
][heady
]=NORTH
;
1093 board
[headx
][heady
]=EAST
;
1097 board
[headx
][heady
]=SOUTH
;
1101 board
[headx
][heady
]=WEST
;
1116 draw_head_bit(headx
, heady
);
1119 if(applecountdown
<= 0)
1121 rb
->lcd_set_drawmode(DRMODE_SOLID
|DRMODE_INVERSEVID
);
1122 draw_head_bit(tailx
, taily
);
1123 rb
->lcd_set_drawmode(DRMODE_SOLID
);
1125 taildir
= board
[tailx
][taily
];
1126 board
[tailx
][taily
] = 0;
1163 int olddir
, noldx
, noldy
, temp
;
1170 if(heady
== HEIGHT
-1)
1175 olddir
= board
[headx
][temp
];
1184 olddir
= board
[temp
][heady
];
1193 olddir
= board
[headx
][temp
];
1197 if(headx
== WIDTH
-1)
1202 olddir
= board
[temp
][heady
];
1209 now redraw the bit that was
1210 the tail, to something snake-like:
1212 draw_snake_bit(dir
, olddir
, noldx
, noldy
);
1214 collision(headx
, heady
);
1219 void game_pause (void)
1223 rb
->lcd_clear_display();
1224 rb
->lcd_getstringsize("Paused",&strwdt
,&strhgt
);
1225 rb
->lcd_putsxy((LCD_WIDTH
- strwdt
)/2,LCD_HEIGHT
/2,"Paused");
1230 button
= rb
->button_get(true);
1233 case SNAKE2_PLAYPAUSE
:
1236 draw_head_bit(headx
, heady
);
1241 #ifdef SNAKE2_RC_QUIT
1242 case SNAKE2_RC_QUIT
:
1250 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
) {
1272 if(frames
> 0) frames
= 0;
1286 /*it has, great set frames to a positive value again:*/
1296 rb
->sleep(HZ
/speed
);
1298 button
= rb
->button_get(false);
1300 #ifdef HAS_BUTTON_HOLD
1301 if (rb
->button_hold())
1302 button
= SNAKE2_PLAYPAUSE
;
1308 case SNAKE2_UP
| BUTTON_REPEAT
:
1309 if (dir
!= SOUTH
) set_direction(NORTH
);
1313 case SNAKE2_RIGHT
| BUTTON_REPEAT
:
1314 if (dir
!= WEST
) set_direction(EAST
);
1318 case SNAKE2_DOWN
| BUTTON_REPEAT
:
1319 if (dir
!= NORTH
) set_direction(SOUTH
);
1323 case SNAKE2_LEFT
| BUTTON_REPEAT
:
1324 if (dir
!= EAST
) set_direction(WEST
);
1327 #ifdef SNAKE2_RC_QUIT
1328 case SNAKE2_RC_QUIT
:
1334 case SNAKE2_PLAYPAUSE
:
1339 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
) {
1349 void select_maze(void)
1354 load_level( level_from_file
);
1360 #if LCD_WIDTH >= 160 && LCD_HEIGHT >= 128
1361 draw_frame_bitmap(1);
1363 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", level
);
1364 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1365 rb
->lcd_putsxy(TOP_X3
- strwdt
/2, TOP_Y2
, strbuf
);
1367 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", level_from_file
);
1368 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1369 rb
->lcd_putsxy(TOP_X2
- strwdt
/2, TOP_Y1
, strbuf
);
1371 rb
->strcpy(strbuf
, game_type
==0? "A": "B");
1372 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1373 rb
->lcd_putsxy(TOP_X1
, TOP_Y1
, strbuf
);
1375 rb
->snprintf(strbuf
, sizeof(strbuf
), "%d", highscores
[0].score
);
1376 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1377 rb
->lcd_putsxy(TOP_X4
- strwdt
/2, TOP_Y2
, strbuf
);
1380 rb
->snprintf(strbuf
, sizeof(strbuf
), "Maze: %d", level_from_file
);
1381 rb
->lcd_getstringsize(strbuf
, &strwdt
, &strhgt
);
1382 rb
->lcd_putsxy((WIDTH
*MULTIPLIER
- strwdt
)/2,
1383 (HEIGHT
*MULTIPLIER
- strhgt
)/2, strbuf
);
1388 button
= rb
->button_get(true);
1392 case SNAKE2_PLAYPAUSE
:
1397 if(level_from_file
< num_levels
)
1400 level_from_file
= 0;
1401 load_level( level_from_file
);
1406 if(level_from_file
> 0)
1409 level_from_file
= num_levels
;
1410 load_level( level_from_file
);
1414 if (rb
->default_event_handler(button
)==SYS_USB_CONNECTED
) {
1424 void game_init(void)
1428 static const struct opt_items type_options
[] = {
1433 MENUITEM_STRINGLIST(menu
, "Snake2 Menu", NULL
,
1435 "Game Type", "Select Maze", "Speed",
1437 "Playback Control", "Quit");
1439 rb
->button_clear_queue();
1447 switch (rb
->do_menu(&menu
, &selection
, NULL
, false)) {
1452 rb
->set_option("Game Type", &game_type
, INT
,
1453 type_options
, 2, NULL
);
1460 rb
->set_int("Speed", "", UNIT_INT
, &level
,
1461 NULL
, 1, 1, 10, NULL
);
1464 highscore_show(-1, highscores
, NUM_SCORES
, true);
1467 playback_control(NULL
);
1472 case MENU_ATTACHED_USB
:
1481 enum plugin_status
plugin_start(const void* parameter
)
1485 /* Lets use the default font */
1486 rb
->lcd_setfont(FONT_SYSFIXED
);
1488 rb
->lcd_set_backdrop(NULL
);
1493 if (num_levels
== 0) {
1494 rb
->splash(HZ
*2, "Failed loading levels!");
1498 highscore_load(SCORE_FILE
, highscores
, NUM_SCORES
);
1506 rb
->lcd_clear_display();
1515 highscore_save(SCORE_FILE
, highscores
, NUM_SCORES
);
1517 return (quit
==1) ? PLUGIN_OK
: PLUGIN_USB_CONNECTED
;