Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Windows API for the console
- */
- #include <iostream>
- #include <windows.h>
- #include <conio.h>
- const char TO_PRINT = 219;
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- COORD coordinates;
- using namespace std;
- int CONSOLE_X_SIZE;
- int CONSOLE_Y_SIZE;
- void moveCursor(int _x, int _y)
- {
- coordinates.X = _x;
- coordinates.Y = _y;
- SetConsoleCursorPosition(console, coordinates);
- }
- void getConsoleSize()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- CONSOLE_X_SIZE = csbi.srWindow.Right - csbi.srWindow.Left;
- CONSOLE_Y_SIZE = csbi.srWindow.Bottom - csbi.srWindow.Top;
- }
- void drawMap(char wall)
- {
- for (int i = 0; i < CONSOLE_Y_SIZE; i++)
- {
- moveCursor(0, i);
- cout << wall;
- }
- for (int i = 0; i < CONSOLE_X_SIZE; i++)
- {
- moveCursor(i, 0);
- cout << wall;
- }
- for (int i = 0; i < CONSOLE_X_SIZE; i++)
- {
- moveCursor(i, CONSOLE_Y_SIZE);
- cout << wall;
- }
- for (int i = 0; i < CONSOLE_Y_SIZE + 1; i++)
- {
- moveCursor(CONSOLE_X_SIZE, i);
- cout << wall;
- }
- }
- bool hitTheWall(int x, int y) {
- return x == CONSOLE_X_SIZE || x == 0 || y == CONSOLE_Y_SIZE || y == 0;
- }
- int main()
- {
- // w AVANTI
- // s INDIETRO
- // d DESTRA
- // a sinistra
- system("COLOR A");
- int command = 'D';
- getConsoleSize();
- drawMap('#');
- int x = CONSOLE_X_SIZE / 2;
- int y = CONSOLE_Y_SIZE / 2;
- moveCursor(x, y);
- cout << "@";
- bool gameRunning = true;
- while (gameRunning)
- {
- if (kbhit())
- {
- command = getch();
- }
- switch (command)
- {
- case 's':
- case 'S':
- y++;
- break;
- case 'a':
- case 'A':
- x--;
- break;
- case 'd':
- case 'D':
- x++;
- break;
- case 'w':
- case 'W':
- y--;
- break;
- }
- system("cls");
- drawMap('#');
- moveCursor(x, y);
- cout << "(" << x << ", " << y << ") HIT THE WALL: " << (hitTheWall(x, y) ? "Yes" : "No");
- if(hitTheWall(x, y)) {
- gameRunning = false;
- }
- Sleep(100);
- }
- system("cls");
- drawMap((int)2);
- moveCursor(CONSOLE_X_SIZE / 2 - 5, CONSOLE_Y_SIZE / 2);
- cout << "GAME OVER";
- system("COLOR C");
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement