20 return (rand() % (high - low + 1)) + low;
25 srand((unsigned int)time(NULL));
26 int x, y, aoff = 0, boff = 0;
28 int max_y_new = 0, max_x_new = 0;
30 int a_score = 0, b_score = 0;
31 int slope_x = randslope();
35 struct timeval start, stop;
41 nodelay(stdscr, 1); // Don't wait for getch input
42 getmaxyx(stdscr, max_y, max_x);
45 int index = slope_x*max_y/2;
48 gettimeofday(&start, NULL);
51 getmaxyx(stdscr, max_y_new, max_x_new);
55 if (max_y_new != max_y || max_x_new != max_x) {
65 } else if (key == 'k') {
67 } else if (key == 'i') {
69 } else if (key == 's') {
71 } else if (key == 'w') {
76 for (int i = 0; i < max_y + 1; i++) {
79 mvprintw(i, max_x, " ");
80 mvprintw(i, max_x - 1, " ");
84 for (int i = aoff; i < aoff + pad_h; i++) {
88 for (int i = boff; i < boff + pad_h; i++) {
89 mvprintw(i, max_x, "|");
93 for (int i = 0; i < max_y; i++) {
94 mvprintw(i, max_x/2, ":");
98 mvprintw(0, max_x/2 - 2, "%d", a_score);
99 mvprintw(0, max_x/2 + 2, "%d", b_score);
101 // Move ball if enough time has elapsed
102 gettimeofday(&stop, NULL);
103 if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
104 (double)(stop.tv_sec - start.tv_sec) > speed) {
105 gettimeofday(&start, NULL);
113 if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
114 (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
119 } else if (x >= max_x - 1 || x <= 1) {
120 // Ball goes out of bounds
127 index = slope_x*max_y/2;
128 slope_x = randslope();
131 } else if (y == 0 || y == max_y) {
132 // Ball hits top or bottom wall