Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <WINDOWS.H>
  3. #include <ctime>
  4.  
  5. int main()
  6. {
  7.   unsigned seed = time(nullptr);
  8.   srand(seed);
  9.   const int WID = 20;
  10.   const int HIG = 10;
  11.   char width[WID];
  12.   int posx = WID / 2;
  13.   int posy = HIG - 1;
  14.   int obx = rand() % WID;
  15.   int oby = 2;
  16.   int score = 0;
  17.   bool done = false;
  18.  
  19.   while (!done)
  20.   {
  21.     system("cls");
  22.  
  23.     std::cout << "SCORE: " << score << std::endl;
  24.  
  25.     //MAP BOUNDRIES
  26.     if (posx > WID - 1)
  27.     {
  28.       posx = 0; //leftmost position
  29.     }
  30.     else if (posx < 0)
  31.     {
  32.       posx = WID - 1; //rightmost position
  33.     }
  34.  
  35.     if (oby > HIG)
  36.     {
  37.       oby = 0;
  38.       obx = rand() % WID;
  39.     }
  40.  
  41.     //DRAW
  42.     for (int k = 0; k < HIG; k++)
  43.     {
  44.       for (int i = 0; i < WID; i++)
  45.       {
  46.         if (k == posy && i == posx)
  47.         {
  48.           width[i] = 'O'; //player
  49.         }
  50.         else if (k == oby && i == obx)
  51.         {
  52.           width[i] = 'X'; //object
  53.         }
  54.         else
  55.         {
  56.           width[i] = '_'; //background
  57.         }
  58.         std::cout << width[i];
  59.       }
  60.       std::cout <<std:: endl;
  61.     }
  62.  
  63.     //CONTROLS
  64.     if (GetKeyState('A') < 0)
  65.     {
  66.       posx -= 1;
  67.       Sleep(5);
  68.     }
  69.     else if (GetKeyState('D') < 0)
  70.     {
  71.       posx += 1;
  72.       Sleep(5);
  73.     }
  74.     else if (GetKeyState('Q') < 0)
  75.     {
  76.       done = true;
  77.     }
  78.  
  79.     //OBJECT MOVE
  80.     oby++;
  81.  
  82.     //SCORE
  83.     if (obx == posx && oby == posy)
  84.     {
  85.       score++;
  86.     }
  87.     Sleep(50);
  88.   }
  89.   return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement