Advertisement
Slawcio

My Snake , not complete.

Apr 30th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8. bool gameOver;
  9. const int width = 20;
  10. const int height = 20;
  11. int x, y, fruitX, fruitY, score;
  12. int tailX[100] , tailY[100];
  13. int nTail;
  14. int K[100];
  15. enum eDirecton { STOP = 0 , LEFT , RIGHT, UP, DOWN};
  16. eDirecton dir;
  17.  
  18. void Setup()
  19. {
  20.     gameOver = false;
  21.     dir = STOP;
  22.     x = width /2;
  23.     y = height /2;
  24.     fruitX = rand() % width;
  25.     fruitY = rand() % height;
  26. }
  27. void Draw()
  28. {
  29.     system("cls");
  30.     for (int i = 0; i < width; i++)
  31.         cout << "#";
  32.     cout << endl;
  33.  
  34.     for (int i=0; i < height; i++)
  35.     {
  36.         for (int j = 0; j < width; j++)
  37.         {
  38.             if (j == 0)
  39.                 cout << "#";
  40.             if (i == y && j == x)
  41.                 cout << "0";
  42.             else if (i == fruitY && j == fruitX)
  43.                 cout << "F";
  44.             else
  45.             {
  46.                 for (int k = 0; k < nTail; k++)
  47.                 {
  48.                     if (tailX[k] == j && tailY[k] == i)
  49.                     {
  50.                         cout << "o";
  51.                     }
  52.                 }
  53.             }
  54.                 cout << " ";
  55.  
  56.             if (j == width - 1)
  57.                 cout << "#";
  58.  
  59.         }
  60.         cout << endl;
  61.     }
  62.  
  63.     for (int i = 0; i < width+2; i++)
  64.         cout << "#";
  65.     cout << endl;
  66.     cout << "Score:" << score << endl;
  67. }
  68. void Input()
  69. {
  70.     if (_kbhit())
  71.     {
  72.         switch (_getch())
  73.         {
  74.         case 'a':
  75.             dir = LEFT;
  76.             break;
  77.         case 'd':
  78.             dir = RIGHT;
  79.             break;
  80.         case 'w':
  81.             dir = UP;
  82.             break;
  83.         case 's':
  84.             dir = DOWN;
  85.             break;
  86.         case 'x':
  87.             gameOver = true;
  88.             break;
  89.         }
  90.     }
  91. }
  92. void Logic()
  93. {
  94.      int prevX = tailX[0];
  95.      int prevY = tailY[0];
  96.      int prev2X , prev2Y;
  97.      tailX[0] = x;
  98.      tailY[0] = y;
  99.     for (int i = 1; i < nTail; i++)
  100.     {
  101.         prev2X = tailX[i];
  102.         prev2Y = tailY[i];
  103.         tailX[i] = prevX;
  104.         tailY[i] = prevY;
  105.     }
  106.     switch (dir)
  107.     {
  108.     case LEFT:
  109.         x--;
  110.         break;
  111.     case RIGHT:
  112.         x++;
  113.         break;
  114.     case UP:
  115.         y--;
  116.         break;
  117.     case DOWN:
  118.         y++;
  119.         break;
  120.     }
  121.     if (x > width || x < 0 || y> height || y < 0)
  122.         gameOver = true;
  123.     for (int i = 0; i < nTail; i++)
  124.     {
  125.  
  126.  
  127.         if (tailX[i] == x && tailY[i] == y)
  128.             gameOver
  129.     if (x == fruitX && y == fruitY)
  130.     {
  131.         score +=10;
  132.         fruitX = rand() % width;
  133.         fruitY = rand() % height;
  134.         nTail++;
  135.     }
  136. }
  137. int main()
  138. {       Setup();
  139.         while (!gameOver)
  140.         {
  141.             Draw();
  142.             Input();
  143.             Logic();
  144.             Sleep(10); Sleep(10);
  145.         }
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement