candrei98

Untitled

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