Advertisement
Guest User

"Survive"

a guest
Jan 5th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1.  
  2. enum  Direction {
  3.     dir_none,
  4.     dir_left,
  5.     dir_right,
  6.     dir_up,
  7.     dir_down,
  8. };
  9.  
  10. enum CollisionType {
  11.     coll_simple,
  12.     coll_horiz,
  13.     coll_vert,
  14. };
  15.  
  16. struct log {
  17.     int x,
  18.         y,
  19.         dir;
  20. };
  21.  
  22. struct apple {
  23.     int x,
  24.         y;
  25. };
  26.  
  27. struct player {
  28.     int x,
  29.         y,
  30.         dir,
  31.         energy,
  32.         points,
  33.         weird;
  34. };
  35.  
  36. struct entity {
  37.     int x,
  38.         y,
  39.         dir;
  40. };
  41.  
  42. struct wall {
  43.     int left,
  44.         right,
  45.         top,
  46.         bottom;
  47. };
  48.  
  49. void check_walls(void *v, wall w);
  50. int collision(player *p, void *v, int ctype, int length);
  51. void do_keys(player *p);
  52. void do_weird_thing(player *p, apple *a, log *logs);
  53. void draw_apple(apple *a);
  54. void draw_logs(log *logs);
  55. void draw_player(player *p);
  56. void game_loop();
  57. void game_over();
  58. void init_ncurses();
  59. void move_entity(void *v);
  60. void print_surround(void *v, char c);
  61. int rand_pos(int offset, int range);
  62.  
  63.  
  64.  
  65. /*
  66.  * Survive
  67.  * original by: Wilson Perez <wperez274@gmail.com>
  68.  * improved by: Braden Best <bradentbest@gmail.com>
  69.  */
  70.  
  71. #include <ncurses.h>
  72. #include <stdlib.h>
  73. #include <time.h>
  74.  
  75. #include "survive.h"
  76.  
  77. void
  78. check_walls(void *v, wall w)
  79. {
  80.   entity *e = (entity *)v;
  81.  
  82. if ( e->x < w.left )
  83. {
  84.    e->x = w.left;
  85.    e->dir = dir_right;
  86. }
  87.  
  88. if ( e->x > w.right )
  89. {
  90.    e->x = w.right;
  91.    e->dir = dir_left;
  92. }
  93.  
  94. if ( e->y < w.top )
  95. {
  96. e->y = w.top;
  97. e->dir = dir_down;
  98. }
  99.  
  100.     if(e->y > w.bottom){
  101.         e->y = w.bottom;
  102.         e->dir = dir_up;
  103.     }
  104. }
  105.  
  106. int
  107. collision(player *p, void *v, CollisionType ctype, int length)
  108. {
  109.     entity *e = (entity *)v;
  110.  
  111.     switch(ctype){
  112.         case coll_simple:
  113.             return p->x == e->x && p->y == e->y;
  114.         case coll_horiz:
  115.             return p->x >= e->x && p->x <= e->x + length && p->y == e->y;
  116.         case coll_vert:
  117.             return p->y >= e->y && p->y <= e->y + length && p->x == e->x;
  118.     }
  119.  
  120.     return false;
  121. }
  122.  
  123. void
  124. do_keys( player *p )
  125. {
  126.  int ch = getch();
  127.  
  128.  switch (ch)
  129.  {
  130.  case 'a': p->dir = dir_left; break;
  131.  case 'd': p->dir = dir_right; break;
  132.  case 'w': p->dir =
  133. dir_up; break;
  134. case 'z': p->dir =  
  135. dir_down; break;
  136. case 'p':
  137.   mvprintw(0, 0, "PAUSED");
  138.   refresh();
  139. while (1)
  140. {
  141.  ch = getch();
  142.  if (ch == 'p')
  143.  {
  144.    break;
  145.     }
  146.   }
  147.  break;
  148. case 'q': game_over();
  149. break;
  150.     }
  151. }
  152.  
  153. void
  154. do_weird_thing(player *p, apple *a, log *logs)
  155. {
  156.     p->weird++;
  157.     if (p->weird >= rand_pos(20, 20)){
  158.         logs[0].dir = rand_pos(1, 3);
  159.         logs[1].dir = rand_pos(1, 3);
  160.         a->x = rand_pos(1, 29);
  161.         a->y = rand_pos(1, 12);
  162.         p->energy -= 2;
  163.         p->weird = 0;
  164.     }
  165. }
  166.  
  167. void
  168. draw_apple(apple *a)
  169. {
  170.     attrset(COLOR_PAIR(1));
  171.     mvprintw(a->y, a->x, "@");
  172.     attrset(COLOR_PAIR(0));
  173. }
  174.  
  175. void
  176. draw_logs(log *logs)
  177. {
  178.     int i;
  179.  
  180.     attrset(COLOR_PAIR(6));
  181.     mvprintw(logs[0].y, logs[0].x, "             ");
  182.  
  183.     for (i = 0; i < 5; i++)
  184.         mvprintw(logs[1].y + i, logs[1].x, "  ");
  185.  
  186.     attrset(COLOR_PAIR(0));
  187. }
  188.  
  189. void
  190. draw_player(player *p)
  191. {
  192.     attrset(COLOR_PAIR(1));
  193.     mvprintw(p->y, p->x, "O");
  194.  
  195.     attrset(COLOR_PAIR(3));
  196.     mvprintw(0, 0, "Health: %i", p->energy);
  197.     mvprintw(0, 15, "Score: %i", p->points);
  198.  
  199.     attrset(COLOR_PAIR(0));
  200. }
  201.  
  202. void
  203. game_loop()
  204. {
  205.     log logs[2] = {
  206.         { rand_pos(0, 22), rand_pos(0, 3), dir_none },
  207.         { rand_pos(5, 22), rand_pos(2, 5), dir_none }
  208.     };
  209.     player p = { 4, 12, dir_none, 50, 0, 0 };
  210.     apple a = {0};
  211.  
  212.   while (1)
  213.   {
  214.   do_keys(&p);
  215.   attron(A_BOLD);
  216.   erase();
  217.   do_weird_thing(&p, &a, logs);
  218.   move_entity(&p);
  219.   move_entity(&logs[0]);
  220.   move_entity(&logs[1]);
  221.   check_walls(&logs[0], (const wall){ 0, 30, 1, 13 });
  222.   check_walls(&logs[1], (const wall){ 0, 30, 1, 9  });
  223.   check_walls(&p,       (const wall){ 0, 29, 0, 13 });
  224.  
  225. if (collision(&p, &a, coll_simple, 0))
  226. {
  227.    print_surround(&p, 'o');
  228.    p.energy += 2;
  229.    p.points += 10;
  230.             a.x = rand_pos(1, 29);
  231.    a.y = rand_pos(1, 11);
  232.  }
  233.  
  234.    if ( collision(&p, &logs[0], coll_horiz, 12) || collision(&p, &logs[1], coll_vert, 4) )
  235. {
  236.   print_surround(&p, 'x');
  237.   p.energy -= 10;
  238. }
  239.  
  240. if ( p.energy<1 )
  241. {
  242.    game_over();
  243. }
  244.  
  245.  draw_apple(&a);
  246.  draw_player(&p);
  247.  draw_logs(logs);
  248.   }
  249. }
  250.  
  251. void
  252. game_over()
  253. {
  254.     endwin();
  255.     printf("Game over\n");
  256.     exit(0);
  257. }
  258.  
  259. void
  260. init_ncurses()
  261. {
  262.     initscr();
  263.     noecho();
  264.     curs_set(0);
  265.     keypad(stdscr, TRUE);
  266.     timeout(175);  
  267.     start_color();
  268.     bkgd(COLOR_PAIR(1));
  269.     init_pair(1,COLOR_BLUE, COLOR_WHITE);
  270.     init_pair(2,COLOR_BLACK, COLOR_WHITE);
  271.     init_pair(3,COLOR_CYAN, COLOR_BLACK);
  272.     init_pair(4,COLOR_GREEN, COLOR_GREEN);
  273.     init_pair(5,COLOR_RED, COLOR_WHITE);
  274.     init_pair(6,COLOR_BLACK, COLOR_RED);
  275. }
  276.  
  277. void
  278. move_entity(void *v)
  279. {
  280.     entity *e = (entity *)v;
  281.    
  282. switch ( e->dir ){
  283. case dir_left:
  284.  e->x--; break;
  285. case dir_right:
  286.  e->x++; break;
  287. case dir_up:
  288.  e->y--; break;
  289. case dir_down:
  290.  e->y++; break;
  291.     }
  292. }
  293.  
  294. void
  295. print_surround(void *v, char c)
  296. {
  297.     entity *e = (entity *)v;
  298.  
  299.  mvaddch( e->y + -1, e->x +  0, c );
  300.  mvaddch( e->y +  1, e->x +  0, c );
  301.  mvaddch( e->y +  0, e->x + -1, c );
  302.  mvaddch( e->y +  0, e->x +  1, c );
  303. }
  304.  
  305. int
  306. rand_pos( int offset, int range )
  307. {
  308.   return offset + rand() % range;
  309. }
  310.  
  311. int
  312. main()
  313. {
  314.     srand(time(0));
  315.     init_ncurses();
  316.     game_loop();
  317.     game_over();
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement