Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <bios.h>
- #include <iostream.h>
- #include <time.h>
- const int F3 = 61;
- const int F4 = 62;
- const int ESC = 27;
- const int X1 = 20;
- const int X2 = 10;
- const int Y1 = 20;
- const int Y2 = 2;
- const int DELAY = 1;
- const char* CAGE = "#";
- const char* PRISONER = "$";
- const char* SPACE = " ";
- void delay(int m) {
- int c = clock();
- while(clock()<c+m);
- }
- void draw(int x, int y, const char* symbol) {
- gotoxy(x, y);
- cout << symbol;
- }
- // прерывание 21h функция 07h
- int getch_() {
- union REGS in, out;
- in.h.ah = 0x7;
- int86(0x21, &in, &out);
- return out.h.al;
- }
- // прерывание 21h функция 0Bh
- int kbhit_() {
- union REGS in, out;
- in.h.ah = 0xb;
- int86(0x21, &in, &out);
- return out.h.al;
- }
- int main() {
- _setcursortype(_NOCURSOR);
- clrscr();
- // рисуем границы прямоугольника
- for(int i=X1+1; i>X2-1; i--) {
- draw(i, Y1+1, CAGE);
- draw(i, Y2-1, CAGE);
- }
- for(int j=Y1+1; j>Y2-1; j--) {
- draw(X1+1, j, CAGE);
- draw(X2-1, j, CAGE);
- }
- int x = X1;
- int y = Y1;
- // начальное положение символа
- draw(x, y, PRISONER);
- int key = 0;
- while(key!=ESC) {
- // для спец. клавиш первый вызов возвращает 0
- // второй вызов возвращает расширенный ASCII код
- key = getch_();
- if (key == 0) key = getch_();
- switch(key) {
- case F3:
- while(kbhit_()==0) {
- delay(DELAY);
- draw(x, y, SPACE);
- if (x > X2) {
- x--;
- } else if (y > Y2) {
- x = X1;
- y--;
- } else {
- x = X1;
- y = Y1;
- }
- draw(x, y, PRISONER);
- }
- break;
- case F4:
- while (kbhit_()==0) {
- delay(DELAY);
- draw(x, y, SPACE);
- if (x < X1) {
- x++;
- } else if (y < Y1) {
- x = X2;
- y++;
- } else {
- x = X2;
- y = Y2;
- }
- draw(x, y, PRISONER);
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement