From f200d5b175925fbb28a7fcb139edb3dee4d8dfa4 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 15 May 2021 14:46:06 -0400 Subject: [PATCH] Initial commit Its crap, but hey, its pong --- .gitignore | 1 + Makefile | 10 +++++ pong.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 pong.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e55469 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +pong diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..269671b --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: + ${CC} -o pong pong.c ${CLAGS} -lcurses ${LDFLAGS} + +install: + cp pong /usr/local/bin/pong + +uninstall: + rm /usr/local/bin/pong + +.PHONY: all diff --git a/pong.c b/pong.c new file mode 100644 index 0000000..e6b8f38 --- /dev/null +++ b/pong.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include + +int +die(int code) +{ + endwin(); + exit(code); +} + +int +main(void) { + int x = 1, y = 0, aoff = 0, boff = 0; + int max_y = 0, max_x = 0; + int max_y_new = 0, max_x_new = 0; + int direction = 1; + int pad_h = 5; + int a_score = 0, b_score = 0; + int slope_x = 5; + int slope_y = 1; + int index = 0; + int index_dir = 1; + + // Initialize curses + initscr(); + noecho(); + curs_set(0); + nodelay(stdscr, 1); // Don't wait for getch input + index = max_y/2; + x = max_x/2; + + while(1) { + getmaxyx(stdscr, max_y_new, max_x_new); + --max_x_new; + --max_y_new; + pad_h = 0.2 * max_y; + if (max_y_new != max_y || max_x_new != max_x) { + clear(); + max_x = max_x_new; + max_y = max_y_new; + } + + index += index_dir; + + // Parse keypresses + int key = getch(); + if (key == 'q') { + die(0); + } else if (key == 'k') { + boff += 2; + } else if (key == 'i') { + boff -= 2; + } else if (key == 's') { + aoff += 2; + } else if (key == 'w') { + aoff -= 2; + } + + // Draw paddles + for (int i = 0; i < max_y; i++) { + mvprintw(i, 0, " "); + mvprintw(i, max_x, " "); + } + for (int i = aoff; i < aoff + pad_h; i++) { + mvprintw(i, 0, "|"); + } + + for (int i = boff; i < boff + pad_h; i++) { + mvprintw(i, max_x, "|"); + } + + for (int i = 0; i < max_y; i++) { + mvprintw(i, max_x/2, ":"); + } + + mvprintw(0, 2, "%d", a_score); + mvprintw(0, max_x - 1, "%d", b_score); + + // Move ball + mvprintw(y, x, " "); + + x += direction; + y = index / slope_x * slope_y; + mvprintw(y, x, "o"); + + // Check if touching paddle + if (x == max_x) { + if (y >= boff && y <= boff + pad_h) { + direction *= -1; + } else { + a_score += 1; + usleep(600000); + x = max_x/2; + index = max_y/2; + } + } else if (x == 0) { + if (y >= aoff && y <= aoff + pad_h) { + direction *= -1; + } else { + b_score += 1; + usleep(600000); + x = max_x/2; + index = max_y/2; + } + } else if (y < 0 || y > max_y) { + index_dir *= -1; + } + usleep(3000000 * (1.0/((double)max_x + (double)max_y))); + } +} -- 2.39.2