simple pong
This commit is contained in:
parent
97c466d109
commit
b9821ccd5e
3 changed files with 63 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,3 +7,6 @@ mandelbrot.bmp
|
||||||
threads
|
threads
|
||||||
structs
|
structs
|
||||||
circular-buffer
|
circular-buffer
|
||||||
|
bit-fuckery
|
||||||
|
bit-fuckery2
|
||||||
|
ncurses-pong
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
CFLAGS=-std=c99 -Wall -g -O2
|
CFLAGS=-std=c99 -Wall -g -O2
|
||||||
INDENTOPTS=-kr --no-tabs --braces-on-func-def-line --indent-level2
|
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
|
.PHONY: all
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
59
ncurses-pong.c
Normal file
59
ncurses-pong.c
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#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) {
|
||||||
|
*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 */
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue