root/modules/games/gui_snake.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. draw_element
  2. load_laby
  3. snake_generate_apple
  4. snake_start
  5. game_over
  6. gui_snake_kbd_process
  7. gui_snake_draw
  8. gui_snake_init
  9. basic_module_init

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "keyboard.h"
   4 #include "lang.h"
   5 #include "conf.h"
   6 #include "gui.h"
   7 #include "gui_draw.h"
   8 #include "gui_lang.h"
   9 #include "gui_batt.h"
  10 #include "gui_mbox.h"
  11 #include "modes.h"
  12 
  13 #include "module_def.h"
  14 
  15 void gui_game_menu_kbd_process();
  16 int gui_snake_kbd_process();
  17 void gui_snake_draw();
  18 
  19 gui_handler GUI_MODE_SNAKE = 
  20     /*GUI_MODE_SNAKE*/  { GUI_MODE_MODULE, gui_snake_draw, gui_snake_kbd_process, gui_game_menu_kbd_process, 0, GUI_MODE_FLAG_NODRAWRESTORE };
  21 
  22 //-------------------------------------------------------------------
  23 #define SNAKE_MAX_LENGTH 0xF0
  24 #define SNAKE_ELEMENT_SIZE 8
  25 #define RING_WIDTH 42
  26 #define RING_HEIGHT 24
  27 enum{
  28   DIR_LEFT = 0,
  29   DIR_RIGHT,
  30   DIR_UP,
  31   DIR_DOWN
  32 };
  33 unsigned char ring[RING_WIDTH][RING_HEIGHT];
  34 unsigned short snake_head;
  35 unsigned short snake_tail;
  36 unsigned char snake[SNAKE_MAX_LENGTH][2];
  37 unsigned int points;
  38 unsigned char level;
  39 unsigned char direction;
  40 char str_buf[40];
  41 
  42 const unsigned char labyrinth[4][RING_HEIGHT][RING_WIDTH] = {
  43     {
  44      "                                          ",
  45      "                                          ",
  46      "                                          ",
  47      "                                          ",
  48      "                                          ",
  49      "                                          ",
  50      "                                          ",
  51      "                                          ",
  52      "                                          ",
  53      "                                          ",
  54      "                                          ",
  55      "                                          ",
  56      "                                          ",
  57      "                                          ",
  58      "                                          ",
  59      "                                          ",
  60      "                                          ",
  61      "                                          ",
  62      "                                          ",
  63      "                                          ",
  64      "                                          ",
  65      "                                          ",
  66      "                                          ",
  67      "                                          ",
  68      },
  69     {
  70      "##########################################",
  71      "#                                        #",
  72      "#                                        #",
  73      "#                                        #",
  74      "#     ############                       #",
  75      "#                #                       #",
  76      "#                #                       #",
  77      "#                #########################",
  78      "#                #                       #",
  79      "#                #                       #",
  80      "#                #                       #",
  81      "#                        ######          #",
  82      "#                        #    #          #",
  83      "#            #############    #          #",
  84      "#           #                 #          #",
  85      "#          #                  #          #",
  86      "#         #                   #          #",
  87      "#        #                    #          #",
  88      "#       #                     #          #",
  89      "#       #                     #          #",
  90      "#                             #          #",
  91      "#                             #          #",
  92      "#                             #          #",
  93      "##########################################",
  94      },
  95 
  96     {
  97      "                                          ",
  98      "                                          ",
  99      "                                          ",
 100      "                                          ",
 101      "                                          ",
 102      "                  ##########              ",
 103      "                           #              ",
 104      "                           #              ",
 105      "                           #              ",
 106      "                    ########              ",
 107      "                    #                     ",
 108      "                    #                     ",
 109      "                    #                     ",
 110      "                    #                     ",
 111      "                                          ",
 112      "                    #                     ",
 113      "                                          ",
 114      "                                          ",
 115      "                                          ",
 116      "                                          ",
 117      "                                          ",
 118      "                                          ",
 119      "                                          ",
 120      "                                          ",
 121      },
 122 
 123     {
 124      "                                          ",
 125      "                  #                       ",
 126      "                  #                       ",
 127      "                  #                       ",
 128      "     ##########################           ",
 129      "                           #              ",
 130      "                           #              ",
 131      "            ##########     #              ",
 132      "    #                      ####           ",
 133      "    #                                     ",
 134      "    #                                     ",
 135      "    #                                     ",
 136      "    #                                     ",
 137      "    #                                     ",
 138      "    #################            #        ",
 139      "                                 #        ",
 140      "            #                    #        ",
 141      "     ########                    #        ",
 142      "                                 #        ",
 143      "       ###########################        ",
 144      "                                 #        ",
 145      "                                 #        ",
 146      "                                 #        ",
 147      "                                          ",
 148      },
 149 };
 150 
 151 const char head[8][8] = {
 152   "     GG ",
 153   "    GBBG",
 154   "    G BG",
 155   " GGGGGGG",
 156   "RGGGGGGG",
 157   " GGGGGGG",
 158   "        ",
 159   "        ",
 160 };
 161 
 162 const char tail[8][8] = {
 163   "        ",
 164   "        ",
 165   "        ",
 166   "G GGG GG",
 167   "GGGGGGGG",
 168   "GGG GGG ",
 169   "        ",
 170   "        ",
 171 };
 172 
 173 const char tail2[8][8] = {
 174   "        ",
 175   "        ",
 176   "        ",
 177   " GG     ",
 178   "GGGG    ",
 179   "GGGGG   ",
 180   "  GGG   ",
 181   "  GG    ",
 182 };
 183 
 184 const char apple[8][8] = {
 185   "        ",
 186   " GGGB   ",
 187   "GGG B   ",
 188   " G RRR  ",
 189   "  RRRRR ",
 190   "  RRRRR ",
 191   "  RRRRR ",
 192   "   RRR  ",
 193 };
 194 
 195 const char wall[8][8] = {
 196   "OOOOO OO",
 197   "OOOOO OO",
 198   "        ",
 199   "OO OOOOO",
 200   "OO OOOOO",
 201   "        ",
 202   "OOOOO OO",
 203   "OOOOO OO",
 204 };
 205 
 206 static void draw_element(int x,int y,const char element[8][8],char angle){
 207   x = camera_screen.disp_left + x * SNAKE_ELEMENT_SIZE;
 208   y = y * SNAKE_ELEMENT_SIZE;
 209   int xx,yy;
 210   for(xx=0;xx<SNAKE_ELEMENT_SIZE;xx++)
 211     for(yy=0;yy<SNAKE_ELEMENT_SIZE;yy++){
 212       char c = COLOR_RED;
 213       char col = 0;
 214 
 215       if(angle == 0) col = element[yy][xx];
 216       if(angle == 1) col = element[xx][7-yy];
 217       if(angle == 2) col = element[xx][yy];
 218       if(angle == 3) col = element[yy][7-xx];
 219       if(angle == 4) col = element[7-xx][yy];
 220       switch(col){
 221         case 'R': c = COLOR_RED; break;
 222         case 'G': c = COLOR_GREEN; break;
 223         case 'B': c = COLOR_BLACK; break;
 224         case 'b': c = COLOR_BLUE; break;
 225         case 'Y': c = COLOR_YELLOW; break;
 226         case 'O': c = 0x28; break;
 227         default: c = COLOR_WHITE; break;
 228       };
 229       draw_pixel( x+xx, y+yy, c );
 230     }
 231 }
 232 
 233 
 234 
 235 static void load_laby(int num){
 236   int x,y;
 237   for(x=0;x<RING_WIDTH;x++)
 238     for(y=0;y<RING_HEIGHT;y++){
 239       ring[x][y] = labyrinth[num][y][x];
 240       if(ring[x][y] != ' '){
 241         draw_element(x,y,wall,0);
 242       }else{
 243         draw_rectangle(camera_screen.disp_left + x * SNAKE_ELEMENT_SIZE,
 244                        y * SNAKE_ELEMENT_SIZE,
 245                        camera_screen.disp_left + x * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
 246                        y * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
 247       }
 248     }
 249 }
 250 
 251 static void snake_generate_apple()
 252 {
 253   int x,y,i;
 254   while(1)
 255   {
 256     randomagain:
 257     x = rand() % RING_WIDTH;
 258     y = rand() % RING_HEIGHT;
 259     if(ring[x][y] != ' ') continue;
 260     i = snake_tail;
 261     while(1){
 262       if(snake[i][0] == x && snake[i][1] == y) goto randomagain;
 263       if(i == snake_head) break;
 264       i = (i+1)%SNAKE_MAX_LENGTH;
 265     }
 266     draw_element(x,y,apple,0);
 267     ring[x][y] = 1;
 268     break;
 269   }
 270 }
 271 
 272 static void game_over();
 273 
 274 static void snake_start(){
 275   int i;
 276   load_laby(rand()%4);
 277   snake_head = 3;
 278   snake_tail = 0;
 279   level = 5;
 280   points = 0;
 281   for(i=snake_tail;i<snake_head;i++){
 282     snake[i][0] = 2+i;
 283     snake[i][1] = 2;
 284     draw_rectangle(camera_screen.disp_left + snake[i][0] * SNAKE_ELEMENT_SIZE,
 285                    snake[i][1] * SNAKE_ELEMENT_SIZE,
 286                    camera_screen.disp_left + snake[i][0] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
 287                    snake[i][1] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_RED), RECT_BORDER1|DRAW_FILLED);
 288   }
 289   direction = DIR_RIGHT;
 290   for(i=0;i<10;i++)
 291     snake_generate_apple();
 292 }
 293 
 294 
 295 static void game_over(){
 296     draw_rectangle(camera_screen.disp_left,0,camera_screen.disp_right,camera_screen.height-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
 297     sprintf(str_buf,lang_str(LANG_SNAKE_POINTS),points);
 298     draw_string(camera_screen.disp_left,0,str_buf, MAKE_COLOR(COLOR_WHITE, COLOR_BLUE));
 299     msleep(3000);
 300     snake_start();
 301 }
 302 
 303 int gui_snake_kbd_process() {
 304     switch (kbd_get_autoclicked_key()) {
 305         case KEY_UP:
 306             if(direction != DIR_DOWN) direction = DIR_UP;
 307             break;
 308         case KEY_DOWN:
 309             if(direction != DIR_UP) direction = DIR_DOWN;
 310             break;
 311         case KEY_LEFT:
 312                 if(direction != DIR_RIGHT) direction = DIR_LEFT;
 313             break;
 314         case KEY_RIGHT:
 315                 if(direction != DIR_LEFT) direction = DIR_RIGHT;
 316             break;
 317         case KEY_SET:
 318              
 319              
 320             break;
 321         case KEY_ERASE:
 322         case KEY_DISPLAY:
 323             break;
 324     }
 325     return 0;
 326 }
 327 
 328 void gui_snake_draw() {
 329   unsigned short new_head;
 330   unsigned short new_tail;
 331   unsigned short new_cord;
 332   char game_over_flag = 0,draw_head,clear_tail,draw_points=1;
 333   unsigned char prevdir = direction;
 334   unsigned int i;
 335     new_head = (snake_head+1)%SNAKE_MAX_LENGTH;
 336     new_tail = (snake_tail+1)%SNAKE_MAX_LENGTH;
 337     draw_head = 1;
 338     clear_tail = 1;
 339     if(direction == DIR_RIGHT){
 340       new_cord = (snake[snake_head][0]+1)%RING_WIDTH;
 341       snake[new_head][0] = new_cord;
 342       snake[new_head][1] = snake[snake_head][1];
 343     }
 344     if(direction == DIR_LEFT){
 345       new_cord = snake[snake_head][0]-1;
 346       if(new_cord == 0xFFFF) new_cord = RING_WIDTH-1;
 347       snake[new_head][0] = new_cord;
 348       snake[new_head][1] = snake[snake_head][1];
 349     }
 350     if(direction == DIR_UP){
 351       new_cord = snake[snake_head][1]-1;
 352       if(new_cord == 0xFFFF) new_cord = RING_HEIGHT-1;
 353       snake[new_head][0] = snake[snake_head][0];
 354       snake[new_head][1] = new_cord;
 355     }
 356     if(direction == DIR_DOWN){
 357       new_cord = (snake[snake_head][1]+1)%RING_HEIGHT;
 358       snake[new_head][0] = snake[snake_head][0];
 359       snake[new_head][1] = new_cord;
 360     }
 361 
 362     i = snake_tail;
 363     while(1){
 364       if(snake[i][0] == snake[new_head][0] && snake[i][1] == snake[new_head][1]){
 365         game_over_flag = 1;
 366         break;
 367       }
 368       if(i == snake_head) break;
 369       i = (i+1)%SNAKE_MAX_LENGTH;
 370     }
 371 
 372     if(ring[snake[new_head][0]][snake[new_head][1]] == 1){
 373       ring[snake[new_head][0]][snake[new_head][1]] = ' ';
 374       snake_generate_apple();
 375       points+=level;
 376       draw_points = 1;
 377       if((new_head+1)%SNAKE_MAX_LENGTH != snake_tail){
 378         new_tail = snake_tail;
 379         clear_tail = 0;
 380       }
 381     }else if(ring[snake[new_head][0]][snake[new_head][1]] != ' '){
 382       game_over_flag = 1;
 383     }
 384     if(draw_head){
 385       if(direction == DIR_UP) draw_element(snake[new_head][0],snake[new_head][1],head,2);   //up
 386       if(direction == DIR_LEFT) draw_element(snake[new_head][0],snake[new_head][1],head,0);  //left
 387       if(direction == DIR_DOWN) draw_element(snake[new_head][0],snake[new_head][1],head,1);  //down
 388       if(direction == DIR_RIGHT) draw_element(snake[new_head][0],snake[new_head][1],head,3);  //right
 389 
 390       new_cord = snake_head-1;
 391       if(new_cord == 0xFFFF) new_cord = SNAKE_MAX_LENGTH-1;
 392       if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,2);
 393       else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_LEFT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,0);
 394       else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,4);
 395       else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_RIGHT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,3);
 396       else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_RIGHT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,2);
 397       else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,0);
 398       else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_LEFT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,4);
 399       else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,3);
 400       else if(prevdir == direction && (direction == DIR_RIGHT || direction == DIR_LEFT)) draw_element(snake[snake_head][0],snake[snake_head][1],tail,0);
 401       else if(prevdir == direction && (direction == DIR_UP || direction == DIR_DOWN)) draw_element(snake[snake_head][0],snake[snake_head][1],tail,1);
 402     }
 403     prevdir = direction;
 404     if(clear_tail){
 405         draw_rectangle(camera_screen.disp_left + snake[snake_tail][0] * SNAKE_ELEMENT_SIZE,
 406                        snake[snake_tail][1] * SNAKE_ELEMENT_SIZE,
 407                        camera_screen.disp_left + snake[snake_tail][0] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
 408                        snake[snake_tail][1] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
 409     }
 410     snake_head = new_head;
 411     snake_tail = new_tail;
 412     msleep(100);
 413     if(game_over_flag){
 414       game_over();
 415       game_over_flag = 0;
 416     }
 417     if(draw_points){
 418       draw_points = 0;
 419       sprintf(str_buf,lang_str(LANG_SNAKE_POINTS),points);
 420       draw_string(camera_screen.disp_left + 10,220,str_buf, MAKE_COLOR(COLOR_WHITE, COLOR_BLUE));
 421     }
 422 }
 423 
 424 int gui_snake_init() {
 425     draw_rectangle(camera_screen.disp_left,0,camera_screen.disp_right,camera_screen.height-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
 426     snake_start();
 427     gui_set_mode(&GUI_MODE_SNAKE);
 428     return 1;
 429 }
 430 
 431 int basic_module_init() {
 432   return gui_snake_init();
 433 }
 434 
 435 #include "simple_game.c"
 436 
 437 /******************** Module Information structure ******************/
 438 
 439 ModuleInfo _module_info =
 440 {
 441     MODULEINFO_V1_MAGICNUM,
 442     sizeof(ModuleInfo),
 443     SIMPLE_MODULE_VERSION,      // Module version
 444 
 445     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,         // Requirements of CHDK version
 446     ANY_PLATFORM_ALLOWED,       // Specify platform dependency
 447 
 448     -LANG_MENU_GAMES_SNAKE,     // Module name
 449     MTYPE_GAME,
 450 
 451     &_librun.base,
 452 
 453     ANY_VERSION,                // CONF version
 454     CAM_SCREEN_VERSION,         // CAM SCREEN version
 455     ANY_VERSION,                // CAM SENSOR version
 456     ANY_VERSION,                // CAM INFO version
 457 };
 458 
 459 /*************** END OF AUXILARY PART *******************/

/* [<][>][^][v][top][bottom][index][help] */