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
12 years ago
#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);
12 years ago
}
12 years ago
float lerp(float from, float to, float t) {
12 years ago
return from + t * (to - from);
12 years ago
}
12 years ago
float ball_x = 0.0;
float ball_y = 0.0;
float dir_x = +2.0;
float dir_y = +1.0;
12 years ago
float pad_l = 0.0;
float pad_r = 0.0;
12 years ago
void paint_ball() {
int r, c;
xy2rc(ball_x, ball_y, &r, &c);
mvaddch(r, c, ACS_DIAMOND);
}
12 years ago
void paint_paddles() {
int r, c;
12 years ago
xy2rc(0, pad_l, &r, &c);
12 years ago
mvaddch(r, c, ACS_VLINE);
xy2rc(MAX_X, pad_r, &r, &c);
c--;
mvaddch(r, c, ACS_VLINE);
}
12 years ago
void paint() {
clear();
12 years ago
paint_paddles();
12 years ago
paint_ball();
refresh();
}
int main() {
12 years ago
initscr(); /* Start curses mode */
curs_set(0); /* Disable cursor */
12 years ago
12 years ago
while (true) {
12 years ago
ball_x += dir_x;
ball_y += dir_y;
12 years ago
if (ball_x > MAX_X || ball_x < 0) {
12 years ago
dir_x = -dir_x;
}
12 years ago
if (ball_y > MAX_Y || ball_y < 0) {
12 years ago
dir_y = -dir_y;
}
12 years ago
/* 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);
}
12 years ago
paint();
usleep(50000);
}
12 years ago
getch(); /* Wait for user input */
endwin(); /* End curses mode */
12 years ago
}