diff --git a/textpong.c b/textpong.c index 5bba1d2..f36b88d 100644 --- a/textpong.c +++ b/textpong.c @@ -4,45 +4,47 @@ #define ESC "\x1b[" -void locate(int x, int y) -{ +void locate(int x, int y) { printf(ESC "%d;%dH", y, x); } int abs(int x) { - if (x < 0) - return -x; - else - return x; + return (x < 0)? -x: x; } -int main(int argc, char const* argv[]) -{ +int main(int argc, char const* argv[]) { + int score = 0; int x=10, y=10, bx, by, vx=1, vy=1; - int bar_x = 16; + int bar_x = 16, flag = 1; bx = x; by = y; fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); printf(ESC "2J"); - while (y < 21) { + while (y < 23) { int c = getchar(); - locate(bar_x, 21); printf(" ====== "); locate(bx, by); printf(" "); locate( x, y); printf("o"); + locate(35, 0); printf("Sc: %04d", score); + locate(bar_x, 21); printf(" ====== "); - bar_x += c == 'l'? 1: c=='h'? -1: 0; - if (bar_x < 1) - bar_x = 1; - else if (bar_x > 30) - bar_x = 30; + bar_x += (c == 'l')? +1: + (c == 'h')? -1: + 0; - bx = x; by = y; - x += vx; y += vy; - if (x == 0 || x >= 35) vx = -vx; - if (y == 1 || (y == 20 && - 1 <= (x - bar_x) && (x - bar_x ) <= 7)) vy = -vy; - usleep(200000); + if (bar_x < 1) bar_x = 1; + else if (bar_x > 30) bar_x = 30; + + if (flag) { + bx = x; by = y; + x += vx; y += vy; + if (x == 0 || x >= 35) vx = -vx; + if (y == 1 || (y == 20 && + 1 <= (x - bar_x) && (x - bar_x) < 7)) vy = -vy; + if (y == 20) score++; + } + flag = 1 - flag; + usleep(100000); } printf("GAME OVER\n"); return 0;