21 return (rand() % (high - low + 1)) + low;
27 return pow(-1, rand());
32 srand((unsigned int)time(NULL));
33 int x, y, aoff = 0, boff = 0;
35 int max_y_new = 0, max_x_new = 0;
37 int a_score = 0, b_score = 0;
38 int slope_x = randslope();
39 int index_dir = randsign();
42 struct timeval start, stop;
48 nodelay(stdscr, 1); // Don't wait for getch input
49 getmaxyx(stdscr, max_y, max_x);
52 int index = slope_x*max_y/2;
55 gettimeofday(&start, NULL);
58 getmaxyx(stdscr, max_y_new, max_x_new);
62 if (max_y_new != max_y || max_x_new != max_x) {
72 } else if (key == 'k') {
74 } else if (key == 'i') {
76 } else if (key == 's') {
78 } else if (key == 'w') {
83 for (int i = 0; i < max_y + 1; i++) {
86 mvprintw(i, max_x, " ");
87 mvprintw(i, max_x - 1, " ");
91 for (int i = aoff; i < aoff + pad_h; i++) {
95 for (int i = boff; i < boff + pad_h; i++) {
96 mvprintw(i, max_x, "|");
100 for (int i = 0; i < max_y; i++) {
101 mvprintw(i, max_x/2, ":");
105 mvprintw(0, max_x/2 - 2, "%d", a_score);
106 mvprintw(0, max_x/2 + 2, "%d", b_score);
108 // Move ball if enough time has elapsed
109 gettimeofday(&stop, NULL);
110 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
111 (double)(stop.tv_sec - start.tv_sec) > speed) {
112 gettimeofday(&start, NULL);
120 if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
121 (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
126 } else if (x >= max_x - 1 || x <= 1) {
127 // Ball goes out of bounds
134 index = slope_x*max_y/2;
135 slope_x = randslope();
136 index_dir = randsign();
139 } else if (y == 0 || y == max_y) {
140 // Ball hits top or bottom wall