Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <thread>
- #include <conio.h>
- int main()
- {
- const char* CSI{ "\033[" };
- const char* hideCursor{ "?25l" };
- const char* clearScreen{ "2J" };
- const int minX{ 1 };
- const int minY{ 1 };
- const int maxX{ 79 };
- const int maxY{ 24 };
- const int kbDeltaY{ 1 };
- std::cout << CSI << hideCursor;
- for (int x{ 1 }, y{ 1 }, dx = { 1 }, dy = { 1 }; ; x += dx, y += dy)
- {
- if (_kbhit())
- {
- auto key{ _getch() };
- if (key == 'w' or key == 'W') { dy = dy <= 0 ? dy : -dy; };
- if (key == 's' or key == 'S') { dy = dy >= 0 ? dy : -dy; };
- if (key == 'a' or key == 'A') { dx = dx <= 0 ? dx : -dx; };
- if (key == 'd' or key == 'S') { dx = dx >= 0 ? dx : -dx; };
- }
- if (y > maxY or y < minY) { dy = dy * (-1); y += dy; } // check vertical limits
- if (x > maxX or x < minX) { dx = dx * (-1); x += dx; } // check horizontal limits
- std::cout << CSI << clearScreen << '\n'; // clear screan before draw each frame
- // ANSI ESCAPE CONTROL SEQUENCE
- // https://ru.wikipedia.org/wiki/%D0%A3%D0%BF%D1%80%D0%B0%D0%B2%D0%BB%D1%8F%D1%8E%D1%89%D0%B8%D0%B5_%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%D0%B4%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D0%BD%D0%BE%D1%81%D1%82%D0%B8_ANSI
- // CSI y;xH - positioned output to y,x coordinates on console
- // CSI NUMBERm - colored output to console
- // CSI 0m - restore defaults after colord output to console
- std::cout << CSI << y << ";" << x << "H" << CSI << 31 << "m" << " * " << CSI << "0m" << '\n';
- std::this_thread::sleep_for(std::chrono::milliseconds(100)); // some delay between frames
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement