From b9821ccd5ef334a9c4d4a053283021d026308a96 Mon Sep 17 00:00:00 2001 From: orange Date: Mon, 10 Jun 2013 09:32:09 +0200 Subject: [PATCH] simple pong --- .gitignore | 3 +++ Makefile | 2 +- ncurses-pong.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 ncurses-pong.c diff --git a/.gitignore b/.gitignore index a43b0c7..b4e1f13 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ mandelbrot.bmp threads structs circular-buffer +bit-fuckery +bit-fuckery2 +ncurses-pong diff --git a/Makefile b/Makefile index 24297e5..d10bc25 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CFLAGS=-std=c99 -Wall -g -O2 INDENTOPTS=-kr --no-tabs --braces-on-func-def-line --indent-level2 -TARGETS=approximate-pi linked-list mandelbrot mandelbrot.bmp threads circular-buffer structs ncurses-pong bit-fuckery bit-fuckery2 +TARGETS=approximate-pi linked-list mandelbrot threads circular-buffer structs ncurses-pong bit-fuckery bit-fuckery2 .PHONY: all all: $(TARGETS) diff --git a/ncurses-pong.c b/ncurses-pong.c new file mode 100644 index 0000000..3054dfe --- /dev/null +++ b/ncurses-pong.c @@ -0,0 +1,59 @@ +#include +#include + +#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) { + *c = (x / MAX_X) * MAX_C; + *r = MAX_R - ((y / MAX_Y) * MAX_R); +} + +float ball_x = 0.0; +float ball_y = 0.0; +float dir_x = +2.0; +float dir_y = +1.0; + +void move_ball(float x, float y) { + /* Move ball */ + ball_x = x; + ball_y = y; +} + +void paint_ball() { + int r, c; + xy2rc(ball_x, ball_y, &r, &c); + mvaddch(r, c, ACS_DIAMOND); +} + +void paint() { + clear(); + 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; + } + + paint(); + usleep(50000); + } + refresh(); /* Print it on to the real screen */ + getch(); /* Wait for user input */ + endwin(); /* End curses mode */ +}