Advertisement
Vita94

NVitanovic C++ Tutorial 24 - Simple Cross the road game

Jun 3rd, 2018
6,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. #include <vector>
  4. #include <conio.h>
  5. #include <time.h>
  6. using namespace std;
  7. class cPlayer
  8. {
  9. public:
  10.     int x, y;
  11.     cPlayer(int width) { x = width / 2; y = 0; }
  12. };
  13. class cLane
  14. {
  15. private:
  16.     deque<bool> cars;
  17.     bool right;
  18. public:
  19.     cLane(int width)
  20.     {
  21.         for (int i = 0; i < width; i++)
  22.             cars.push_front(true);
  23.         right = rand() % 2;
  24.     }
  25.     void Move()
  26.     {
  27.         if (right)
  28.         {
  29.             if (rand() % 10 == 1)
  30.                 cars.push_front(true);
  31.             else
  32.                 cars.push_front(false);
  33.             cars.pop_back();
  34.         }
  35.         else
  36.         {
  37.             if (rand() % 10 == 1)
  38.                 cars.push_back(true);
  39.             else
  40.                 cars.push_back(false);
  41.             cars.pop_front();
  42.         }
  43.        
  44.     }
  45.     bool CheckPos(int pos) { return cars[pos]; }
  46.     void ChangeDirection() { right = !right; }
  47. };
  48. class cGame
  49. {
  50. private:
  51.     bool quit;
  52.     int numberOfLanes;
  53.     int width;
  54.     int score;
  55.     cPlayer * player;
  56.     vector<cLane*> map;
  57. public:
  58.     cGame(int w = 20, int h = 10)
  59.     {
  60.         numberOfLanes = h;
  61.         width = w;
  62.         quit = false;
  63.         for (int i = 0; i < numberOfLanes; i++)
  64.             map.push_back(new cLane(width));
  65.         player = new cPlayer(width);
  66.     }
  67.     ~cGame()
  68.     {
  69.         delete player;
  70.         for (int i = 0; i < map.size(); i++)
  71.         {
  72.             cLane * current = map.back();
  73.             map.pop_back();
  74.             delete current;
  75.         }
  76.     }
  77.     void Draw()
  78.     {
  79.         system("cls");
  80.         for (int i = 0; i < numberOfLanes; i++)
  81.         {
  82.             for (int j = 0; j < width; j++)
  83.             {
  84.                 if (i == 0 && (j == 0 || j == width - 1)) cout << "S";
  85.                 if (i == numberOfLanes - 1 && (j == 0 || j == width - 1)) cout << "F";
  86.                 if (map[i]->CheckPos(j) && i != 0 && i != numberOfLanes - 1)
  87.                     cout << "#";
  88.                 else if (player->x == j && player->y == i)
  89.                     cout << "V";
  90.                 else
  91.                     cout << " ";
  92.             }
  93.             cout << endl;
  94.         }
  95.         cout << "Score: " << score << endl;
  96.     }
  97.     void Input()
  98.     {
  99.         if (_kbhit())
  100.         {
  101.             char current = _getch();
  102.             if (current == 'a')
  103.                 player->x--;
  104.             if (current == 'd')
  105.                 player->x++;
  106.             if (current == 'w')
  107.                 player->y--;
  108.             if (current == 's')
  109.                 player->y++;
  110.             if (current == 'q')
  111.                 quit = true;
  112.         }
  113.     }
  114.     void Logic()
  115.     {
  116.         for (int i = 1; i < numberOfLanes - 1; i++)
  117.         {
  118.             if (rand() % 10 == 1)
  119.                 map[i]->Move();
  120.             if (map[i]->CheckPos(player->x) && player->y == i)
  121.                 quit = true;
  122.         }
  123.         if (player->y == numberOfLanes - 1)
  124.         {
  125.             score++;
  126.             player->y = 0;
  127.             cout << "\x07";
  128.             map[rand() % numberOfLanes]->ChangeDirection();
  129.         }
  130.     }
  131.     void Run()
  132.     {
  133.         while (!quit)
  134.         {
  135.             Input();
  136.             Draw();
  137.             Logic();
  138.         }
  139.     }
  140. };
  141. int main()
  142. {
  143.     srand(time(NULL));
  144.     cGame game(30, 5);
  145.     game.Run();
  146.     cout << "Game over!" << endl;
  147.     getchar();
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement