21 return (rand() % (high - low + 1)) + low;
29 return ((rand() % (high - low + 1)) + low) / 100.0;
35 return pow(-1, rand());
40 srand((unsigned int)time(NULL));
41 int aoff = 0, boff = 0;
43 int max_y_new = 0, max_x_new = 0;
45 int a_score = 0, b_score = 0;
46 int slope_x = randslope();
47 int index_dir = randsign();
48 int direction = randsign();
49 double speed = randspeed();
50 struct timeval start, stop;
56 nodelay(stdscr, 1); // Don't wait for getch input
57 getmaxyx(stdscr, max_y, max_x);
63 gettimeofday(&start, NULL);
66 getmaxyx(stdscr, max_y_new, max_x_new);
70 if (max_y_new != max_y || max_x_new != max_x) {
80 } else if (key == 'k') {
81 if (boff < max_y - pad_h) boff += 1;
82 } else if (key == 'i') {
83 if (boff > 0) boff -= 1;
84 } else if (key == 's') {
85 if (aoff < max_y - pad_h) aoff += 1;
86 } else if (key == 'w') {
87 if (aoff > 0) aoff -= 1;
91 for (int i = 0; i < max_y + 1; i++) {
94 mvprintw(i, max_x, " ");
95 mvprintw(i, max_x - 1, " ");
99 for (int i = aoff; i < aoff + pad_h; i++) {
103 for (int i = boff; i < boff + pad_h; i++) {
104 mvprintw(i, max_x, "|");
108 for (int i = 0; i < max_y; i++) {
109 mvprintw(i, max_x/2, ":");
113 mvprintw(0, max_x/2 - 2, "%d", a_score);
114 mvprintw(0, max_x/2 + 2, "%d", b_score);
116 // Move ball if enough time has elapsed
117 gettimeofday(&stop, NULL);
118 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
119 (double)(stop.tv_sec - start.tv_sec) > speed) {
120 gettimeofday(&start, NULL);
123 if (x % slope_x == 0) y += index_dir;
127 if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
128 (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
131 /* Find the distance that the ball hits from the center of the padel and
132 accordingly adjust ball path */
133 int poff = (x <= 1) ? aoff : boff;
134 double fromc = fabs(0.5 * (poff + 0.5 * pad_h - y));
135 slope_x *= (slope_x < 20) ? 1 : (int) ((double) slope_x / fromc);
141 } else if (x >= max_x - 1 || x <= 1) {
142 // Ball goes out of bounds
149 slope_x = randslope();
150 index_dir = randsign();
153 } else if (y == 0 || y == max_y) {
154 // Ball hits top or bottom wall
157 y = (y == max_y) ? max_y - 1 : 1;