Advertisement
dimasmine88

zmejka

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