Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- enum Key { UP = 72, DOWN = 80, LEFT = 75, RIGHT = 77, ESC = 27 };
- enum Level { EASY = 20, NORMAL = 10, HARD = 5 };
- enum Object { EMPTY = 0, WALL = 1, FOOD = 2 };
- char arr_sign[9] = { '-','-','-','-','-','-','-','-','-' };
- int main()
- {
- srand(time(0));
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- const int raws = 20;
- const int cols = 20;
- int array[raws][cols] = {};
- int level = NORMAL;
- int score = 0;
- // 0 - EMPTY
- // 1 - WALLS
- // 2 - FOOD
- for (int iY = 0; iY < raws; iY++)
- {
- for (int jX = 0; jX < cols; jX++)
- {
- int wallRandom = rand() % level;
- int foodRandom = rand() % 5;
- if (iY == 0 || jX == 0 || iY == raws - 1 || jX == cols - 1)
- {
- array[iY][jX] = WALL;
- }
- else if (wallRandom == 0)
- {
- array[iY][jX] = WALL;
- }
- else if (foodRandom == 0)
- {
- array[iY][jX] = FOOD;
- }
- }
- }
- int playerX = 5, playerY = 5;
- int key = 0;
- int startTime = time(0);
- int gameTime = 0;
- while (true)
- {
- //system("cls");
- SetConsoleCursorPosition(h, { 0, 0 });
- CONSOLE_CURSOR_INFO CURSOR;
- CURSOR.dwSize = 1;
- CURSOR.bVisible = FALSE;
- SetConsoleCursorInfo(h, &CURSOR);
- cout << "Score: " << score << "\n";
- cout << "Time: " << gameTime << "\n\n";
- for (int iY = 0; iY < raws; iY++)
- {
- for (int jX = 0; jX < cols; jX++)
- {
- if (iY == playerY && jX == playerX)
- {
- cout << "X";
- }
- else if (array[iY][jX] == EMPTY)
- {
- cout << " ";
- }
- else if (array[iY][jX] == WALL)
- {
- cout << char(219);
- }
- else if (array[iY][jX] == FOOD)
- {
- cout << char(249);
- }
- }
- cout << endl;
- }
- if (_kbhit())
- {
- key = _getch();
- }
- if (key == 224)
- {
- switch (_getch())
- {
- case UP:
- if (array[playerY - 1][playerX] != WALL)
- {
- cout << playerY-- << endl;
- }
- break;
- case LEFT:
- if (array[playerY][playerX - 1] != WALL)
- {
- cout << playerX-- << endl;
- }
- break;
- case DOWN:
- if (array[playerY + 1][playerX] != WALL)
- {
- cout << playerY++ << endl;
- }
- break;
- case RIGHT:
- if (array[playerY][playerX + 1] != WALL)
- {
- cout << playerX++ << endl;
- }
- break;
- }
- }
- else if (key == ESC)
- {
- exit(0);
- }
- if (array[playerY][playerX] == FOOD)
- {
- array[playerY][playerX] = EMPTY;
- score++;
- }
- gameTime = time(0) - startTime;
- if (gameTime > 120)
- {
- system("color 04");
- exit(0);
- }
- key = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment