TrOoN

Snake

Feb 12th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. */ https://www.facebook.com/fysl.fyslm
  2. */ ked ans | eVilscript | bresco_dz |mosta_team|hacker-fire | hacker_1420| elite-trojen &aLl algerian |
  3.  
  4. */ oroginal by : plewh
  5. ~~ all resv to plewn :D i just share LOOl :p
  6.  
  7. #include <ncurses.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #define SLEEPTIME 125000
  12. #define SCREENWIDTH 60
  13. #define SCREENHEIGHT 20
  14. #define MAXSNAKELENGTH 200
  15.  
  16. struct coords
  17. {
  18. int x;
  19. int y;
  20. };
  21.  
  22. struct snake
  23. {
  24. int alive;
  25. int length;
  26. struct coords segment[MAXSNAKELENGTH];
  27. };
  28.  
  29. void initObjects(struct snake* playa, struct coords* froot)
  30. {
  31. /* Sets initial state of snake object and fruit object
  32. */
  33.  
  34. // INIT SNAKE
  35. int j;
  36. playa->alive = 1;
  37. playa->length = 5;
  38. for(j = 0; j < MAXSNAKELENGTH; ++j)
  39. {
  40. if(j < playa->length)
  41. {
  42. playa->segment[j].x = 30;
  43. playa->segment[j].y = (j + 10);
  44. }
  45. else
  46. {
  47. playa->segment[j].x = 0;
  48. playa->segment[j].y = 0;
  49. }
  50. }
  51.  
  52. // INIT FRUIT
  53. froot->x = 30;
  54. froot->y = 5;
  55. }
  56.  
  57. void drawHUD(int screenWidth, int screenHeight, int score)
  58. {
  59. /* Draws the pretty border around the play field and the score.
  60. * Maybe stats later (can snake have stats?)
  61. */
  62.  
  63. int j;
  64. for(j = 0; j < screenWidth; ++j)
  65. {
  66. mvaddch(1, j, '-');
  67. mvaddch(screenHeight, j, '-');
  68. }
  69. for(j = 1; j <= screenHeight; ++j)
  70. {
  71. mvaddch(j, 0, '|');
  72. mvaddch(j, screenWidth, '|');
  73. }
  74.  
  75. mvprintw(0, 0, "SCORE %d", score);
  76. }
  77.  
  78. int getInput(int direction)
  79. {
  80. /* Pressing keys will buffer the input. getch() will take the character in the
  81. * buffer and place it in keyTemp. Because nodelay() is true, if no key is
  82. * pressed buffer will be NULL. If buffer is NULL or input is not one of the
  83. * macros in the switch, or player attempts to move the snake in the opposite
  84. * direction of travel (back into itself), snake continues in current direction.
  85. */
  86.  
  87. int bufferedKey = getch();
  88. int temp = 0;
  89.  
  90. switch(bufferedKey)
  91. {
  92. case KEY_UP:
  93. temp = 0;
  94. break;
  95. case KEY_RIGHT:
  96. temp = 1;
  97. break;
  98. case KEY_DOWN:
  99. temp = 2;
  100. break;
  101. case KEY_LEFT:
  102. temp = 3;
  103. break;
  104. default:
  105. temp = direction;
  106. break;
  107. }
  108.  
  109. if(temp == 0 && direction == 2)
  110. return direction;
  111. if(temp == 1 && direction == 3)
  112. return direction;
  113. if(temp == 2 && direction == 0)
  114. return direction;
  115. if(temp == 3 && direction == 1)
  116. return direction;
  117.  
  118. return temp;
  119. }
  120.  
  121. void moveSnake(struct snake* player, int direction)
  122. {
  123. /* Not much to comment on, code speaks for itself really.
  124. */
  125.  
  126. int j;
  127. for(j = player->length; j > 0; --j)
  128. {
  129. player->segment[j] = player->segment[j - 1];
  130. }
  131.  
  132. // Figure out where to move head
  133. switch(direction)
  134. {
  135. // UP
  136. case 0:
  137. player->segment[0].y--;
  138. break;
  139. // RIGHT
  140. case 1:
  141. player->segment[0].x++;
  142. break;
  143. // DOWN
  144. case 2:
  145. player->segment[0].y++;
  146. break;
  147. // LEFT
  148. case 3:
  149. player->segment[0].x--;
  150. break;
  151. }
  152. }
  153.  
  154. int fruitEaten(struct snake* player, struct coords* fruit)
  155. {
  156. /* Return true if head and fruit share a space.
  157. */
  158.  
  159. if(player->segment[0].y == fruit->y && player->segment[0].x == fruit->x)
  160. return 1;
  161. return 0;
  162. }
  163.  
  164. void moveFruit(struct coords* fruit)
  165. {
  166. /* Called if fruit gets eaten. Uses rand() from stdlib. Modulo sets MAX_RAND.
  167. * Cases where fruit appears where it shouldn't are caught with the loop and
  168. * re-rolled until they are acceptable.
  169. */
  170.  
  171. fruit->x = rand() % SCREENWIDTH;
  172. fruit->y = rand() % SCREENHEIGHT;
  173.  
  174. while(fruit->x < 1 || fruit->y < 2)
  175. {
  176. fruit->x = rand() % SCREENWIDTH;
  177. fruit->y = rand() % SCREENHEIGHT;
  178. }
  179. }
  180.  
  181. int playerDead(struct snake* player)
  182. {
  183. /* If player hits a wall or hits any part of itself, return true.
  184. */
  185.  
  186. /*** WALLS ***/
  187. if(player->segment[0].y == SCREENHEIGHT || player->segment[0].y == 1)
  188. return 1;
  189. if(player->segment[0].x == SCREENWIDTH || player->segment[0].x == 0)
  190. return 1;
  191.  
  192. /*** SELF ***/
  193. int j;
  194. for(j = 1; j < player->length; ++j)
  195. {
  196. if(player->segment[0].x == player->segment[j].x && player->segment[0].y == player->segment[j].y)
  197. return 1;
  198. }
  199.  
  200. return 0;
  201. }
  202.  
  203. void drawObjects(struct snake* player, struct coords* fruit)
  204. {
  205. /* Draws dynamic objects (snake and fruit) on screen.
  206. */
  207.  
  208. /*** SNAKE ***/
  209. int j;
  210. for(j = 0; j < player->length; j++)
  211. {
  212. if(j == 0) // Draw head
  213. mvaddch(player->segment[j].y, player->segment[j].x, 'S');
  214. else // Draw body
  215. mvaddch(player->segment[j].y, player->segment[j].x, 'O');
  216. }
  217.  
  218. /*** FRUIT ***/
  219. mvaddch(fruit->y, fruit->x, 'F');
  220. }
  221.  
  222. int main(void)
  223. {
  224. /*** Create initial variables ***/
  225. int score = 0;
  226. int direction = 0;
  227. struct snake player;
  228. struct coords fruit;
  229.  
  230. /*** Perform inits ***/
  231. srand(time(NULL)); // Seed timer with time since system boot
  232. initObjects(&player, &fruit);
  233. initscr(); // Created stdscr, a blank canvas that covers the screen.
  234.  
  235. /*** ncurses behaviour toggles ***/
  236. cbreak(); // don't buffer key presses, instead make them available ASAP
  237. curs_set(0); // don't draw cursor
  238. nodelay(stdscr, TRUE); // don't wait for input when getch() is called.
  239. // Return ERR if nothing in buffer. Allows game
  240. // to loop with no input :D
  241. keypad(stdscr, TRUE); // replace keyboard input with ncurses macros for
  242. // arrow keys
  243.  
  244. /*** GAME LOOP ***/
  245. while(player.alive)
  246. {
  247. clear();
  248. drawHUD(SCREENWIDTH, SCREENHEIGHT, score);
  249. direction = getInput(direction);
  250. moveSnake(&player, direction);
  251. if(fruitEaten(&player, &fruit) == 1)
  252. {
  253. moveFruit(&fruit);
  254. ++score;
  255. ++player.length;
  256. }
  257. if(playerDead(&player) == 1)
  258. player.alive = 0;
  259. drawObjects(&player, &fruit);
  260. refresh();
  261. usleep(SLEEPTIME); // prevents game running too fast
  262. }
  263. endwin(); // tell ncurses to clean up after itself.
  264. return 0;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment