Advertisement
Guest User

Snake C++

a guest
Nov 17th, 2019
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.62 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <dos.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. //#pragma warn -wrch
  9. #define MAX      50
  10.  
  11. #define UP_ARROW      72
  12. #define DOWN_ARROW    80
  13. #define LEFT_ARROW    75
  14. #define RIGHT_ARROW   77
  15.  
  16. #define WinMinX       40
  17. #define WinMaxX       600
  18. #define WinMinY       40
  19. #define WinMaxY       440
  20.  
  21. enum Direction {
  22.     Forward,
  23.     Backward,
  24.     Upward,
  25.     Downward
  26. };
  27.  
  28. struct Coord {
  29.     int x, y;
  30. };
  31.  
  32. class Snake;
  33. class Point {
  34.     int x, y, color;
  35.     public:
  36.         Point() {
  37.             set();
  38.         }
  39.  
  40.     void set();
  41.     void draw();
  42.     int getx() {
  43.         return x;
  44.     }
  45.     int gety() {
  46.         return y;
  47.     }
  48.     friend int point_vanished(Point & p, Snake & s);
  49. };
  50.  
  51. class Snake {
  52.     Coord * _Snake;
  53.     int _CurSize, _color, _MaxSize, _Points;
  54.     char _player;
  55.     Direction _Direction;
  56.     public:
  57.         Snake(int size = 20, int color = RED, char player = 'M') {
  58.             _Snake = new Coord[size];
  59.             _CurSize = 3;
  60.             if (player == 'C') {
  61.                 _Snake[0].x = WinMaxX - 10;
  62.                 _Direction = Backward;
  63.             } else {
  64.                 _Snake[0].x = WinMinX + 10;
  65.                 _Direction = Forward;
  66.             }
  67.             //_Snake [0].x = WinMinX + 10;
  68.             _Snake[0].y = WinMinY + 10;
  69.             _color = color;
  70.             _MaxSize = size;
  71.             _player = player;
  72.             _Points = 0;
  73.         }
  74.     void set(int size = 20, int color = RED, char player = 'M') {
  75.         delete _Snake;
  76.         _Snake = new Coord[size];
  77.         _CurSize = 3;
  78.         if (player == 'C') {
  79.             _Direction = Backward;
  80.             _Snake[0].x = WinMaxX - 10;
  81.         } else {
  82.             _Snake[0].x = WinMinX + 10;
  83.             _Direction = Forward;
  84.         }
  85.         _Snake[0].x = WinMinX + 10;
  86.         //_Snake [0].y = WinMinY + 10;
  87.         _color = color;
  88.         _MaxSize = size;
  89.         _player = player;
  90.         _Points = 0;
  91.     }
  92.  
  93.     void change_direction(Direction d);
  94.     void increment();
  95.     void inc_disp();
  96.     void shift_all();
  97.     void display(int color = BLACK);
  98.     void com_play(Point p1);
  99.     friend int point_vanished(Point & p, Snake & s);
  100. };
  101. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  102. void Sound(int s);
  103. void Message_Display(char msg[30], char color);
  104. void show_Header();
  105. void signature();
  106. int menu();
  107. void drawMenu(int selected, int defCol, int selCol);
  108. void show_About();
  109. void show_HowTOPlay();
  110. void show_New();
  111. void Play();
  112. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  113.  
  114. int main() {
  115.  
  116.     int g = DETECT, d;
  117.     initgraph( & g, & d, "d:\tc\bgi");
  118.  
  119.     int selected_option;
  120.  
  121.     Start:
  122.         selected_option = menu();
  123.  
  124.     switch (selected_option) {
  125.     case 1:
  126.         Play();
  127.         goto Start;
  128.     case 2:
  129.         show_HowTOPlay();
  130.         goto Start;
  131.     case 3:
  132.         show_New();
  133.         goto Start;
  134.     case 4:
  135.         show_About();
  136.         goto Start;
  137.     case 5:
  138.         return 1;
  139.     }
  140.     return 1;
  141. }
  142.  
  143. void Snake::increment() {
  144.     //int i;
  145.  
  146.     shift_all();
  147.     if (_Direction == Forward) {
  148.         if (_Snake[0].x >= WinMaxX) {
  149.             _Snake[0].x = WinMinX;
  150.         } else
  151.             _Snake[0].x = _Snake[0].x + 10;
  152.     } else if (_Direction == Backward) {
  153.         if (_Snake[0].x <= WinMinX) {
  154.             _Snake[0].x = WinMaxX;
  155.         } else
  156.             _Snake[0].x = _Snake[0].x - 10;
  157.     } else if (_Direction == Upward) {
  158.         if (_Snake[0].y <= WinMinY) {
  159.             _Snake[0].y = WinMaxY;
  160.         } else
  161.             _Snake[0].y = _Snake[0].y - 10;
  162.     } else if (_Direction == Downward) {
  163.         if (_Snake[0].y >= WinMaxY) {
  164.             _Snake[0].y = WinMinY;
  165.         } else
  166.             _Snake[0].y = _Snake[0].y + 10;
  167.     }
  168. }
  169.  
  170. void Snake::shift_all() {
  171.     int i;
  172.     for (i = _CurSize - 1; i > 0; i--) {
  173.         _Snake[i].x = _Snake[i - 1].x;
  174.         _Snake[i].y = _Snake[i - 1].y;
  175.     }
  176. }
  177.  
  178. void Snake::inc_disp() {
  179.     display(BLACK);
  180.     increment();
  181.     display(_color);
  182. }
  183.  
  184. void Snake::display(int color) {
  185.     setfillstyle(1, color);
  186.     if (color == 0) {
  187.         setcolor(0);
  188.         bar(_Snake[_CurSize - 1].x - 5, _Snake[_CurSize - 1].y - 5, _Snake[_CurSize - 1].x + 5, _Snake[_CurSize - 1].y + 5);
  189.         rectangle(_Snake[_CurSize - 1].x - 5, _Snake[_CurSize - 1].y - 5, _Snake[_CurSize - 1].x + 5, _Snake[_CurSize - 1].y + 5);
  190.         //return ;
  191.     } else {
  192.         setcolor(WHITE);
  193.         for (int i = 0; i < _CurSize; i++) {
  194.             bar(_Snake[i].x - 5, _Snake[i].y - 5, _Snake[i].x + 5, _Snake[i].y + 5);
  195.             rectangle(_Snake[i].x - 5, _Snake[i].y - 5, _Snake[i].x + 5, _Snake[i].y + 5);
  196.         }
  197.  
  198.         /*
  199.         //int i = 0;
  200.         bar ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 , _Snake[i].y+ 5 );
  201.         rectangle ( _Snake[i].x - 5 , _Snake[i].y - 5 , _Snake[i].x + 5 ,_Snake[i].y );
  202.         */
  203.         setfillstyle(1, 0);
  204.         fillellipse(_Snake[0].x, _Snake[0].y, 2, 2);
  205.  
  206.         char msg[50];
  207.         setcolor(WHITE);
  208.  
  209.         if (_player == 'C') {
  210.             bar(250, 12, 630, WinMinY - 10);
  211.             sprintf(msg, "Com Snake at :- ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y, _Points);
  212.             outtextxy(250, 12, msg);
  213.         } else {
  214.             bar(250, 1, 630, WinMinY - 10);
  215.             sprintf(msg, "Ur Snake at :-  ( %d , %d ) Score:- %d", _Snake[0].x, _Snake[0].y, _Points);
  216.             outtextxy(250, 1, msg);
  217.         }
  218.     }
  219. }
  220.  
  221. void Snake::change_direction(Direction d) {
  222.     if ((_Direction == Forward) && (d == Backward)) {
  223.         Sound(-1);
  224.     } else if ((_Direction == Backward) && (d == Forward)) {
  225.         Sound(-1);
  226.     } else if ((_Direction == Upward) && (d == Downward)) {
  227.         Sound(-1);
  228.     } else if ((_Direction == Downward) && (d == Upward)) {
  229.         Sound(-1);
  230.     } else {
  231.         _Direction = d;
  232.         Sound(1);
  233.     }
  234. }
  235.  
  236. void Point::draw() {
  237.     char msg[30];
  238.     setfillstyle(1, color);
  239.     setcolor(YELLOW);
  240.     bar(x - 4, y - 4, x + 4, y + 4);
  241.     rectangle(x - 4, y - 4, x + 4, y + 4);
  242.  
  243.     setfillstyle(1, 0);
  244.     fillellipse(x, y, 2, 2);
  245.  
  246.     bar(1, 1, 300, WinMinY - 10);
  247.     sprintf(msg, "Point at :- ( %d , %d )", x, y);
  248.     outtextxy(40, 1, msg);
  249. }
  250.  
  251. void Point::set() {
  252.     color = random(15) + 1;
  253.     x = random(((WinMaxX - WinMinX) / 10));
  254.     y = random(((WinMaxY - WinMinY) / 10));
  255.     x = (x * 10) + WinMinX;
  256.     y = (y * 10) + WinMinY;
  257.     draw();
  258. }
  259.  
  260. int point_vanished(Point & p, Snake & s) {
  261.     if ((s._Snake[0].x == p.x) && (s._Snake[0].y == p.y)) {
  262.         s._CurSize++;
  263.         if (s._CurSize == s._MaxSize) {
  264.             return 2;
  265.         }
  266.         s.increment();
  267.         s.display(RED);
  268.         Sound(2);
  269.         delay(100);
  270.  
  271.         s._Points = s._Points + 20;
  272.         p.set();
  273.         return 1;
  274.     } else {
  275.         return -1;
  276.     }
  277. }
  278.  
  279. void Sound(int s) {
  280.     if (s == -1) {
  281.         sound(150);
  282.         delay(30);
  283.  
  284.         sound(250);
  285.         delay(30);
  286.         nosound();
  287.     } else if (s == 1) {
  288.         sound(450);
  289.         delay(20);
  290.         nosound();
  291.     } else if (s == 2) {
  292.         sound(650);
  293.         delay(20);
  294.         nosound();
  295.     }
  296.  
  297. }
  298.  
  299. void Snake::com_play(Point p1) {
  300.     if (p1.getx() < _Snake[0].x) {
  301.         if (_Direction == Forward)
  302.             _Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
  303.         else
  304.             _Direction = Backward;
  305.     } else if (p1.getx() > _Snake[0].x) {
  306.         if (_Direction == Backward)
  307.             _Direction = p1.gety() < _Snake[0].y ? Upward : Downward;
  308.         else
  309.             _Direction = Forward;
  310.     } else {
  311.         if (p1.gety() < _Snake[0].y) {
  312.             _Direction = Upward;
  313.         } else if (p1.gety() > _Snake[0].y) {
  314.             _Direction = Downward;
  315.         }
  316.     }
  317. }
  318.  
  319. void Message_Display(char msg[30], char color) {
  320.     settextstyle(1, 0, 5);
  321.     setcolor(8);
  322.     outtextxy(195, 205, msg);
  323.  
  324.     settextstyle(1, 0, 5);
  325.     setcolor(color);
  326.     outtextxy(200, 200, msg);
  327.     delay(1000);
  328. }
  329.  
  330. int menu() {
  331. int ch;
  332. int selected = 1;
  333. int TotalOptions = 5;
  334.  
  335. cleardevice();
  336. setbkcolor(BLUE);
  337. show_Header();
  338. signature();
  339.  
  340. drawMenu(selected, RED, GREEN);
  341. do {
  342.     ch = getch();
  343.     if (ch == DOWN_ARROW) {
  344.         selected = selected >= TotalOptions ? 1 : selected + 1;
  345.         drawMenu(selected, RED, GREEN);
  346.     } else if (ch == UP_ARROW) {
  347.         selected = selected < 2 ? TotalOptions : selected - 1;
  348.         drawMenu(selected, RED, GREEN);
  349.     }
  350.  
  351. } while (ch != '
  352.    ' );
  353.  
  354.     return selected;
  355. }
  356.  
  357. void drawMenu(int selected, int defCol, int selCol) {
  358.     int x = 250;
  359.     int y = 100;
  360.     int width = 150;
  361.     int height = 30;
  362.     int i;
  363.     int TotalOptions = 5;
  364.     char menu_option[5][14] = {
  365.         "    PLAY     ",
  366.         " HOW TO PLAY ",
  367.         " WHAT'S NEW  ",
  368.         "   ABOUT ME  ",
  369.         "     EXIT    "
  370.     };
  371.     setcolor(WHITE);
  372.  
  373.     for (i = 1; i <= TotalOptions; i++) {
  374.         if (i == selected)
  375.             setfillstyle(1, selCol);
  376.         else
  377.             setfillstyle(1, defCol);
  378.         bar(x, y, x + width, y + height);
  379.         rectangle(x, y, x + width, y + height);
  380.         outtextxy(x + 20, y + 10, menu_option[i - 1]);
  381.         y = y + height + 30;
  382.     }
  383. }
  384.  
  385. void show_About() {
  386.     cleardevice();
  387.     setbkcolor(BLACK);
  388.     show_Header();
  389.     setcolor(WHITE);
  390.     settextstyle(0, 0, 0);
  391.  
  392.     signature();
  393.  
  394.     getch();
  395. }
  396.  
  397. void show_HowTOPlay() {
  398.     cleardevice();
  399.     setbkcolor(BLACK);
  400.     show_Header();
  401.     settextstyle(0, 0, 0);
  402.     setcolor(WHITE);
  403.     outtextxy(20, 100, "Objective:");
  404.     outtextxy(20, 150, "Playing:");
  405.     outtextxy(20, 220, "Tip:");
  406.  
  407.     setcolor(LIGHTGREEN);
  408.     outtextxy(120, 120, "To collect 50 boxes before the computer Snake.");
  409.     outtextxy(120, 170, "1. Use arrow keys to control your Snake.");
  410.     outtextxy(120, 180, "2. To collect the box just come near to the BOX.");
  411.     outtextxy(120, 190, "3. Press <ESC> to QUIT any time.");
  412.     outtextxy(120, 240, "1. Use shortcuts to collect the BOX. [ Computer Snake never ");
  413.     outtextxy(120, 250, "   uses shortcut]");
  414.     outtextxy(120, 260, "2. Computer Snake can't Hurt you, so enjoy moving around.");
  415.  
  416.     signature();
  417.  
  418.     getch();
  419. }
  420.  
  421. void signature() {
  422.     outtextxy(350, 400, "WWW ");
  423. }
  424.  
  425. void show_Header() {
  426.     setcolor(RED);
  427.     settextstyle(1, 0, 4);
  428.     outtextxy(193, 27, " SNAKE WAR - I ");
  429.     setcolor(YELLOW);
  430.     outtextxy(195, 25, " SNAKE WAR - I ");
  431. }
  432.  
  433. void show_New() {
  434.     cleardevice();
  435.     setbkcolor(BLACK);
  436.     show_Header();
  437.     settextstyle(0, 0, 0);
  438.     setcolor(WHITE);
  439.  
  440.     outtextxy(20, 100, "What's new");
  441.     outtextxy(20, 150, "What's next");
  442.     outtextxy(20, 260, "When to expect next version");
  443.     outtextxy(20, 320, "Comments, Bugs and Suggestions");
  444.  
  445.     setcolor(LIGHTGREEN);
  446.     outtextxy(70, 120, "Nothing, cos it's the first version. :-)");
  447.  
  448.     outtextxy(70, 170, "In next version of this Game:- ");
  449.     outtextxy(90, 180, " > One or more player will be able to play."
  450.     outtextxy(90, 190, " > You'll be able to select Zero or more computer players."); outtextxy(90, 200, " > You'll be able to PAUSE the Game any time."); outtextxy(90, 210, " > You'll be able to select the color of each snake."); outtextxy(90, 220, " > Keys will be customizable."); outtextxy(90, 230, " > Snakes will be able to Hurt each other.");
  451.  
  452.     outtextxy(70, 280, "Don't worry, i'll mail the code of next version too. [ Very Soon ]");
  453.  
  454.     outtextxy(70, 340, "For any suggestion or comment or Bug report feel free to mail me."); outtextxy(70, 350, "There may be Bugs too in this game, so please let me know them.");
  455.  
  456.     signature(); getch();
  457. }
  458.  
  459. void Play() {
  460.     Snake s1(MAX, GREEN, 'M');
  461.     Snake s2(MAX, MAGENTA, 'C');
  462.  
  463.     char ch, KeyPressed = 0;
  464.  
  465.     cleardevice();
  466.     randomize();
  467.  
  468.     rectangle(WinMinX - 7, WinMinY - 7, WinMaxX + 7, WinMaxY + 7);
  469.     Point p1;
  470.  
  471.     setbkcolor(BLUE);
  472.     s1.inc_disp();
  473.     s2.inc_disp();
  474.  
  475.     setcolor(YELLOW);
  476.     outtextxy(10, 450, "> Collect 50 Boxes to WIN.   > Use shortcuts to WIN.");
  477.     setcolor(CYAN);
  478.     outtextxy(10, 460, "> Use <ESC> to QUIT anytime. > LEFT , RIGHT , UP , DOWN Arrow Keys to Play. ");
  479.     getch();
  480.  
  481.     KeyPressed = 1;
  482.     ch = 'R';
  483.     while (1) {
  484.         while (!kbhit()) {
  485.             s1.inc_disp();
  486.             if (point_vanished(p1, s1) == 2) {
  487.                 Message_Display("YOU WIN ", GREEN);
  488.                 ch = 0x1b;
  489.                 getch();
  490.                 break;
  491.             }
  492.  
  493.             s2.com_play(p1);
  494.             s2.inc_disp();
  495.             if (point_vanished(p1, s2) == 2) {
  496.                 Message_Display("YOU LOSE ", GREEN);
  497.                 ch = 0x1b;
  498.                 getch();
  499.                 break;
  500.             }
  501.  
  502.             delay(100);
  503.             if (KeyPressed == 1) KeyPressed = 0;
  504.         }
  505.         if (ch == 0x1b)
  506.             break;
  507.  
  508.         ch = getch();
  509.         if (KeyPressed == 1) {
  510.             KeyPressed = 0;
  511.             continue;
  512.         }
  513.         if (ch == 0x1b)
  514.             break;
  515.         else if (ch == 0) {
  516.             ch = getch();
  517.             if (ch == UP_ARROW) {
  518.                 s1.change_direction(Upward);
  519.                 KeyPressed = 1;
  520.             } else if (ch == DOWN_ARROW) {
  521.                 s1.change_direction(Downward);
  522.                 KeyPressed = 1;
  523.             } else if (ch == LEFT_ARROW) {
  524.                 s1.change_direction(Backward);
  525.                 KeyPressed = 1;
  526.             } else if (ch == RIGHT_ARROW) {
  527.                 s1.change_direction(Forward);
  528.                 KeyPressed = 1;
  529.             }
  530.         }
  531.     }
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement