Advertisement
Felanpro

Game with setCursorPosition()

Mar 3rd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <conio.h> //Gives us infamous getch()
  6.  
  7. using namespace std;
  8.  
  9. char sheet[10][10];
  10.  
  11. void cls()
  12. {
  13.     // Get the Win32 handle representing standard output.
  14.     // This generally only has to be done once, so we make it static.
  15.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  16.  
  17.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  18.     COORD topLeft = { 0, 0 };
  19.  
  20.     // std::cout uses a buffer to batch writes to the underlying console.
  21.     // We need to flush that to the console because we're circumventing
  22.     // std::cout entirely; after we clear the console, we don't want
  23.     // stale buffered text to randomly be written out.
  24.     std::cout.flush();
  25.  
  26.     // Figure out the current width and height of the console window
  27.     if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
  28.         // TODO: Handle failure!
  29.         abort();
  30.     }
  31.     DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
  32.  
  33.     DWORD written;
  34.  
  35.     // Flood-fill the console with spaces to clear it
  36.     FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
  37.  
  38.     // Reset the attributes of every character to the default.
  39.     // This clears all background colour formatting, if any.
  40.     FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
  41.  
  42.     // Move the cursor back to the top left for the next sequence of writes
  43.     SetConsoleCursorPosition(hOut, topLeft);
  44. }
  45.  
  46. void setCursorPosition(int x, int y)
  47. {
  48.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  49.     std::cout.flush();
  50.     COORD coord = { (SHORT)x, (SHORT)y };
  51.     SetConsoleCursorPosition(hOut, coord);
  52. }
  53.  
  54. void initialize()
  55. {
  56.     for(int x = 0; x <10; x++)
  57.     {
  58.         for(int y = 0; y < 10; y++)
  59.         {
  60.             sheet[x][y] = '*';
  61.         }
  62.     }
  63.  
  64.     sheet[5][5] = 'X'; //This is where the player starts
  65. }
  66.  
  67. void draw()
  68. {
  69.     for(int xx = 0; xx < 10; xx++)
  70.     {
  71.         for(int yy = 0; yy < 10; yy++)
  72.         {
  73.             cout << sheet[xx][yy];
  74.         }
  75.         cout << endl;
  76.     }
  77. }
  78.  
  79. void input_and_update()
  80. {
  81.  
  82.     //Get row and column (position) of player
  83.  
  84.     int c;
  85.     int p;
  86.     int breaking = 0;
  87.     for(c = 0; c < 10; c++)
  88.     {
  89.         for(p = 0; p < 10; p++)
  90.         {
  91.             if(sheet[c][p] == 'X')
  92.             {
  93.                 breaking = 1;
  94.                 break;
  95.             }
  96.         }
  97.         if(breaking == 1)
  98.             break;
  99.     }
  100.  
  101.     breaking = 0;
  102.  
  103.     char variable;
  104.  
  105.     //77 = right arrow
  106.     //72 = up arrow
  107.     //80 down arrow
  108.     //75 = left arrow
  109.     // c = row, p = column
  110.     while(breaking == 0)
  111.     {
  112.         variable = getch();
  113.  
  114.         if(variable == 75)
  115.         {
  116.             if(p - 1 < 0)
  117.             {
  118.                 ;
  119.             }
  120.             else
  121.             {
  122.                 sheet[c][p] = '*';
  123.                 setCursorPosition(p, c);
  124.                 cout << "*";
  125.                 sheet[c][p - 1] = 'X';
  126.                 setCursorPosition((p - 1), c);
  127.                 cout << "X";
  128.                 setCursorPosition(0, 11);
  129.                 breaking = 1;
  130.             }
  131.         }
  132.         else if(variable == 72)
  133.         {
  134.             if(c - 1 < 0)
  135.             {
  136.                 ;
  137.             }
  138.             else
  139.             {
  140.                 sheet[c][p] = '*';
  141.                 setCursorPosition(p, c);
  142.                 cout << "*";
  143.                 sheet[c - 1][p] = 'X';
  144.                 setCursorPosition(p, c - 1);
  145.                 cout << "X";
  146.                 setCursorPosition(0, 11);
  147.                 breaking = 1;
  148.             }
  149.         }
  150.         else if(variable == 77)
  151.         {
  152.             if(p + 1 > 9)
  153.             {
  154.                 ;
  155.             }
  156.             else
  157.             {
  158.                 sheet[c][p] = '*';
  159.                 setCursorPosition(p, c);
  160.                 cout << "*";
  161.                 sheet[c][p + 1] = 'X';
  162.                 setCursorPosition((p + 1), c);
  163.                 cout << "X";
  164.                 setCursorPosition(0, 11);
  165.                 breaking = 1;
  166.             }
  167.         }
  168.         else if(variable == 80)
  169.         {
  170.             if(c + 1 > 9)
  171.             {
  172.                 ;
  173.             }
  174.             else
  175.             {
  176.                 sheet[c][p] = '*';
  177.                 setCursorPosition(p, c);
  178.                 cout << "*";
  179.                 sheet[c + 1][p] = 'X';
  180.                 setCursorPosition(p, c + 1);
  181.                 cout << "X";
  182.                 setCursorPosition(0, 11);
  183.                 breaking = 1;
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189. int main()
  190. {
  191.     initialize(); // Create play sheet
  192.     draw();
  193.     while(1)
  194.         input_and_update();
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement