|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <ncurses.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <sys/time.h> |
| 5 | +#include <math.h> |
| 6 | + |
| 7 | +void reset_screen() |
| 8 | +{ |
| 9 | + delwin(stdscr); |
| 10 | + endwin(); |
| 11 | + refresh(); |
| 12 | + |
| 13 | + puts("Bye."); |
| 14 | +} |
| 15 | + |
| 16 | +int main() |
| 17 | +{ |
| 18 | + initscr(); // start ncurses |
| 19 | + atexit(reset_screen); // vacuum |
| 20 | + |
| 21 | + if (!has_colors()) |
| 22 | + { |
| 23 | + exit(2); // we want colors |
| 24 | + } |
| 25 | + start_color(); // start colors |
| 26 | + |
| 27 | + if (COLOR_PAIRS < 16) |
| 28 | + { |
| 29 | + exit(2); // at least 16 pairs |
| 30 | + } |
| 31 | + |
| 32 | + init_pair(1, COLOR_BLUE, COLOR_BLACK); |
| 33 | + init_pair(2, COLOR_RED, COLOR_BLACK); |
| 34 | + init_pair(3, COLOR_BLACK, COLOR_YELLOW); |
| 35 | + |
| 36 | + cbreak(); // line buffering disabled |
| 37 | + keypad(stdscr, true); // enables F_keys |
| 38 | + noecho(); // disables echoing of characters |
| 39 | + curs_set(0); // hide cursor |
| 40 | + |
| 41 | + int x, y; |
| 42 | + getmaxyx(stdscr, y, x); |
| 43 | + |
| 44 | + attron(COLOR_PAIR(3) | A_BLINK); |
| 45 | + mvaddstr(y / 2, x / 2 - 10, "WAITING FOR PLAYER..."); |
| 46 | + attroff(COLOR_PAIR(3) | A_BLINK); |
| 47 | + |
| 48 | + getch(); // blocking key input |
| 49 | + erase(); |
| 50 | + |
| 51 | + int last_key = -1; |
| 52 | + int angle = 0; |
| 53 | + int radius = 5; |
| 54 | + int speed = 5; |
| 55 | + |
| 56 | + nodelay(stdscr, 1); // non blocking key input |
| 57 | + while (true) |
| 58 | + { |
| 59 | + int c = getch(); |
| 60 | + |
| 61 | + if (c == 27) |
| 62 | + break; |
| 63 | + if (c == 'q') |
| 64 | + break; |
| 65 | + |
| 66 | + switch (c) |
| 67 | + { |
| 68 | + case KEY_RESIZE: |
| 69 | + getmaxyx(stdscr, y, x); |
| 70 | + break; |
| 71 | + case KEY_DOWN: |
| 72 | + speed = speed > -10 ? speed - 1 : -10; |
| 73 | + break; |
| 74 | + case KEY_UP: |
| 75 | + speed = speed < 10 ? speed + 1 : 10; |
| 76 | + break; |
| 77 | + case KEY_RIGHT: |
| 78 | + radius = radius < 10 ? radius + 1 : 10; |
| 79 | + break; |
| 80 | + case KEY_LEFT: |
| 81 | + radius = radius > 1 ? radius - 1 : 1; |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + // clear screan |
| 86 | + erase(); |
| 87 | + |
| 88 | + // draw the XY axis |
| 89 | + mvvline(0, x / 2, ACS_VLINE, y); |
| 90 | + mvhline(y / 2, 0, ACS_HLINE, x); |
| 91 | + |
| 92 | + // print CROSS at center of the screen |
| 93 | + attron(COLOR_PAIR(1)); |
| 94 | + mvaddstr(y / 2, x / 2 - 2, "CROSS"); |
| 95 | + attroff(COLOR_PAIR(1)); |
| 96 | + |
| 97 | + // print screen size |
| 98 | + attron(COLOR_PAIR(2)); |
| 99 | + mvprintw(y - 2, 2, "x,y = %d,%d", x, y); |
| 100 | + attroff(COLOR_PAIR(2)); |
| 101 | + |
| 102 | + // print the last key |
| 103 | + if (c != -1) |
| 104 | + { |
| 105 | + last_key = c; |
| 106 | + } |
| 107 | + if (last_key >= 32 && last_key <= 126) |
| 108 | + { |
| 109 | + mvprintw(2, 2, "last key: %c 0x%x", last_key, last_key); |
| 110 | + } |
| 111 | + else if (last_key != -1) |
| 112 | + { |
| 113 | + mvprintw(2, 2, "last key: 0x%x 0%03o", last_key, last_key, last_key); |
| 114 | + } |
| 115 | + |
| 116 | + // draw the radar pursuit |
| 117 | + int bx, by; |
| 118 | + bx = (int)(x / 2 + (x / 20.) * radius * cos(angle * M_PI / 180.)); |
| 119 | + by = (int)(y / 2 - (y / 20.) * radius * sin(angle * M_PI / 180.)); |
| 120 | + |
| 121 | + if (mvinch(by, bx) != ' ') |
| 122 | + { |
| 123 | + // let's add some alea when hiting the cross |
| 124 | + if (rand() % 2) |
| 125 | + { |
| 126 | + // bounce the ball |
| 127 | + speed = -speed; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + // accelerate or decelerate the ball |
| 132 | + int r = rand() % 3; |
| 133 | + if (r == 1 && speed < 10) |
| 134 | + speed++; |
| 135 | + else if (r == 2 && speed > -10) |
| 136 | + speed--; |
| 137 | + } |
| 138 | + } |
| 139 | + mvaddch(by, bx, '@'); |
| 140 | + |
| 141 | + // print the parameters |
| 142 | + mvprintw(0, x - 10, "a=%3d r=%2d", angle, radius); |
| 143 | + mvprintw(1, x - 9, "speed=%3d", speed); |
| 144 | + |
| 145 | + struct timeval tv; |
| 146 | + gettimeofday(&tv, nullptr); |
| 147 | + mvprintw(1, 2, "%ld.%06ld", tv.tv_sec, tv.tv_usec); |
| 148 | + |
| 149 | + // update |
| 150 | + angle = (angle + speed + 360) % 360; |
| 151 | + |
| 152 | + // |
| 153 | + usleep(40000); // 40 ms ~ 25 Hz |
| 154 | + } |
| 155 | + |
| 156 | + return 0; |
| 157 | +} |
0 commit comments