#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#include <sys/time.h>
+#include <time.h>
int
die(int code)
int
main(void) {
- int x = 1, y = 0, aoff = 0, boff = 0;
- int max_y = 0, max_x = 0;
+ int x, y, aoff = 0, boff = 0;
+ int max_y, max_x;
int max_y_new = 0, max_x_new = 0;
- int direction = 1;
int pad_h = 5;
int a_score = 0, b_score = 0;
- int slope_x = 5;
+ int slope_x = 8;
int slope_y = 1;
- int index = 0;
int index_dir = 1;
+ int direction = 1;
+ double speed = 0.015;
+ struct timeval start, stop;
// Initialize curses
initscr();
noecho();
curs_set(0);
nodelay(stdscr, 1); // Don't wait for getch input
- index = max_y/2;
+ getmaxyx(stdscr, max_y, max_x);
+ --max_x;
+ --max_y;
+ int index = (max_y/2)*(slope_x/slope_y);
x = max_x/2;
+ gettimeofday(&start, NULL);
while(1) {
getmaxyx(stdscr, max_y_new, max_x_new);
--max_x_new;
- --max_y_new;
+ --max_y_new;
pad_h = 0.2 * max_y;
if (max_y_new != max_y || max_x_new != max_x) {
clear();
max_y = max_y_new;
}
- index += index_dir;
-
// Parse keypresses
int key = getch();
if (key == 'q') {
}
// Draw paddles
- for (int i = 0; i < max_y; i++) {
+ for (int i = 0; i < max_y + 1; i++) {
mvprintw(i, 0, " ");
+ mvprintw(i, 1, " ");
mvprintw(i, max_x, " ");
+ mvprintw(i, max_x - 1, " ");
}
for (int i = aoff; i < aoff + pad_h; i++) {
mvprintw(i, 0, "|");
mvprintw(0, max_x - 1, "%d", b_score);
// Move ball
- mvprintw(y, x, " ");
-
- x += direction;
- y = index / slope_x * slope_y;
- mvprintw(y, x, "o");
+ gettimeofday(&stop, NULL);
+ 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;
+ mvprintw(y, x, "o");
+ }
- // Check if touching paddle
- if (x == max_x) {
+ // Check if touching paddle or edges
+ if (x >= max_x - 1) {
if (y >= boff && y <= boff + pad_h) {
direction *= -1;
+ x += direction;
} else {
a_score += 1;
- usleep(600000);
x = max_x/2;
- index = max_y/2;
+ index = (max_y/2)*(slope_x/slope_y);
+ speed /= 0.8;
+ sleep(1);
}
- } else if (x == 0) {
+ } else if (x <= 1) {
if (y >= aoff && y <= aoff + pad_h) {
direction *= -1;
+ x += direction;
} else {
b_score += 1;
- usleep(600000);
x = max_x/2;
- index = max_y/2;
+ index = (max_y/2)*(slope_x/slope_y);
+ speed /= 0.8;
+ sleep(1);
}
- } else if (y < 0 || y > max_y) {
+ } else if (y == 0 || y == max_y) {
index_dir *= -1;
}
- usleep(3000000 * (1.0/((double)max_x + (double)max_y)));
}
}