You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.5 KiB
C

#define _BSD_SOURCE
#include <ncurses.h>
#include <unistd.h>
#define MAX_X 100.0
#define MAX_Y 100.0
#define MAX_R 24
#define MAX_C 80
void xy2rc(float x, float y, int *r, int *c) {
int mrow, mcol;
getmaxyx(stdscr, mrow, mcol);
*c = (x / MAX_X) * mcol;
*r = mrow - ((y / MAX_Y) * mrow);
}
float lerp(float from, float to, float t) {
return from + t * (to - from);
}
float ball_x = 0.0;
float ball_y = 0.0;
float dir_x = +2.0;
float dir_y = +1.0;
float pad_l = 0.0;
float pad_r = 0.0;
void paint_ball() {
int r, c;
xy2rc(ball_x, ball_y, &r, &c);
mvaddch(r, c, ACS_DIAMOND);
}
void paint_paddles() {
int r, c;
xy2rc(0, pad_l, &r, &c);
mvaddch(r, c, ACS_VLINE);
xy2rc(MAX_X, pad_r, &r, &c);
c--;
mvaddch(r, c, ACS_VLINE);
}
void paint() {
clear();
paint_paddles();
paint_ball();
refresh();
}
int main() {
initscr(); /* Start curses mode */
curs_set(0); /* Disable cursor */
while (true) {
ball_x += dir_x;
ball_y += dir_y;
if (ball_x > MAX_X || ball_x < 0) {
dir_x = -dir_x;
}
if (ball_y > MAX_Y || ball_y < 0) {
dir_y = -dir_y;
}
/* Awesome artificial intelligence working here. */
if (ball_x < 0.25f * MAX_X) {
pad_l = lerp(pad_l, ball_y, 0.3);
}
if (ball_x > 0.75f * MAX_X) {
pad_r = lerp(pad_r, ball_y, 0.3);
}
paint();
usleep(50000);
}
getch(); /* Wait for user input */
endwin(); /* End curses mode */
}