random

:-)
git clone read: git://git.margiolis.net/random.git
Log | Files | Refs | LICENSE

bounce.c (746B)


      1 #include <curses.h>
      2 #include <stdio.h>
      3 #include <unistd.h>
      4 
      5 struct ball {
      6 	int x;
      7 	int y;
      8 	int vx;
      9 	int vy;
     10 	int xmax;
     11 	int ymax;
     12 };
     13 
     14 static void
     15 cursesinit(void)
     16 {
     17 	if (!initscr())
     18 		exit(1);
     19 	cbreak();
     20 	noecho();
     21 	curs_set(0);
     22 }
     23 
     24 static void
     25 collision(struct ball *b)
     26 {
     27 	if (b->y < 2 || b->y > b->ymax-1)
     28 		b->vy *= -1.0f;
     29 	if (b->x < 1 || b->x > b->xmax-1)
     30 		b->vx *= -1.0f;
     31 	b->y += b->vy;
     32 	b->x += b->vx;
     33 }
     34 
     35 int
     36 main(int argc, char *argv[])
     37 {
     38 	struct ball b = {1, 2, 1, 1, 0, 0};
     39 
     40 	cursesinit();
     41 	getmaxyx(stdscr, b.ymax, b.xmax);
     42 
     43 	for (;;) {
     44 		erase();
     45 		collision(&b);
     46 		mvaddch(b.y, b.x, 'O');
     47 		mvprintw(0, 0, "(x, y) = (%d, %d)", b.x, b.y);
     48 		mvhline(1, 0, ACS_HLINE, b.xmax);
     49 		refresh();
     50 		usleep(15000);
     51 	}
     52 	endwin();
     53 
     54 	return 0;
     55 }