Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

222 řádky
6.7 KiB

  1. /*
  2. * conio-snake snake game using the <conio.h> API
  3. * Copyright (c) 2003-2004 Simon Huggins <webmaster#simonhuggins.com>
  4. * All Rights Reserved
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <caca_conio.h>
  21. /* prototypes */
  22. void draw_line(int col, int row);
  23. void show_score();
  24. void add_segment();
  25. void setup_level();
  26. /* constants */
  27. const int maxrow=15, maxcol=77;
  28. const int snake_start_col=33,snake_start_row=7;
  29. const char up_key='a', down_key='z', left_key='o', right_key='p';
  30. const int pause_length=500000;
  31. /* global variables */
  32. int score, snake_length, speed, obstacles, level, firstpress, high_score=0;
  33. char screen_grid[maxrow][maxcol];
  34. char direction = right_key;
  35. struct snake_segment {
  36. int row,col;
  37. } snake[100];
  38. int main()
  39. {
  40. /* Variable declarations within main() only */
  41. char keypress;
  42. do /* restart game loop */
  43. {
  44. obstacles=4; level=1; score=0; speed=14;
  45. //randomize(); /* Ensure random seed initiated */
  46. setup_level();
  47. /* main loop */
  48. do
  49. {
  50. delay(speed * pause_length / 50000); /*pause*/
  51. /* If key has been hit, then check it is a direction key - if so,
  52. change direction */
  53. if (kbhit())
  54. {
  55. keypress=(char)getch();
  56. if((keypress==right_key)||(keypress==left_key)||
  57. (keypress==up_key)||(keypress==down_key))
  58. direction = keypress;
  59. }
  60. /* Add a segment to the end of the snake */
  61. add_segment();
  62. /* Blank last segment of snake */
  63. gotoxy(snake[0].col,snake[0].row);
  64. cprintf(" ");
  65. /* ... and remove it from the array */
  66. for (int i=1;i<=snake_length;i++)
  67. snake[i-1]=snake[i];
  68. /* Display snake in yellow */
  69. textcolor(YELLOW);
  70. for (int i=0;i<=snake_length;i++)
  71. {
  72. gotoxy(snake[i].col,snake[i].row);
  73. cprintf("O");
  74. }
  75. /* keeps cursor flashing in one place instead of following snake */
  76. gotoxy(1,1);
  77. /* If first press on each level, pause until a key is pressed */
  78. if (firstpress) { while(!kbhit()); firstpress = 0; }
  79. /* Collision detection - walls (bad!) */
  80. if ((snake[snake_length-1].row>maxrow+1)||(snake[snake_length-1].row<=1)||
  81. (snake[snake_length-1].col>maxcol+1)||(snake[snake_length-1].col<=1)||
  82. /* Collision detection - obstacles (bad!) */
  83. (screen_grid[snake[snake_length-1].row-2][snake[snake_length-1].col-2]=='x'))
  84. keypress='x'; /* i.e. exit loop - game over */
  85. /* Collision detection - snake (bad!) */
  86. for (int i=0;i<snake_length-1;i++)
  87. if ( (snake[snake_length-1].row)==(snake[i].row) &&
  88. (snake[snake_length-1].col)==(snake[i].col))
  89. {
  90. keypress='x'; /* i.e. exit loop - game over */
  91. break; /* no need to check any more segments */
  92. }
  93. /* Collision detection - food (good!) */
  94. if (screen_grid[snake[snake_length-1].row-2][snake[snake_length-1].col-2]=='.')
  95. {
  96. /* increase score and length of snake */
  97. score+=snake_length*obstacles; show_score(); snake_length++; add_segment();
  98. /* if length of snake reaches certain size, onto next level */
  99. if (snake_length==(level+3)*2)
  100. {
  101. score+=level*1000; obstacles+=2; level++; /* add to obstacles */
  102. if ((level%5==0)&&(speed>1)) speed--; /* increase speed every 5 levels */
  103. setup_level(); /* display next level */
  104. }
  105. }
  106. } while (keypress!='x');
  107. /* game over message */
  108. if (score > high_score) high_score = score;
  109. show_score();
  110. gotoxy(30,6); textcolor(LIGHTRED); cprintf("G A M E O V E R");
  111. gotoxy(30,9); textcolor(YELLOW); cprintf("Another Game (y/n)? ");
  112. do keypress=getch(); while((keypress!='y')&&(keypress!='n'));
  113. } while (keypress=='y');
  114. }
  115. void setup_level()
  116. {
  117. /* variables local to setup_level() */
  118. int row,col;
  119. /* Set up global variables for new level */
  120. snake_length=level+4; direction = right_key;
  121. firstpress = 1;
  122. /* Fill grid with blanks */
  123. for(row=0;row<maxrow;row++)
  124. for(col=0;col<maxcol;col++)
  125. screen_grid[row][col]= ' ';
  126. /* Fill grid with Xs and food */
  127. for(int i=0;i<obstacles*2;i++)
  128. {
  129. row= rand()%maxrow;
  130. col= rand()%maxcol;
  131. if(i<obstacles)
  132. screen_grid[row][col]='x';
  133. else
  134. screen_grid[row][col]='.';
  135. }
  136. /* Create snake array of length snake_length */
  137. for(int i=0;i<snake_length;i++)
  138. {
  139. snake[i].row=snake_start_row;
  140. snake[i].col=snake_start_col+i;
  141. }
  142. /* Draw playing board */
  143. draw_line(1,1);
  144. for(row=0;row<maxrow;row++)
  145. {
  146. gotoxy(1,row+2);
  147. textcolor(LIGHTBLUE); cprintf("|");
  148. textcolor(WHITE);
  149. for(col=0;col<maxcol;col++)
  150. cprintf("%c",screen_grid[row][col]);
  151. textcolor(LIGHTBLUE);
  152. cprintf("|");
  153. }
  154. draw_line(1,maxrow+2);
  155. show_score();
  156. gotoxy(2,maxrow+5);
  157. textcolor(LIGHTRED);
  158. cprintf("~~ SNAKE GAME~~ Left: %c, Right: %c, Up: %c, Down: %c, Exit: x. Any key to start.",
  159. left_key,right_key,up_key,down_key);
  160. }
  161. void draw_line(int col, int row)
  162. {
  163. gotoxy(col,row); textcolor(LIGHTBLUE);
  164. for (col=0;col<maxcol+2;col++) cprintf("=");
  165. }
  166. void show_score()
  167. {
  168. textcolor(LIGHTCYAN);
  169. gotoxy(2,maxrow+3);
  170. cprintf("Level: %05d",level);
  171. gotoxy(40,maxrow+3);
  172. textcolor(LIGHTGREEN);
  173. cprintf("Score: %05d",score);
  174. gotoxy(60,maxrow+3);
  175. textcolor(LIGHTMAGENTA);
  176. cprintf("High Score: %05d",high_score);
  177. }
  178. void add_segment()
  179. {
  180. switch(direction)
  181. {
  182. case(right_key): snake[snake_length].row=snake[snake_length-1].row;
  183. snake[snake_length].col=snake[snake_length-1].col+1;
  184. break;
  185. case(left_key) : snake[snake_length].row=snake[snake_length-1].row;
  186. snake[snake_length].col=snake[snake_length-1].col-1;
  187. break;
  188. case(up_key) : snake[snake_length].row=snake[snake_length-1].row-1;
  189. snake[snake_length].col=snake[snake_length-1].col;
  190. break;
  191. case(down_key) : snake[snake_length].row=snake[snake_length-1].row+1;
  192. snake[snake_length].col=snake[snake_length-1].col;
  193. }
  194. }