Guest User

Untitled

a guest
Aug 4th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5.  
  6. using namespace std;
  7.  
  8. enum Key { UP = 72, DOWN = 80, LEFT = 75, RIGHT = 77, ESC = 27 };
  9. enum Level { EASY = 20, NORMAL = 10, HARD = 5 };
  10. enum Object { EMPTY = 0, WALL = 1, FOOD = 2 };
  11.  
  12. char arr_sign[9] = { '-','-','-','-','-','-','-','-','-' };
  13.  
  14.  
  15. int main()
  16. {
  17.     srand(time(0));
  18.  
  19.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  20.  
  21.  
  22.     const int raws = 20;
  23.     const int cols = 20;
  24.  
  25.     int array[raws][cols] = {};
  26.     int level = NORMAL;
  27.     int score = 0;
  28.  
  29.     // 0 - EMPTY
  30.     // 1 - WALLS
  31.     // 2 - FOOD
  32.  
  33.     for (int iY = 0; iY < raws; iY++)
  34.     {
  35.         for (int jX = 0; jX < cols; jX++)
  36.         {
  37.             int wallRandom = rand() % level;
  38.             int foodRandom = rand() % 5;
  39.  
  40.             if (iY == 0 || jX == 0 || iY == raws - 1 || jX == cols - 1)
  41.             {
  42.                 array[iY][jX] = WALL;
  43.             }
  44.             else if (wallRandom == 0)
  45.             {
  46.                 array[iY][jX] = WALL;
  47.             }
  48.             else if (foodRandom == 0)
  49.             {
  50.                 array[iY][jX] = FOOD;
  51.             }
  52.         }
  53.     }
  54.  
  55.     int playerX = 5, playerY = 5;
  56.     int key = 0;
  57.     int startTime = time(0);
  58.     int gameTime = 0;
  59.  
  60.     while (true)
  61.     {
  62.         //system("cls");
  63.         SetConsoleCursorPosition(h, { 0, 0 });
  64.         CONSOLE_CURSOR_INFO CURSOR;
  65.         CURSOR.dwSize = 1;
  66.         CURSOR.bVisible = FALSE;
  67.         SetConsoleCursorInfo(h, &CURSOR);
  68.  
  69.         cout << "Score: " << score << "\n";
  70.         cout << "Time: " << gameTime << "\n\n";
  71.  
  72.         for (int iY = 0; iY < raws; iY++)
  73.         {
  74.             for (int jX = 0; jX < cols; jX++)
  75.             {
  76.                 if (iY == playerY && jX == playerX)
  77.                 {
  78.                     cout << "X";
  79.                 }
  80.                 else if (array[iY][jX] == EMPTY)
  81.                 {
  82.                     cout << " ";
  83.                 }
  84.                 else if (array[iY][jX] == WALL)
  85.                 {
  86.                     cout << char(219);
  87.                 }
  88.                 else if (array[iY][jX] == FOOD)
  89.                 {
  90.                     cout << char(249);
  91.                 }
  92.             }
  93.             cout << endl;
  94.         }
  95.  
  96.  
  97.  
  98.         if (_kbhit())
  99.         {
  100.             key = _getch();
  101.         }
  102.         if (key == 224)
  103.         {
  104.             switch (_getch())
  105.             {
  106.             case UP:
  107.                 if (array[playerY - 1][playerX] != WALL)
  108.                 {
  109.                     cout << playerY-- << endl;
  110.                 }
  111.                 break;
  112.             case LEFT:
  113.                 if (array[playerY][playerX - 1] != WALL)
  114.                 {
  115.                     cout << playerX-- << endl;
  116.                 }
  117.                 break;
  118.             case DOWN:
  119.                 if (array[playerY + 1][playerX] != WALL)
  120.                 {
  121.                     cout << playerY++ << endl;
  122.                 }
  123.                 break;
  124.             case RIGHT:
  125.                 if (array[playerY][playerX + 1] != WALL)
  126.                 {
  127.                     cout << playerX++ << endl;
  128.                 }
  129.                 break;
  130.             }
  131.         }
  132.         else if (key == ESC)
  133.         {
  134.             exit(0);
  135.         }
  136.  
  137.  
  138.         if (array[playerY][playerX] == FOOD)
  139.         {
  140.             array[playerY][playerX] = EMPTY;
  141.             score++;
  142.         }
  143.  
  144.         gameTime = time(0) - startTime;
  145.  
  146.         if (gameTime > 120)
  147.         {
  148.             system("color 04");
  149.             exit(0);
  150.         }
  151.  
  152.         key = 0;
  153.     }
  154.  
  155.  
  156.  
  157.     return 0;
  158. }
  159.  
  160.  
Advertisement
Add Comment
Please, Sign In to add comment