Advertisement
informaticage

Console game development

Feb 15th, 2021
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. /*
  2.     Windows API for the console
  3. */
  4.  
  5. #include <iostream>
  6. #include <windows.h>
  7. #include <conio.h>
  8.  
  9. const char TO_PRINT = 219;
  10. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  11. COORD coordinates;
  12. using namespace std;
  13.  
  14. int CONSOLE_X_SIZE;
  15. int CONSOLE_Y_SIZE;
  16.  
  17. void moveCursor(int _x, int _y)
  18. {
  19.     coordinates.X = _x;
  20.     coordinates.Y = _y;
  21.     SetConsoleCursorPosition(console, coordinates);
  22. }
  23.  
  24. void getConsoleSize()
  25. {
  26.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  27.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
  28.     CONSOLE_X_SIZE = csbi.srWindow.Right - csbi.srWindow.Left;
  29.     CONSOLE_Y_SIZE = csbi.srWindow.Bottom - csbi.srWindow.Top;
  30. }
  31.  
  32. void drawMap(char wall)
  33. {
  34.     for (int i = 0; i < CONSOLE_Y_SIZE; i++)
  35.     {
  36.         moveCursor(0, i);
  37.         cout << wall;
  38.     }
  39.  
  40.     for (int i = 0; i < CONSOLE_X_SIZE; i++)
  41.     {
  42.         moveCursor(i, 0);
  43.         cout << wall;
  44.     }
  45.  
  46.     for (int i = 0; i < CONSOLE_X_SIZE; i++)
  47.     {
  48.         moveCursor(i, CONSOLE_Y_SIZE);
  49.         cout << wall;
  50.     }
  51.  
  52.     for (int i = 0; i < CONSOLE_Y_SIZE + 1; i++)
  53.     {
  54.         moveCursor(CONSOLE_X_SIZE, i);
  55.         cout << wall;
  56.     }
  57. }
  58.  
  59. bool hitTheWall(int x, int y) {
  60.     return x == CONSOLE_X_SIZE || x == 0 || y == CONSOLE_Y_SIZE || y == 0;
  61. }
  62.  
  63. int main()
  64. {
  65.     // w AVANTI
  66.     // s INDIETRO
  67.     // d DESTRA
  68.     // a sinistra
  69.     system("COLOR A");
  70.  
  71.     int command = 'D';
  72.     getConsoleSize();
  73.  
  74.     drawMap('#');
  75.  
  76.     int x = CONSOLE_X_SIZE / 2;
  77.     int y = CONSOLE_Y_SIZE / 2;
  78.     moveCursor(x, y);
  79.     cout << "@";
  80.  
  81.     bool gameRunning = true;
  82.     while (gameRunning)
  83.     {
  84.         if (kbhit())
  85.         {
  86.             command = getch();
  87.         }
  88.  
  89.         switch (command)
  90.         {
  91.         case 's':
  92.         case 'S':
  93.             y++;
  94.             break;
  95.  
  96.         case 'a':
  97.         case 'A':
  98.             x--;
  99.             break;
  100.  
  101.         case 'd':
  102.         case 'D':
  103.             x++;
  104.             break;
  105.  
  106.         case 'w':
  107.         case 'W':
  108.             y--;
  109.             break;
  110.         }
  111.  
  112.         system("cls");
  113.         drawMap('#');
  114.         moveCursor(x, y);
  115.         cout << "(" << x << ", " << y << ") HIT THE WALL: " << (hitTheWall(x, y) ? "Yes" : "No");
  116.         if(hitTheWall(x, y)) {
  117.             gameRunning = false;
  118.         }
  119.         Sleep(100);
  120.     }
  121.  
  122.     system("cls");
  123.     drawMap((int)2);
  124.     moveCursor(CONSOLE_X_SIZE / 2 - 5, CONSOLE_Y_SIZE / 2);
  125.     cout << "GAME OVER";
  126.     system("COLOR C");
  127.     cin.get();
  128.     return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement