#include <unistd.h>
#include <sys/time.h>
#include <time.h>
+#include <math.h>
int
die(int code)
exit(code);
}
+int
+randslope(void)
+{
+ const int low = 2;
+ const int high = 10;
+ return (rand() % (high - low + 1)) + low;
+}
+
+double
+randspeed(void)
+{
+ const int low = 1;
+ const int high = 2;
+ return ((rand() % (high - low + 1)) + low) / 100.0;
+}
+
+int
+randsign(void)
+{
+ return pow(-1, rand());
+}
+
int
main(void) {
- int x, y, aoff = 0, boff = 0;
+ srand((unsigned int)time(NULL));
+ int aoff = 0, boff = 0;
int max_y, max_x;
int max_y_new = 0, max_x_new = 0;
int pad_h = 5;
int a_score = 0, b_score = 0;
- int slope_x = 8;
- int slope_y = 1;
- int index_dir = 1;
- int direction = 1;
- double speed = 0.015;
+ int slope_x = randslope();
+ int index_dir = randsign();
+ int direction = randsign();
+ double speed = randspeed();
struct timeval start, stop;
// Initialize curses
getmaxyx(stdscr, max_y, max_x);
--max_x;
--max_y;
- int index = (max_y/2)*(slope_x/slope_y);
- x = max_x/2;
+ int y = max_y/2;
+ int x = max_x/2;
gettimeofday(&start, NULL);
while(1) {
if (key == 'q') {
die(0);
} else if (key == 'k') {
- boff += 2;
+ if (boff < max_y - pad_h) boff += 1;
} else if (key == 'i') {
- boff -= 2;
+ if (boff > 0) boff -= 1;
} else if (key == 's') {
- aoff += 2;
+ if (aoff < max_y - pad_h) aoff += 1;
} else if (key == 'w') {
- aoff -= 2;
+ if (aoff > 0) aoff -= 1;
}
// Clear screen
if ((double)(stop.tv_usec - start.tv_usec) / 1000000 +
(double)(stop.tv_sec - start.tv_sec) > speed) {
gettimeofday(&start, NULL);
- index += index_dir;
mvprintw(y, x, " ");
x += direction;
- y = slope_y * index / slope_x;
+ if (x % slope_x == 0) y += index_dir;
mvprintw(y, x, "o");
}
- // Check if touching paddle or edges
- if (x >= max_x - 1) {
- if (y >= boff && y <= boff + pad_h) {
- direction *= -1;
- x += direction;
+ if ((x >= max_x - 1 && y >= boff && y <= boff + pad_h) ||
+ (x <= 1 && y >= aoff && y <= aoff + pad_h)) {
+ // Ball hits paddle
+
+ /* Find the distance that the ball hits from the center of the padel and
+ accordingly adjust ball path */
+ int poff = (x <= 1) ? aoff : boff;
+ double fromc = fabs(0.5 * (poff + 0.5 * pad_h - y));
+ slope_x *= (slope_x < 20) ? 1 : (int) ((double) slope_x / fromc);
+ slope_x += 1;
+
+ direction *= -1;
+ x += direction;
+ speed *= 0.9;
+ } else if (x >= max_x - 1 || x <= 1) {
+ // Ball goes out of bounds
+ if (x <=1) {
+ b_score += 1;
} else {
a_score += 1;
- x = max_x/2;
- index = (max_y/2)*(slope_x/slope_y);
- speed /= 0.8;
- sleep(1);
- }
- } else if (x <= 1) {
- if (y >= aoff && y <= aoff + pad_h) {
- direction *= -1;
- x += direction;
- } else {
- b_score += 1;
- x = max_x/2;
- index = (max_y/2)*(slope_x/slope_y);
- speed /= 0.8;
- sleep(1);
}
+ x = max_x/2;
+ slope_x = randslope();
+ index_dir = randsign();
+ speed = randspeed();
+ sleep(1);
} else if (y == 0 || y == max_y) {
+ // Ball hits top or bottom wall
index_dir *= -1;
+ mvprintw(y, x, " ");
+ y = (y == max_y) ? max_y - 1 : 1;
}
}
}