Advertisement
Guest User

o-lab4

a guest
Dec 12th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <bios.h>
  4. #include <iostream.h>
  5. #include <time.h>
  6.  
  7. const int F3 = 61;
  8. const int F4 = 62;
  9. const int ESC = 27;
  10.  
  11. const int X1 = 20;
  12. const int X2 = 10;
  13. const int Y1 = 20;
  14. const int Y2 = 2;
  15.  
  16. const int DELAY = 1;
  17.  
  18. const char* CAGE = "#";
  19. const char* PRISONER = "$";
  20. const char* SPACE = " ";
  21.  
  22. void delay(int m) {
  23.     int c = clock();
  24.     while(clock()<c+m);
  25. }
  26.  
  27. void draw(int x, int y, const char* symbol) {
  28.     gotoxy(x, y);
  29.     cout << symbol;
  30. }
  31.  
  32. // прерывание 21h функция 07h
  33. int getch_() {
  34.     union REGS in, out;
  35.     in.h.ah = 0x7;
  36.     int86(0x21, &in, &out);
  37.     return out.h.al;
  38. }
  39.  
  40. // прерывание 21h функция 0Bh
  41. int kbhit_() {
  42.     union REGS in, out;
  43.     in.h.ah = 0xb;
  44.     int86(0x21, &in, &out);
  45.     return out.h.al;
  46. }
  47.  
  48. int main() {
  49.     _setcursortype(_NOCURSOR);
  50.     clrscr();
  51.  
  52.     // рисуем границы прямоугольника
  53.     for(int i=X1+1; i>X2-1; i--) {
  54.         draw(i, Y1+1, CAGE);
  55.         draw(i, Y2-1, CAGE);
  56.     }
  57.     for(int j=Y1+1; j>Y2-1; j--) {
  58.         draw(X1+1, j, CAGE);
  59.         draw(X2-1, j, CAGE);
  60.     }
  61.  
  62.     int x = X1;
  63.     int y = Y1;
  64.  
  65.     // начальное положение символа
  66.     draw(x, y, PRISONER);
  67.  
  68.     int key = 0;
  69.  
  70.     while(key!=ESC) {
  71.  
  72.         // для спец. клавиш первый вызов возвращает 0
  73.         // второй вызов возвращает расширенный ASCII код
  74.         key = getch_();
  75.         if (key == 0) key = getch_();
  76.  
  77.         switch(key) {
  78.             case F3:
  79.                 while(kbhit_()==0) {
  80.                     delay(DELAY);
  81.  
  82.                     draw(x, y, SPACE);
  83.  
  84.                     if (x > X2) {
  85.                         x--;
  86.                     } else if (y > Y2) {
  87.                         x = X1;
  88.                         y--;
  89.                     } else {
  90.                         x = X1;
  91.                         y = Y1;
  92.                     }
  93.  
  94.                     draw(x, y, PRISONER);
  95.                 }
  96.                 break;
  97.             case F4:
  98.                 while (kbhit_()==0) {
  99.                     delay(DELAY);
  100.  
  101.                     draw(x, y, SPACE);
  102.  
  103.                     if (x < X1) {
  104.                         x++;
  105.                     } else if (y < Y1) {
  106.                         x = X2;
  107.                         y++;
  108.                     } else {
  109.                         x = X2;
  110.                         y = Y2;
  111.                     }
  112.  
  113.                     draw(x, y, PRISONER);
  114.                 }
  115.                 break;
  116.         }
  117.     }
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement