Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 Itai Shaked |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | /* |
| 23 | Snake! |
| 24 | |
| 25 | by Itai Shaked |
| 26 | |
| 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; |
| 32 | |
| 33 | */ |
| 34 | |
| 35 | #include "plugin.h" |
| 36 | #ifdef HAVE_LCD_BITMAP |
| 37 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 38 | PLUGIN_HEADER |
| 39 | |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 40 | /* variable button definitions */ |
| 41 | #if CONFIG_KEYPAD == RECORDER_PAD |
| 42 | #define SNAKE_QUIT BUTTON_OFF |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 43 | #define SNAKE_LEFT BUTTON_LEFT |
| 44 | #define SNAKE_RIGHT BUTTON_RIGHT |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 45 | #define SNAKE_UP BUTTON_UP |
| 46 | #define SNAKE_DOWN BUTTON_DOWN |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 47 | #define SNAKE_PLAYPAUSE BUTTON_PLAY |
| 48 | |
Dave Chapman | d64e626 | 2007-01-14 13:48:09 +0000 | [diff] [blame] | 49 | #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD |
| 50 | #define SNAKE_QUIT BUTTON_OFF |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 51 | #define SNAKE_LEFT BUTTON_LEFT |
| 52 | #define SNAKE_RIGHT BUTTON_RIGHT |
Dave Chapman | d64e626 | 2007-01-14 13:48:09 +0000 | [diff] [blame] | 53 | #define SNAKE_UP BUTTON_UP |
| 54 | #define SNAKE_DOWN BUTTON_DOWN |
| 55 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 56 | |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 57 | #elif CONFIG_KEYPAD == ONDIO_PAD |
| 58 | #define SNAKE_QUIT BUTTON_OFF |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 59 | #define SNAKE_LEFT BUTTON_LEFT |
| 60 | #define SNAKE_RIGHT BUTTON_RIGHT |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 61 | #define SNAKE_UP BUTTON_UP |
| 62 | #define SNAKE_DOWN BUTTON_DOWN |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 63 | #define SNAKE_PLAYPAUSE BUTTON_MENU |
| 64 | |
Daniel Stenberg | 01377e2 | 2005-06-29 12:47:24 +0000 | [diff] [blame] | 65 | #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \ |
| 66 | (CONFIG_KEYPAD == IRIVER_H300_PAD) |
Daniel Stenberg | 6700ad8 | 2005-02-04 12:41:09 +0000 | [diff] [blame] | 67 | #define SNAKE_QUIT BUTTON_OFF |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 68 | #define SNAKE_LEFT BUTTON_LEFT |
| 69 | #define SNAKE_RIGHT BUTTON_RIGHT |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 70 | #define SNAKE_UP BUTTON_UP |
| 71 | #define SNAKE_DOWN BUTTON_DOWN |
Daniel Stenberg | 6700ad8 | 2005-02-04 12:41:09 +0000 | [diff] [blame] | 72 | #define SNAKE_PLAYPAUSE BUTTON_ON |
| 73 | |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 74 | #define SNAKE_RC_QUIT BUTTON_RC_STOP |
| 75 | |
Dave Chapman | fb4e384 | 2006-02-24 20:54:09 +0000 | [diff] [blame] | 76 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \ |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 77 | (CONFIG_KEYPAD == IPOD_3G_PAD) || \ |
| 78 | (CONFIG_KEYPAD == IPOD_1G2G_PAD) |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 79 | #define SNAKE_QUIT (BUTTON_SELECT|BUTTON_MENU) |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 80 | #define SNAKE_LEFT BUTTON_LEFT |
| 81 | #define SNAKE_RIGHT BUTTON_RIGHT |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 82 | #define SNAKE_UP BUTTON_MENU |
| 83 | #define SNAKE_DOWN BUTTON_PLAY |
| 84 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 85 | |
Jens Arnold | 85a226d | 2007-03-16 23:02:39 +0000 | [diff] [blame] | 86 | #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD) |
Daniel Stenberg | cedba88 | 2006-01-18 11:09:06 +0000 | [diff] [blame] | 87 | #define SNAKE_QUIT BUTTON_POWER |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 88 | #define SNAKE_LEFT BUTTON_LEFT |
| 89 | #define SNAKE_RIGHT BUTTON_RIGHT |
Daniel Stenberg | cedba88 | 2006-01-18 11:09:06 +0000 | [diff] [blame] | 90 | #define SNAKE_UP BUTTON_UP |
| 91 | #define SNAKE_DOWN BUTTON_DOWN |
| 92 | #define SNAKE_PLAYPAUSE BUTTON_PLAY |
| 93 | |
Marcoen Hirschberg | 338e2bb | 2006-02-24 15:42:52 +0000 | [diff] [blame] | 94 | #elif (CONFIG_KEYPAD == GIGABEAT_PAD) |
Marcoen Hirschberg | a7168fe | 2007-05-19 23:38:09 +0000 | [diff] [blame] | 95 | #define SNAKE_QUIT BUTTON_POWER |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 96 | #define SNAKE_LEFT BUTTON_LEFT |
| 97 | #define SNAKE_RIGHT BUTTON_RIGHT |
Marcoen Hirschberg | 338e2bb | 2006-02-24 15:42:52 +0000 | [diff] [blame] | 98 | #define SNAKE_UP BUTTON_UP |
| 99 | #define SNAKE_DOWN BUTTON_DOWN |
| 100 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 101 | |
Marianne Arnold | 12ddb8e | 2007-09-20 10:49:48 +0000 | [diff] [blame] | 102 | #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \ |
| 103 | (CONFIG_KEYPAD == SANSA_C200_PAD) |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 104 | #define SNAKE_QUIT BUTTON_POWER |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 105 | #define SNAKE_LEFT BUTTON_LEFT |
| 106 | #define SNAKE_RIGHT BUTTON_RIGHT |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 107 | #define SNAKE_UP BUTTON_UP |
| 108 | #define SNAKE_DOWN BUTTON_DOWN |
| 109 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 110 | |
Daniel Stenberg | 1e88be5 | 2006-08-03 20:17:25 +0000 | [diff] [blame] | 111 | #elif (CONFIG_KEYPAD == IRIVER_H10_PAD) |
| 112 | #define SNAKE_QUIT BUTTON_POWER |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 113 | #define SNAKE_LEFT BUTTON_LEFT |
| 114 | #define SNAKE_RIGHT BUTTON_RIGHT |
Daniel Stenberg | 1e88be5 | 2006-08-03 20:17:25 +0000 | [diff] [blame] | 115 | #define SNAKE_UP BUTTON_SCROLL_UP |
| 116 | #define SNAKE_DOWN BUTTON_SCROLL_DOWN |
| 117 | #define SNAKE_PLAYPAUSE BUTTON_PLAY |
| 118 | |
Will Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 119 | #elif (CONFIG_KEYPAD == GIGABEAT_S_PAD) |
| 120 | #define SNAKE_QUIT BUTTON_BACK |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 121 | #define SNAKE_LEFT BUTTON_LEFT |
| 122 | #define SNAKE_RIGHT BUTTON_RIGHT |
Will Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 123 | #define SNAKE_UP BUTTON_UP |
| 124 | #define SNAKE_DOWN BUTTON_DOWN |
| 125 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 126 | |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 127 | #elif (CONFIG_KEYPAD == MROBE100_PAD) |
| 128 | #define SNAKE_QUIT BUTTON_POWER |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 129 | #define SNAKE_LEFT BUTTON_LEFT |
| 130 | #define SNAKE_RIGHT BUTTON_RIGHT |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 131 | #define SNAKE_UP BUTTON_UP |
| 132 | #define SNAKE_DOWN BUTTON_DOWN |
| 133 | #define SNAKE_PLAYPAUSE BUTTON_SELECT |
| 134 | |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 135 | #elif CONFIG_KEYPAD == IAUDIO_M3_PAD |
| 136 | #define SNAKE_QUIT BUTTON_RC_REC |
| 137 | #define SNAKE_LEFT BUTTON_RC_REW |
| 138 | #define SNAKE_RIGHT BUTTON_RC_FF |
| 139 | #define SNAKE_UP BUTTON_RC_VOL_UP |
| 140 | #define SNAKE_DOWN BUTTON_RC_VOL_DOWN |
| 141 | #define SNAKE_PLAYPAUSE BUTTON_RC_PLAY |
| 142 | |
| 143 | #define SNAKE_RC_QUIT BUTTON_REC |
| 144 | |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 145 | #elif (CONFIG_KEYPAD == COWOND2_PAD) |
| 146 | #define SNAKE_QUIT BUTTON_POWER |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 147 | |
Daniel Stenberg | 6700ad8 | 2005-02-04 12:41:09 +0000 | [diff] [blame] | 148 | #else |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 149 | #error No keymap defined! |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
Rob Purchase | 297e050 | 2008-04-27 15:30:19 +0000 | [diff] [blame] | 152 | #ifdef HAVE_TOUCHPAD |
| 153 | #ifndef SNAKE_QUIT |
| 154 | #define SNAKE_QUIT BUTTON_TOPLEFT |
| 155 | #endif |
| 156 | #ifndef SNAKE_LEFT |
| 157 | #define SNAKE_LEFT BUTTON_MIDLEFT |
| 158 | #endif |
| 159 | #ifndef SNAKE_RIGHT |
| 160 | #define SNAKE_RIGHT BUTTON_MIDRIGHT |
| 161 | #endif |
| 162 | #ifndef SNAKE_UP |
| 163 | #define SNAKE_UP BUTTON_TOPMIDDLE |
| 164 | #endif |
| 165 | #ifndef SNAKE_DOWN |
| 166 | #define SNAKE_DOWN BUTTON_BOTTOMMIDDLE |
| 167 | #endif |
| 168 | #ifndef SNAKE_PLAYPAUSE |
| 169 | #define SNAKE_PLAYPAUSE BUTTON_CENTER |
| 170 | #endif |
| 171 | #endif |
| 172 | |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 173 | #define BOARD_WIDTH (LCD_WIDTH/4) |
| 174 | #define BOARD_HEIGHT (LCD_HEIGHT/4) |
| 175 | |
| 176 | static int board[BOARD_WIDTH][BOARD_HEIGHT],snakelength; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 177 | static unsigned int score,hiscore=0,level=1; |
| 178 | static short dir,frames,apple,dead=0; |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 179 | static const struct plugin_api* rb; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 180 | |
| 181 | void die (void) |
| 182 | { |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 183 | char pscore[17]; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 184 | rb->lcd_clear_display(); |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 185 | rb->snprintf(pscore,sizeof(pscore),"Your score: %d",score); |
| 186 | rb->lcd_puts(0,0,"Oops..."); |
| 187 | rb->lcd_puts(0,1, pscore); |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 188 | if (score>hiscore) { |
| 189 | hiscore=score; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 190 | rb->lcd_puts(0,2,"New High Score!"); |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 191 | } |
| 192 | else { |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 193 | rb->snprintf(pscore,sizeof(pscore),"High Score: %d",hiscore); |
| 194 | rb->lcd_puts(0,2,pscore); |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 195 | } |
| 196 | rb->lcd_update(); |
| 197 | rb->sleep(3*HZ); |
| 198 | dead=1; |
| 199 | } |
| 200 | |
| 201 | void colission (short x, short y) |
| 202 | { |
| 203 | switch (board[x][y]) { |
| 204 | case 0: |
| 205 | break; |
| 206 | case -1: |
| 207 | snakelength+=2; |
| 208 | score+=level; |
| 209 | apple=0; |
| 210 | break; |
| 211 | default: |
| 212 | die(); |
| 213 | break; |
| 214 | } |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 215 | if (x==BOARD_WIDTH || x<0 || y==BOARD_HEIGHT || y<0) |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 216 | die(); |
| 217 | } |
| 218 | |
| 219 | void move_head (short x, short y) |
| 220 | { |
| 221 | switch (dir) { |
| 222 | case 0: |
| 223 | y-=1; |
| 224 | break; |
| 225 | case 1: |
| 226 | x+=1; |
| 227 | break; |
| 228 | case 2: |
| 229 | y+=1; |
| 230 | break; |
| 231 | case 3: |
| 232 | x-=1; |
| 233 | break; |
| 234 | } |
| 235 | colission (x,y); |
| 236 | if (dead) |
| 237 | return; |
| 238 | board[x][y]=1; |
| 239 | rb->lcd_fillrect(x*4,y*4,4,4); |
| 240 | } |
| 241 | |
| 242 | void frame (void) |
| 243 | { |
| 244 | short x,y,head=0; |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 245 | for (x=0; x<BOARD_WIDTH; x++) { |
| 246 | for (y=0; y<BOARD_HEIGHT; y++) { |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 247 | switch (board[x][y]) { |
| 248 | case 1: |
| 249 | if (!head) { |
| 250 | move_head(x,y); |
| 251 | if (dead) |
| 252 | return; |
| 253 | board[x][y]++; |
| 254 | head=1; |
| 255 | } |
| 256 | break; |
| 257 | case 0: |
| 258 | break; |
| 259 | case -1: |
| 260 | break; |
| 261 | default: |
| 262 | if (board[x][y]==snakelength) { |
| 263 | board[x][y]=0; |
Jens Arnold | 04daef1 | 2005-06-24 22:33:21 +0000 | [diff] [blame] | 264 | rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); |
| 265 | rb->lcd_fillrect(x*4,y*4,4,4); |
| 266 | rb->lcd_set_drawmode(DRMODE_SOLID); |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 267 | } |
| 268 | else |
| 269 | board[x][y]++; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | rb->lcd_update(); |
| 275 | } |
| 276 | |
| 277 | void redraw (void) |
| 278 | { |
| 279 | short x,y; |
| 280 | rb->lcd_clear_display(); |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 281 | for (x=0; x<BOARD_WIDTH; x++) { |
| 282 | for (y=0; y<BOARD_HEIGHT; y++) { |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 283 | switch (board[x][y]) { |
| 284 | case -1: |
| 285 | rb->lcd_fillrect((x*4)+1,y*4,2,4); |
| 286 | rb->lcd_fillrect(x*4,(y*4)+1,4,2); |
| 287 | break; |
| 288 | case 0: |
| 289 | break; |
| 290 | default: |
| 291 | rb->lcd_fillrect(x*4,y*4,4,4); |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | rb->lcd_update(); |
| 297 | } |
| 298 | |
| 299 | void game_pause (void) { |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 300 | int button; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 301 | rb->lcd_clear_display(); |
| 302 | rb->lcd_putsxy(3,12,"Game Paused"); |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 303 | #if CONFIG_KEYPAD == RECORDER_PAD |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 304 | rb->lcd_putsxy(3,22,"[Play] to resume"); |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 305 | #elif CONFIG_KEYPAD == ONDIO_PAD |
Jens Arnold | b8c9a0c | 2004-12-26 14:56:05 +0000 | [diff] [blame] | 306 | rb->lcd_putsxy(3,22,"[Mode] to resume"); |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 307 | #endif |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 308 | rb->lcd_putsxy(3,32,"[Off] to quit"); |
| 309 | rb->lcd_update(); |
| 310 | while (1) { |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 311 | button=rb->button_get(true); |
| 312 | switch (button) { |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 313 | #ifdef SNAKE_RC_QUIT |
| 314 | case SNAKE_RC_QUIT: |
| 315 | #endif |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 316 | case SNAKE_QUIT: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 317 | dead=1; |
| 318 | return; |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 319 | case SNAKE_PLAYPAUSE: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 320 | redraw(); |
| 321 | rb->sleep(HZ/2); |
| 322 | return; |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 323 | default: |
| 324 | if (rb->default_event_handler(button)==SYS_USB_CONNECTED) { |
| 325 | dead=2; |
| 326 | return; |
| 327 | } |
| 328 | break; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void game (void) { |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 335 | int button; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 336 | short x,y; |
| 337 | while (1) { |
| 338 | frame(); |
| 339 | if (dead) |
| 340 | return; |
| 341 | frames++; |
| 342 | if (frames==10) { |
| 343 | frames=0; |
| 344 | if (!apple) { |
| 345 | do { |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 346 | x=rb->rand() % BOARD_WIDTH; |
| 347 | y=rb->rand() % BOARD_HEIGHT; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 348 | } while (board[x][y]); |
| 349 | apple=1; |
| 350 | board[x][y]=-1; |
| 351 | rb->lcd_fillrect((x*4)+1,y*4,2,4); |
| 352 | rb->lcd_fillrect(x*4,(y*4)+1,4,2); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | rb->sleep(HZ/level); |
| 357 | |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 358 | button=rb->button_get(false); |
Shachar Liberman | 5360df9 | 2006-07-30 03:10:09 +0000 | [diff] [blame] | 359 | |
| 360 | #ifdef HAS_BUTTON_HOLD |
| 361 | if (rb->button_hold()) |
| 362 | button = SNAKE_PLAYPAUSE; |
| 363 | #endif |
| 364 | |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 365 | switch (button) { |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 366 | case SNAKE_UP: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 367 | if (dir!=2) dir=0; |
| 368 | break; |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 369 | case SNAKE_RIGHT: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 370 | if (dir!=3) dir=1; |
| 371 | break; |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 372 | case SNAKE_DOWN: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 373 | if (dir!=0) dir=2; |
| 374 | break; |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 375 | case SNAKE_LEFT: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 376 | if (dir!=1) dir=3; |
| 377 | break; |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 378 | #ifdef SNAKE_RC_QUIT |
| 379 | case SNAKE_RC_QUIT: |
| 380 | #endif |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 381 | case SNAKE_QUIT: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 382 | dead=1; |
| 383 | return; |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 384 | case SNAKE_PLAYPAUSE: |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 385 | game_pause(); |
| 386 | break; |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 387 | default: |
| 388 | if (rb->default_event_handler(button)==SYS_USB_CONNECTED) { |
| 389 | dead=2; |
| 390 | return; |
| 391 | } |
| 392 | break; |
| 393 | } |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | void game_init(void) { |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 398 | int selection=0; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 399 | short x,y; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 400 | bool menu_quit = false; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 401 | |
Michiel Van Der Kolk | 40efb12 | 2005-06-05 15:30:27 +0000 | [diff] [blame] | 402 | for (x=0; x<BOARD_WIDTH; x++) { |
| 403 | for (y=0; y<BOARD_HEIGHT; y++) { |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 404 | board[x][y]=0; |
| 405 | } |
| 406 | } |
| 407 | dead=0; |
| 408 | apple=0; |
| 409 | snakelength=4; |
| 410 | score=0; |
| 411 | board[11][7]=1; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 412 | |
| 413 | MENUITEM_STRINGLIST(menu,"Snake Menu",NULL,"Start New Game","Starting Level", |
| 414 | "Quit"); |
| 415 | |
| 416 | while (!menu_quit) { |
Jonathan Gordon | 5ca1539 | 2008-03-26 03:35:24 +0000 | [diff] [blame] | 417 | switch(rb->do_menu(&menu, &selection, NULL, false)) |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 418 | { |
| 419 | case 0: |
| 420 | menu_quit = true; /* start playing */ |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 421 | break; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 422 | |
| 423 | case 1: |
| 424 | rb->set_int("Starting Level", "", UNIT_INT, &level, NULL, |
| 425 | 1, 1, 9, NULL ); |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 426 | break; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 427 | |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 428 | default: |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 429 | dead=1; /* quit program */ |
| 430 | menu_quit = true; |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 431 | break; |
Tom Ross | c7f5ccc | 2007-03-28 07:33:18 +0000 | [diff] [blame] | 432 | |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 433 | } |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 434 | } |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 437 | enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter) |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 438 | { |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 439 | (void)(parameter); |
| 440 | rb = api; |
| 441 | |
| 442 | game_init(); |
| 443 | rb->lcd_clear_display(); |
Jens Arnold | e35a658 | 2004-10-18 21:45:00 +0000 | [diff] [blame] | 444 | game(); |
| 445 | return (dead==1)?PLUGIN_OK:PLUGIN_USB_CONNECTED; |
Björn Stenberg | 973d7a9 | 2003-07-02 21:38:08 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | #endif |