Advertisement
utroz

The Snake Game v0.11

Aug 6th, 2011
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.29 KB | None | 0 0
  1. /*
  2. -- This code is: The Snake Game (v 0.11) on Linux Console!
  3. -- Copyright (C) 2011 Utroz - Access: http://Gcoders.wordpress.com
  4. --
  5. -- This program is free software: you can redistribute it and/or modify
  6. -- it under the terms of the GNU General Public License as published by
  7. -- the Free Software Foundation, either version 3 of the License, or
  8. -- (at your option) any later version.
  9. --
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. -- GNU General Public License for more details.
  14. --
  15. -- You should have received a copy of the GNU General Public License
  16. -- along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. -------
  19.  
  20. /*  The Snake Game (v 0.11) on Linux Console!
  21.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  22.  *  File: main.c
  23.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  24. */
  25.  
  26. extern game_start();
  27.  
  28. int main(void)
  29. {
  30.         game_start();
  31.  
  32.         return(0);
  33. }
  34.  
  35. -------
  36. /*  The Snake Game (v 0.11) on Linux Console!
  37.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  38.  *  File: global.h
  39.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  40. */
  41.  
  42. /* Key Cordenates */
  43. #define KEY_UP 'w'
  44. #define KEY_DOWN 's'
  45. #define KEY_LEFT 'a'
  46. #define KEY_RIGHT 'd'
  47.  
  48. /* Map Cordenates */
  49. #define POS_X 0
  50. #define POS_Y 1
  51.  
  52. /* Map Size */
  53. #define ROW 8
  54. #define COLUMN  12
  55.  
  56. /* Map IDS */
  57. #define M 32    //Map ID
  58. #define S 42    //Snake ID
  59. #define A 64    //Apple ID
  60.  
  61. /* Graphics */
  62. #define V 124   //Vertical Graphic
  63. #define H 126   //Horizontal Graphic
  64.  
  65. /* Directions */
  66. #define WE 0    // West
  67. #define EA 1    // East
  68. #define NO 2    // North
  69. #define SO 3    // South
  70.  
  71. /* Snake Status */
  72. #define LIVE 0; // Live
  73. #define DEAD 1; // Dead
  74.  
  75. /* ID's Position
  76.    Row: [0] / Column: [1] */
  77. int snake_pos[99][2];
  78. int apple_pos[2];
  79. #define DEFAULT_X 3
  80. #define DEFAULT_Y 5
  81.  
  82. /* Snake ID's */
  83. #define HEAD 0
  84.  
  85. /* Data Config */
  86. struct {
  87.  
  88.     /* Snake Body Count */
  89.     int count; // (Start on: 0)
  90.  
  91.     /* Snake Status */
  92.     int snake_status, body_found;
  93.  
  94.     /* Log Snake */
  95.     char log[40];
  96.  
  97.     /* Snake Direction */
  98.     int snake_direction;
  99.  
  100.     /* Data User */
  101.     int game_level, points_user;
  102. } config;
  103.  
  104. /* Game Level : Delay */
  105. #define N1 1.4
  106. #define N2 1.2
  107. #define N3 1.0
  108.  
  109. /* Apple Points */
  110. #define POINT 10
  111.  
  112. -------
  113. /*  The Snake Game (v 0.11) on Linux Console!
  114.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  115.  *  File: game.c
  116.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  117. */
  118.  
  119. #include <stdio.h>
  120. #include <stdlib.h>
  121. #include <string.h>
  122. #include "global.h"
  123.  
  124. extern int game_modules(void);
  125. extern void msg_log(char *args);
  126.  
  127. void move_modules(void);
  128. int snake_move(void);
  129. void set_pos(void);
  130. void remove_pos(void);
  131. void remake_body(void);
  132. void create_body(void);
  133. int check_status(void);
  134. int check_body(void);
  135. int check_wall(void);
  136. int check_apple(void);
  137. int create_apple(void);
  138.  
  139. int snake_check = 0;
  140.  
  141. /* Game Map */
  142. char map[ROW][COLUMN] = { //Column |
  143.                 { H, H, H, H, H, H, H, H, H, H, H, H }, //Row -
  144.             { V, M, M, M, M, M, M, M, M, M, M, V },
  145.             { V, M, M, M, M, M, M, M, M, M, M, V },
  146.             { V, M, M, M, M, M, M, M, M, M, M, V },
  147.             { V, M, M, M, M, M, M, M, M, M, M, V },
  148.             { V, M, M, M, M, M, M, M, M, M, M, V },
  149.             { V, M, M, M, M, M, M, M, M, M, M, V },
  150.                 { H, H, H, H, H, H, H, H, H, H, H, H },
  151.             };
  152.  
  153. /* Snake Move Modules */
  154. void move_modules()
  155. {
  156.     /* Remove all Snake IDS on map */
  157.     remove_pos();  
  158.  
  159.     /* If snake head are in body don't walk */
  160.     if(snake_check == 0) remake_body();
  161.    
  162.     /* Return snake condition */
  163.     snake_check = snake_move();
  164.  
  165.     /* If snake head are in wall don't walk! */
  166.     if(check_status())
  167.         set_pos();
  168. }
  169.  
  170. /* Snake Move */
  171. snake_move(void)
  172. {
  173.     int i;
  174.  
  175.     /* Increment snake position. */
  176.     switch(config.snake_direction)
  177.     {
  178.  
  179.         case WE: // West.
  180.             snake_pos[HEAD][POS_Y]--;
  181.             if(check_body() == 1)
  182.             {
  183.                 snake_pos[HEAD][POS_Y]++;
  184.                 return 1;
  185.             }
  186.             break;
  187.  
  188.         case EA: // East.
  189.             snake_pos[HEAD][POS_Y]++;
  190.             if(check_body() == 1)
  191.             {
  192.                 snake_pos[HEAD][POS_Y]--;
  193.                 return 1;
  194.             }
  195.             break;
  196.  
  197.         case NO: // North.
  198.             snake_pos[HEAD][POS_X]--;
  199.             if(check_body() == 1)
  200.             {
  201.                 snake_pos[HEAD][POS_X]++;
  202.                 return 1;
  203.             }  
  204.             break;
  205.  
  206.         case SO: // South.
  207.             snake_pos[HEAD][POS_X]++;
  208.             if(check_body() == 1)
  209.             {
  210.                 snake_pos[HEAD][POS_X]--;
  211.                 return 1;
  212.             }
  213.             break;
  214.         case '1': exit(0);
  215.     }
  216.  
  217.     return 0;  
  218. }
  219.  
  220. /* Set Pos on map. */
  221. void set_pos(void)
  222. {
  223.     int i;
  224.  
  225.     for(i = config.count; i >= 0; i--) 
  226.         map[snake_pos[i][POS_X]][snake_pos[i][POS_Y]] = S; // Snake ID
  227. }
  228.  
  229. /* Remove pos on map */
  230. void remove_pos(void)
  231. {
  232.     int i;
  233.  
  234.     for(i = config.count; i >= 0; i--) 
  235.         map[snake_pos[i][POS_X]][snake_pos[i][POS_Y]] = M; // Map ID
  236. }
  237.  
  238. /* Remake all body parts */
  239. void remake_body(void)
  240. {
  241.     int i;
  242.  
  243.     for(i = config.count; i > 0; i--)
  244.     {
  245.         snake_pos[i][POS_X] = snake_pos[i-1][POS_X];
  246.         snake_pos[i][POS_Y] = snake_pos[i-1][POS_Y];
  247.  
  248.     }
  249. }
  250.  
  251. /* Create a Body */
  252. void create_body(void)
  253. {
  254.     ++config.count;
  255.  
  256.     snake_pos[config.count][POS_X] = snake_pos[config.count-1][POS_X];
  257.     snake_pos[config.count][POS_Y] = snake_pos[config.count-1][POS_Y];     
  258. }
  259.  
  260. /* Check Snake Status */
  261. check_status(void)
  262. {
  263.     if(check_apple() == 1)
  264.     {
  265.         config.points_user += POINT;
  266.         strcpy(config.log, "Apple eat!");
  267.         create_apple();
  268.         create_body();
  269.         return 1;
  270.     }
  271.  
  272.     if(check_wall() == 1)
  273.     {
  274.         strcpy(config.log, "You are dead!");
  275.         config.snake_status = DEAD;
  276.         return 0;
  277.     }
  278.     return 1;
  279. }
  280.  
  281. check_body(void)
  282. {
  283.     int i;
  284.  
  285.     for(i = config.count; i > 0; i--)
  286.     {
  287.         if (snake_pos[HEAD][POS_X] == snake_pos[i][POS_X])
  288.             if(snake_pos[HEAD][POS_Y] == snake_pos[i][POS_Y])
  289.             {
  290.                 config.body_found = 1;
  291.                 strcpy(config.log, "Body Found!");
  292.                 return 1;
  293.             }
  294.     }
  295.     return 0;
  296. }
  297.  
  298. check_wall(void)
  299. {
  300.     // Check if snake are in wall.
  301.     return map[snake_pos[HEAD][POS_X]][snake_pos[HEAD][POS_Y]] != M ? 1 : 0;
  302. }
  303.  
  304. check_apple(void)
  305. {
  306.     // Check if snake apple eat.
  307.     return map[snake_pos[HEAD][POS_X]][snake_pos[HEAD][POS_Y]] == A ? 1 : 0;
  308. }
  309.  
  310. /* Create Apple on Map */
  311. create_apple(void)
  312. {
  313.     srand(time(NULL));
  314.     apple_pos[POS_X] = rand() % (ROW-2) + 1;
  315.     apple_pos[POS_Y] = rand() % (COLUMN-2) + 1;
  316.  
  317.     // Check if apple position is equals to snake.
  318.     int i;
  319.  
  320.     for(i = 0; i <= config.count; i++)
  321.     {
  322.         if(apple_pos[POS_X] == snake_pos[i][POS_X] && apple_pos[POS_Y] == snake_pos[i][POS_Y]) return create_apple();
  323.     }
  324.  
  325.     map[apple_pos[POS_X]][apple_pos[POS_Y]] = A; // Set Apple ID on map.
  326.  
  327.     return (0);
  328. }
  329.  
  330. -------
  331. /*  The Snake Game (v 0.11) on Linux Console!
  332.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  333.  *  File: functions.c
  334.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  335. */
  336.  
  337. #include <stdio.h>
  338. #include <stdlib.h>
  339. #include "global.h"
  340.  
  341. extern void goto_apple(void);
  342. extern char map[ROW][COLUMN];
  343. extern void move_modules(void);
  344. extern void set_key(void);
  345.  
  346. void game_start(void);
  347. void player_data(void);
  348. int game_modules(void);
  349. void msg_log(char *args);
  350. void clean_body(void);
  351. void delay_time(int seconds);
  352. void clean_console(void);
  353. void show_game(void);
  354. void clean_console(void);
  355. void remove_garbage(void);
  356. void game_exit(void);
  357.  
  358. const char game_name[] = ("The Snake Game (v0.11) on Linux Console!"),
  359. author[] = (":: By Utroz(RSC)::");
  360.  
  361. /* Game Start */
  362. void game_start(void)
  363. {
  364.     clean_console();
  365.     player_data();
  366.     printf("\n\t\t%s\n\t\t\t%s\n", game_name, author);
  367.     delay_time(2);
  368.  
  369.     while(1)
  370.     {
  371.         /* Check if snake is dead */
  372.         if(config.snake_status == 1) game_exit();
  373.  
  374.         game_modules();
  375.         printf("\n");
  376.     }
  377. }
  378.  
  379. /* Reset Player Data */
  380. void player_data(void)
  381. {
  382.     config.snake_status = LIVE;
  383.     snake_pos[HEAD][POS_X] = DEFAULT_X;
  384.     snake_pos[HEAD][POS_Y] = DEFAULT_Y;
  385.     create_apple();
  386.     clean_body();
  387.     config.count = 0;
  388.     config.points_user = 0;
  389.     config.snake_direction = WE;
  390.     config.game_level = N3;
  391. }  
  392.  
  393. /* Run all modules of game */
  394. game_modules(void)
  395. {
  396.     clean_console();
  397.     goto_apple();
  398.     move_modules();
  399.     show_game();
  400.     //set_key();
  401.  
  402.     delay_time(config.game_level); // Nivel.
  403.  
  404.     return (0);
  405. }
  406.  
  407. /* Show Logs in Game Screen */
  408. void msg_log(char *args)
  409. {
  410.     printf("\t\t\t:: Last Log: %s ::\n", args);
  411.     strcpy(config.log, "-");
  412.  
  413. }
  414.  
  415. /* Clean all body parts in the map */
  416. void clean_body(void)
  417. {
  418.     int i;
  419.    
  420.     for(i = 1; i <= config.count; i++)
  421.     {
  422.         snake_pos[i][POS_X] = 0;
  423.         snake_pos[i][POS_Y] = 0;
  424.     }
  425. }
  426.  
  427. /* Delay time in snake movement */
  428. void delay_time(int seconds)
  429. {
  430.     // sleep(seconds);
  431.     usleep(seconds * 1000000);
  432. }
  433.  
  434. /* Clean the game image in console */
  435. void clean_console(void)
  436. {
  437.     printf("\e[H\e[2J");
  438. }
  439.  
  440. /* Return the direction in console */
  441. char *direct_msg(void)
  442. {
  443.     if(config.snake_direction == 0) return "West";
  444.     else if(config.snake_direction == 1) return "East";
  445.     else if(config.snake_direction == 2) return "North";
  446.     else return "South";
  447.    
  448.     return NULL;
  449. }
  450.        
  451.  
  452. /* Show Screen of Game. */
  453. void show_game(void)
  454. {
  455.     int level;
  456.     register int i, j;
  457.     char direction[2];
  458.  
  459.     printf("\n\t\t%s\n\t\t\t%s\n", game_name, author);
  460.  
  461.     //if(config.snake_direction == 0) direction
  462.  
  463.     if(config.game_level == N3) level = 3;
  464.     else if(config.game_level == N2) level = 2;
  465.     else level = 1;
  466.  
  467.     printf("\n\t\t\tLevel: %d | Points: %d\n\n", level, config.points_user);
  468.     for(i = 0; i < ROW; i++)
  469.     {
  470.         printf("\t\t%c%c%c%c%c", M, M, M, M, M);
  471.         for(j = 0; j < COLUMN; j++) printf(" %c",map[i][j]);
  472.  
  473.         printf("\n");
  474.     }
  475.     msg_log(config.log);
  476.     printf("\t\tApple Count: %d  / Snake Direction: %s\n", config.count, direct_msg());
  477.     printf("\n\t\tPlease access: http://GCoders.wordpress.com\n");
  478. }  
  479.  
  480. /* Remove Garbage by the map */
  481. void remove_garbage(void)
  482. {
  483.     register int i, j;
  484.     for(i = 1; i < ROW-1; i++)
  485.     {
  486.         for(j = 1; j < COLUMN-1; j++)
  487.         {
  488.             if(map[i][j] != M) map[i][j] = M;
  489.         }
  490.     }      
  491. }
  492.  
  493. /* Snake Dead: game exit */
  494. void game_exit(void)
  495. {
  496.     char choose;
  497.  
  498.     printf("\t\t\t:::::::::::::::::::\n");
  499.     printf("\t\t\t:: You are dead! ::\n");
  500.     printf("\t\t\t:::::::::::::::::::\n");
  501.     delay_time(1);     
  502.  
  503.     while(1)
  504.     {
  505.         printf("\n\t\tYou wanna play other time? (y/n):");
  506.         scanf("%c", &choose);
  507.         fflush(stdin);
  508.  
  509.         switch(choose)
  510.         {
  511.             case 'y':
  512.                 remove_garbage();
  513.                 player_data();
  514.                 game_start();
  515.                 break;
  516.             case 'n':
  517.                 printf("\n\t\tThanks for play the game!\n");
  518.                 delay_time(1); 
  519.                 clean_console();
  520.                 exit(1);
  521.  
  522.                 break;
  523.             default:
  524.                 printf("\n\t\tPlease input a valid option! (y/n)\n");
  525.                 break;
  526.         }
  527.     }
  528. }
  529.  
  530. -------
  531. /*  The Snake Game (v 0.11) on Linux Console!
  532.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  533.  *  File: control.c
  534.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  535. */
  536.  
  537. #include <stdio.h>
  538. #include "global.h"
  539.  
  540. /* Choose a snake direction */
  541. void set_key(void)
  542. {
  543.     char key, ch;
  544.     ch = getchar();
  545.     key = ch;
  546.     fflush(stdin);
  547.  
  548.     switch(key)
  549.     {
  550.         case KEY_UP:
  551.             config.snake_direction = NO;
  552.             break;
  553.         case KEY_DOWN:
  554.             config.snake_direction = SO;
  555.             break;
  556.         case KEY_LEFT:
  557.             config.snake_direction = WE;
  558.             break;
  559.         case KEY_RIGHT:
  560.             config.snake_direction = EA;
  561.             break;
  562.  
  563.     }
  564.  
  565.     if(key != KEY_UP && key != KEY_DOWN && key != KEY_LEFT && key != KEY_RIGHT) set_key();
  566.  
  567.     key = 0;
  568. }
  569.  
  570. -------
  571. /*  The Snake Game (v 0.11) on Linux Console!
  572.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  573.  *  File: ia.c
  574.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  575. */
  576.  
  577. #include "global.h"
  578. #include <stdlib.h>
  579.  
  580. extern char map[ROW][COLUMN];
  581.  
  582. /* Snake Head Pos */
  583. int h_pos_x, h_pos_y;
  584.  
  585.  
  586. void head_pos(void)
  587. {
  588.     h_pos_x = snake_pos[HEAD][POS_X];
  589.     h_pos_y = snake_pos[HEAD][POS_Y];
  590. }
  591.  
  592. /* Make a function to don't found body! */
  593. int goto_apple(void)
  594. {
  595.     head_pos();
  596.  
  597.     if(config.body_found == 1)
  598.     {
  599.         srand(time(NULL));
  600.  
  601.         /* Select a random direction */
  602.         config.snake_direction = rand() % 4;
  603.  
  604.         config.body_found = 0;
  605.         return 0;
  606.  
  607.     }
  608.  
  609.     /* Snake pos is below line of Apple Pos */
  610.     if(h_pos_x > apple_pos[POS_X])
  611.     {
  612.         if(map[(h_pos_x) -1][h_pos_y] != M && map[(h_pos_x) -1][h_pos_y] != A)
  613.    
  614.             if(h_pos_y > apple_pos[POS_Y]) 
  615.                 config.snake_direction = WE;
  616.             else
  617.                 config.snake_direction = EA;
  618.          
  619.         else config.snake_direction = NO;
  620.     }  
  621.  
  622.     /* Snake pos is above line of Apple Pos */
  623.     else if (h_pos_x < apple_pos[POS_X])
  624.     {
  625.         if(map[(h_pos_x) +1][h_pos_y] != M && map[(h_pos_x) +1][h_pos_y] != A)
  626.            
  627.             if(h_pos_y > apple_pos[POS_Y]) 
  628.                 config.snake_direction = WE;
  629.             else
  630.                 config.snake_direction = EA;
  631.  
  632.         else config.snake_direction = SO;
  633.     }
  634.  
  635.     /* Snake pos is equals line of Apple Pos */
  636.     else if (h_pos_x == apple_pos[POS_X])
  637.     {
  638.         /* Apple pos are to left from Snake. */
  639.         if(h_pos_y > apple_pos[POS_Y])
  640.         {
  641.             if(map[h_pos_x][(h_pos_y) -1] != M && map[h_pos_x][(h_pos_y) -1] != A)
  642.  
  643.                 if(map[(h_pos_x) -1][h_pos_y] == V || map[(h_pos_x) -1][h_pos_y] == H) 
  644.                     config.snake_direction = SO;
  645.                 else
  646.                     config.snake_direction = NO;
  647.  
  648.             else config.snake_direction = WE;
  649.         }
  650.  
  651.         /* Apple pos are to right from Snake. */
  652.         else if(h_pos_y < apple_pos[POS_Y])
  653.         {
  654.             if(map[h_pos_x][(h_pos_y) +1] != M && map[h_pos_x][(h_pos_y) +1] != A)         
  655.                 if(map[(h_pos_x) -1][h_pos_y] == V || map[(h_pos_x) -1][h_pos_y] == H) 
  656.                     config.snake_direction = SO;
  657.                 else
  658.                     config.snake_direction = NO;
  659.  
  660.             else config.snake_direction = EA;
  661.         }
  662.     }
  663.     return 0;
  664. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement