Advertisement
Wilson_p_2037

ncurses game: "Snake_8.5" (auto pilot)

Jan 28th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. /*
  2. "Snake_8.5"
  3. Written By: Wilson P.
  4. <wperez274@gmail.com>
  5. */
  6.  
  7.  
  8.  
  9. #include <ncurses.h>
  10. #include <list>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. class Snake
  15. {
  16.  
  17.   private:
  18.     int x, y;
  19.  
  20.   public:
  21.       Snake(int a, int b)
  22.     {
  23.         x = a;
  24.         y = b;
  25.     }
  26.  
  27.     int getX()
  28.     {
  29.         return x;
  30.     }
  31.  
  32.     int getY()
  33.     {
  34.         return y;
  35.     }
  36. };
  37.  
  38. enum Nav
  39. {
  40.  
  41.     LEFT = 1,
  42.     RIGHT = 2,
  43.     UP = 3,
  44.     DOWN = 4,
  45.     LEFT_UP = 5,
  46.     RIGHT_UP = 6,
  47.     LEFT_DOWN = 7,
  48.     RIGHT_DOWN = 8
  49.     };
  50.  
  51.  
  52.  
  53.  
  54. enum Border_limit
  55. {
  56.     MIN_X = -1,
  57.     MAX_X = 32,
  58.     MIN_Y = 1,
  59.     MAX_Y = 15
  60. };
  61.  
  62.  
  63. class Player
  64. {
  65.   public:
  66.  
  67.     int getkey, dir, lives, energy, points, clock;
  68.  
  69. };
  70.  
  71. Player p = {
  72.     -1,
  73.     2,
  74.     3,
  75.     20,
  76.     0,
  77.     0
  78. };
  79.  
  80.  
  81. class Machine
  82. {
  83.   public:
  84.     int dir, x, y;
  85. };
  86.  
  87. Machine spaceship = {
  88.     1,
  89.     500,
  90.     -100
  91. };
  92.  
  93. class Food
  94. {
  95.   public:
  96.     int x, y, clock;
  97. };
  98.  
  99. Food apple = {
  100.     20,
  101.     5,
  102.     0
  103. };
  104.  
  105. void add_color()
  106. {
  107.     start_color();
  108.  
  109.     bkgd(COLOR_PAIR(1));
  110.  
  111.     init_pair(1, COLOR_WHITE, COLOR_BLACK);
  112.     init_pair(2, COLOR_GREEN, COLOR_BLACK);
  113.     init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
  114.     init_pair(4, COLOR_RED, COLOR_BLACK);
  115.     init_pair(5, COLOR_CYAN, COLOR_BLACK);
  116.     init_pair(6, COLOR_WHITE, COLOR_BLUE);
  117.  
  118.  
  119. }
  120.  
  121. void start_both_clocks()
  122. {
  123.     apple.clock++;
  124.     p.clock++;
  125. }
  126.  
  127. void start_cursing()
  128. {
  129.     srand(time(NULL));
  130.     initscr();
  131.     noecho();
  132.     curs_set(0);
  133.     keypad(stdscr, TRUE);
  134.     timeout(200);
  135. }
  136.  
  137. void draw()
  138. {
  139.     attrset(COLOR_PAIR(6));
  140. attron(A_BOLD);
  141.     for (int r = 1; r < 16; r++)
  142.     {
  143.         mvprintw(r, 0, "B");
  144.         mvprintw(r, 32, "B");
  145.     };
  146.  
  147.     mvprintw(1, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
  148.     mvprintw(15, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
  149.     attrset(COLOR_PAIR(1));
  150. attron(A_BOLD);
  151. mvprintw(0, 0, "Lives:%i", p.lives);
  152. mvprintw(0, 20, "Energy:%i", p.energy);
  153. mvprintw(16, 0, "Score:%i", p.points);
  154. mvprintw(16, 15, "Spaceship GPS: X=%i", spaceship.x);
  155. mvprintw(17, 30, "Y=%i", spaceship.y);
  156.     attrset(COLOR_PAIR(4));
  157. attron(A_BOLD);
  158. mvprintw(apple.y, apple.x, "@");
  159.    
  160.    
  161. }
  162.  
  163. void draw_plane()
  164. {
  165.     attrset(COLOR_PAIR(1));
  166.  
  167. attron(A_BOLD); mvprintw(spaceship.y, spaceship.x, "[==]");
  168. }
  169.  
  170. void player_nav()
  171. {
  172.  
  173.     p.getkey = getch();
  174.  
  175.     switch (p.getkey)
  176.     {
  177.     case KEY_LEFT:
  178.     case 'w':
  179.     case 'a':
  180.     case 's':
  181.     case 'd':
  182.         p.dir = LEFT;
  183.         break;
  184.     case KEY_RIGHT:
  185.     case 'i':
  186.     case 'o':
  187.     case 'j':
  188.     case 'k':
  189.     case 'l':
  190.         p.dir = RIGHT;
  191.         break;
  192.     case KEY_UP:
  193.     case 'r':
  194.     case 't':
  195.     case 'y':
  196.     case 'u':
  197.         p.dir = UP;
  198.         break;
  199.     case KEY_DOWN:
  200.     case 'c':
  201.     case 'v':
  202.     case 'b':
  203.     case ' ':
  204.         p.dir = DOWN;
  205.         break;
  206.        
  207.         case 'q':
  208.         p.dir=LEFT_UP;
  209.         break;
  210.         case 'p':
  211.         p.dir=RIGHT_UP;
  212.         break;
  213.         case ',':
  214.         p.dir=LEFT_DOWN;
  215.         break;
  216.         case '.':
  217.         p.dir=RIGHT_DOWN;
  218.         break;
  219.     case 10:
  220.         attrset(COLOR_PAIR(1));
  221. attron(A_BOLD);
  222. mvprintw(5, 12, " PAUSED ");
  223.  
  224.  
  225.         refresh();
  226.         while (1)
  227.         {
  228.             p.getkey = getch();
  229.  
  230.             if (p.getkey == 10)
  231.             {
  232.                 break;
  233.             }
  234.         }
  235.         break;
  236.     }
  237.  
  238. }
  239.  
  240. void game_end()
  241. {
  242.    
  243.     erase();
  244.     timeout(-1);
  245.     attrset(COLOR_PAIR(1));
  246. attron(A_BOLD);
  247.     mvprintw(6, 12, "GAME OVER");
  248.     mvprintw(8, 12, "SCORE: %i", p.points);
  249.     if (p.points<2000)
  250.  mvprintw(10, 12, "Need more practice.");
  251.     if (p.points>=2000 && p.points<=4000)
  252.     mvprintw(10, 12, "Good Game!");
  253.         if (p.points>4000 && p.points<=8000)
  254.     mvprintw(10, 12, "You did fantastic!!!");
  255.     if (p.points>8000)
  256.     mvprintw(10, 12, "You are a Snake Master!!");
  257.    
  258.     refresh();
  259.     getch();
  260.     endwin();
  261. }
  262.  
  263. int main()
  264. {
  265.  
  266.     start_cursing();
  267.     add_color();
  268.  
  269. std::list < Snake > snakes;
  270.     std::list < Snake >::iterator it;
  271.  
  272.  
  273.     for (int i = 0; i < 3; i++)
  274.  
  275. snakes.push_front(Snake(5 + i, 6));
  276.  
  277. while (1)
  278.     {
  279. player_nav();
  280. start_both_clocks();
  281.  
  282. Snake logic = snakes.front();
  283.  
  284.         int x = logic.getX();
  285.         int y = logic.getY();
  286.  
  287. switch (p.dir)
  288.         {
  289.         case LEFT:
  290.             x--;
  291.             break;
  292.         case RIGHT:
  293.             x++;
  294.             break;
  295.         case UP:
  296.             y--;
  297.             break;
  298.         case DOWN:
  299.             y++;
  300.             break;
  301.             case LEFT_UP:
  302.             x--; y--;
  303.             break;
  304.         case RIGHT_UP:
  305.             x++; y--;
  306.             break;
  307.                 case LEFT_DOWN:
  308.             x--; y++;
  309.             break;
  310.         case RIGHT_DOWN:
  311.             x++; y++;
  312.             break;
  313.         }
  314.  
  315.         attron(A_BOLD);
  316.  
  317. snakes.push_front(Snake(x, y));
  318.  
  319.         if (apple.clock >= 25 + rand() % 10 + 1)
  320.         {
  321.             apple.x = 1 + rand() % 29;
  322.             apple.y = 2 + rand() % 11;
  323.             p.energy -= 5;
  324.             apple.clock = 0;
  325.         }
  326.  
  327. if (x == apple.x && y == apple.y)
  328.         {
  329.             apple.x = rand() % 29;
  330.             apple.y = 2 + rand() % 11;
  331.             p.energy += 2 + rand() % 5;
  332.             p.points += 50 + rand() % 100;
  333.             apple.clock = 0;
  334.         }
  335.         else
  336.             snakes.pop_back();
  337.  
  338.         erase();
  339.  
  340.     if (spaceship.y >= MAX_Y)
  341.         {
  342.             spaceship.x = 1 + rand() % 25;
  343.             spaceship.y = rand() % 1 + 2;
  344.         }
  345.  
  346. draw();
  347. if (p.clock >= 2 && p.clock <= 8)
  348.         {
  349.             attrset(COLOR_PAIR(1));
  350.             mvprintw(5, 12, "STAGE 1 ");
  351.         }
  352.         if (p.clock >= 10 && p.clock <= 18)
  353.         {
  354.             attrset(COLOR_PAIR(1));
  355.             mvprintw(2, 9, "Eat Food!");
  356.             mvprintw(apple.y, apple.x - 2, ">");
  357.             mvprintw(apple.y, apple.x + 2, "<");
  358.  
  359.         };
  360.  
  361. if (spaceship.x<=15)
  362.         {
  363. spaceship.dir = DOWN;
  364. draw_plane();
  365. }
  366.   if (spaceship.dir == DOWN)
  367.             spaceship.y++;
  368.              if (spaceship.dir == LEFT)
  369.             spaceship.x--;
  370.            
  371.             if (spaceship.x<=15 && spaceship.y>=-50 && spaceship.y<=-20)
  372.             mvprintw(3,2,"WARNING: ENEMY SHIP IS HERE!");
  373.            
  374.            
  375.   if (x >= spaceship.x && x <= spaceship.x + 3 && y == spaceship.y)
  376.         {
  377.     attrset(COLOR_PAIR(1));
  378. for (int r = 1; r < 12; r++)
  379.             {
  380.                 mvprintw(r, x, ":");
  381.             }
  382.             p.energy -= 1 + rand() % 2;
  383.         }
  384.  
  385.         attrset(A_BOLD);
  386.  
  387.         for (it = snakes.begin(); it != snakes.end(); it++)
  388.         {
  389.  
  390.             attron(A_BOLD);
  391.             attrset(COLOR_PAIR(2));
  392.         attron(A_BOLD); mvprintw((*it).getY(), (*it).getX(), "@");
  393.  
  394.  
  395.             if ((*it).getY() == y && (*it).getX() == x && it != snakes.begin())
  396.             p.energy-=10;
  397.              
  398.              
  399.             if (x < MIN_X){
  400.                 p.dir=RIGHT;
  401.                 p.energy-=5;
  402.             };
  403.     if (x > MAX_X){
  404.                 p.dir=LEFT;
  405.                 p.energy-=5;
  406.             };
  407.             if (y <= MIN_Y){
  408.                 p.dir=DOWN;
  409.                 p.energy-=5;
  410.             };
  411.                 if (y >= MAX_Y){
  412.                 p.dir=UP;
  413.                 p.energy-=5;
  414.             };
  415.     }
  416.  
  417. if (p.energy<1){
  418.     p.energy=30;
  419.     p.lives--;
  420. };
  421. if (p.energy>60){
  422.     p.energy=30;
  423.     p.lives++;
  424. };
  425.  
  426. if (p.lives <= 0)
  427.             game_end();
  428.            
  429.    
  430.        
  431.             if (x<apple.x)
  432.                 p.dir=RIGHT;
  433.             if (x>apple.x)
  434.             p.dir=LEFT;
  435.          if (y<apple.y)
  436.             p.dir=DOWN;
  437.         if (y>apple.y)
  438.             p.dir=UP;
  439.            
  440.            
  441.        
  442.        
  443.            
  444.         }
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement