Advertisement
sebasvp2005

Untitled

May 19th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5. using namespace System;
  6.  
  7. const int WIDTH = 80;
  8. const int HEIGTH = 20;
  9.  
  10. const int LIMITVUELTAS = 5;
  11.  
  12. int main() {
  13.  
  14.  
  15.  
  16.     Console::SetWindowSize(WIDTH, HEIGTH);
  17.     Console::CursorVisible = false;
  18.  
  19.     int x = 10, y = 10;
  20.     int upperbound = 9, lowerbound = 12, leftbound = 9, rightbound = 20;
  21.  
  22.     char dir = 'd';
  23.  
  24.  
  25.     int num_vueltas = 0;
  26.  
  27.    
  28.     bool active = false;
  29.  
  30.     while (true) {
  31.  
  32.         if (_kbhit()) {
  33.             int key = _getch();
  34.             if (key == 'y') active = true;
  35.         }
  36.  
  37.         Console::SetCursorPosition(x, y);
  38.         cout << " ";
  39.  
  40.         if (active && num_vueltas <= LIMITVUELTAS) {
  41.             if (dir == 'd' && y + 1 >= lowerbound) {
  42.                 dir = 'r';
  43.                 leftbound--;
  44.             }
  45.             if (dir == 'r' && x + 1 >= rightbound) {
  46.                 dir = 'u';
  47.                 lowerbound++;
  48.             }
  49.             if (dir == 'u' && y - 1 <= upperbound) {
  50.                 dir = 'l';
  51.                 rightbound++;
  52.             }
  53.             if (dir == 'l' && x - 1 <= leftbound) {
  54.                 dir = 'd';
  55.                 upperbound--;
  56.                 num_vueltas++;
  57.             }
  58.  
  59.             int ny = y, nx = x;
  60.             if (dir == 'u') ny--;
  61.             if (dir == 'l') nx--;
  62.             if (dir == 'r') nx++;
  63.             if (dir == 'd') ny++;
  64.  
  65.             if (ny > upperbound && ny < lowerbound && nx > leftbound && nx < rightbound) {
  66.                 x = nx;
  67.                 y = ny;
  68.             }
  69.         }
  70.  
  71.         Console::SetCursorPosition(x, y);
  72.         cout << "*";
  73.  
  74.         _sleep(25);
  75.  
  76.     }
  77.  
  78.  
  79.     getch();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement