From aea9a53762c03205db229c63505b48a41bf86688 Mon Sep 17 00:00:00 2001 From: orange Date: Sat, 15 Jun 2013 22:26:40 +0200 Subject: [PATCH] paddles! --- ncurses-pong.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ncurses-pong.c b/ncurses-pong.c index 06e806c..e2d5f96 100644 --- a/ncurses-pong.c +++ b/ncurses-pong.c @@ -13,19 +13,37 @@ void xy2rc(float x, float y, int *r, int *c) { *r = MAX_R - ((y / MAX_Y) * MAX_R); } +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(); @@ -46,6 +64,14 @@ int main() { 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); }