Advertisement
gashink_t

class_human(lab_1, VP)

Feb 16th, 2021
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <conio.h>
  4. using namespace std;
  5. #define N 20
  6. bool gameOver;
  7.  
  8. class human
  9. {
  10. private:
  11.     int age = 0, height = 0, weight = 0, x_init = N/2, y_init = N/2;
  12.  
  13. public:
  14.     int x = x_init;
  15.     int y = y_init;
  16.     float d = 0;
  17.  
  18.     void create(int a, int h, int w)
  19.     {
  20.         age = a;
  21.         height = h;
  22.         weight = w;
  23.     }
  24.  
  25.     void print()
  26.     {
  27.         cout << "Age = " << age << "\tHeight = " << height << "\tweight = " << weight << endl;
  28.     }
  29.  
  30.     void draw()
  31.     {
  32.         system("cls");
  33.         for (int i = 0; i < N; i++)
  34.         {
  35.             for (int j = 0; j < N; j++)
  36.             {
  37.                 if (i == x && j == y)
  38.                     cout << "0";
  39.                 else cout << ".";
  40.             }
  41.             cout << endl;
  42.         }
  43.     }
  44.  
  45.     void play()
  46.     {
  47.         switch (_getch())
  48.         {
  49.         case 'w':
  50.             if (x > 0)
  51.                 x--;
  52.             break;
  53.         case 's':
  54.             if (x < N - 1)
  55.                 x++;
  56.             break;
  57.         case 'a':
  58.             if (y > 0)
  59.                 y--;
  60.             break;
  61.         case 'd':
  62.             if (y < N - 1)
  63.                 y++;
  64.             break;
  65.         case 'z':
  66.             gameOver = true;
  67.         }
  68.     }
  69.  
  70.     void calc_d()
  71.     {
  72.         d = sqrt(pow(x - x_init, 2) + pow(y - y_init, 2));
  73.     }
  74.  
  75.     void print_d()
  76.     {
  77.         cout << "\nDistance = " << d;
  78.     }
  79. };
  80.  
  81.  
  82. int main()
  83. {
  84.     human h;
  85.     h.create(19, 164, 50);
  86.     h.print();
  87.     cout << "Game Management:\n'W' - UP\n'S' - DOWN\n'D' - RIGHT\n'A' - LEFT\n'Z' - THE END\n";
  88.     cout << "To start the game, press any key!\n";
  89.     _getch();
  90.     gameOver = false;
  91.     while (!gameOver)
  92.     {
  93.         h.draw();
  94.         h.play();
  95.     }
  96.    
  97.     h.calc_d();
  98.     h.print_d();
  99.  
  100.     return 0;
  101. }
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement