From e9459fc46af4f934faaee5f806df0b73bd494124 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Mon, 6 Feb 2023 01:10:38 -0800 Subject: idk what I added but i added something --- colors.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ snake.c | 23 +++++++++++++------ 2 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 colors.h diff --git a/colors.h b/colors.h new file mode 100644 index 0000000..d6cfab0 --- /dev/null +++ b/colors.h @@ -0,0 +1,80 @@ +/* Code taken from https://gist.github.com/RabaDabaDoba/145049536f815903c79944599c6f952a; unlicensed */ + +#ifndef COLORS_H +#define COLORS_H + +#define BLK "\e[0;30m" +#define RED "\e[0;31m" +#define GRN "\e[0;32m" +#define YEL "\e[0;33m" +#define BLU "\e[0;34m" +#define MAG "\e[0;35m" +#define CYN "\e[0;36m" +#define WHT "\e[0;37m" + +//Regular bold text +#define BBLK "\e[1;30m" +#define BRED "\e[1;31m" +#define BGRN "\e[1;32m" +#define BYEL "\e[1;33m" +#define BBLU "\e[1;34m" +#define BMAG "\e[1;35m" +#define BCYN "\e[1;36m" +#define BWHT "\e[1;37m" + +//Regular underline text +#define UBLK "\e[4;30m" +#define URED "\e[4;31m" +#define UGRN "\e[4;32m" +#define UYEL "\e[4;33m" +#define UBLU "\e[4;34m" +#define UMAG "\e[4;35m" +#define UCYN "\e[4;36m" +#define UWHT "\e[4;37m" + +//Regular background +#define BLKB "\e[40m" +#define REDB "\e[41m" +#define GRNB "\e[42m" +#define YELB "\e[43m" +#define BLUB "\e[44m" +#define MAGB "\e[45m" +#define CYNB "\e[46m" +#define WHTB "\e[47m" + +//High intensty background +#define BLKHB "\e[0;100m" +#define REDHB "\e[0;101m" +#define GRNHB "\e[0;102m" +#define YELHB "\e[0;103m" +#define BLUHB "\e[0;104m" +#define MAGHB "\e[0;105m" +#define CYNHB "\e[0;106m" +#define WHTHB "\e[0;107m" + +//High intensty text +#define HBLK "\e[0;90m" +#define HRED "\e[0;91m" +#define HGRN "\e[0;92m" +#define HYEL "\e[0;93m" +#define HBLU "\e[0;94m" +#define HMAG "\e[0;95m" +#define HCYN "\e[0;96m" +#define HWHT "\e[0;97m" + +//Bold high intensity text +#define BHBLK "\e[1;90m" +#define BHRED "\e[1;91m" +#define BHGRN "\e[1;92m" +#define BHYEL "\e[1;93m" +#define BHBLU "\e[1;94m" +#define BHMAG "\e[1;95m" +#define BHCYN "\e[1;96m" +#define BHWHT "\e[1;97m" + +//Reset +#define reset "\e[0m" +#define CRESET "\e[0m" +#define COLOR_RESET "\e[0m" + +#endif diff --git a/snake.c b/snake.c index f7097d9..8afd71c 100644 --- a/snake.c +++ b/snake.c @@ -3,29 +3,38 @@ #include #include #include //handling ctrl-c +#include //terminal size #include "vector.h" #include "snake.h" #include "conio.h" +#include "colors.h" volatile sig_atomic_t INT_CTRL_C = 0; -// unsafe functions like printf cannot be called in async interrupt handler +//unsafe functions like printf cannot be called in async interrupt handler void interrupt_handler(int sig) { INT_CTRL_C = 1; } -const int boardSize = 12; +int boardSize = 15; const int updateInterval = 300000000L; vectorp_t snakePositions; vector_t foodPos; vector_t lastMoveDir; int main(int argc, char **argv) { - printf("\e[?25l"); /* hide cursor */ - + //get terminal size + struct winsize win; + ioctl(fileno(stdout), TIOCGWINSZ, &win); + int width = (int)(win.ws_col); + int height = (int)(win.ws_row); + //multiplied by 2 to account for the fact that the board is twice as wide as high + int smallestAxis = width / 2 < height ? width / 2 : height; + boardSize = smallestAxis < boardSize ? smallestAxis : boardSize; + //hide cursor + printf("\e[?25l"); //make a new thread handling interrupts signal(SIGINT, interrupt_handler); - //init values snakePositions = init_vectorp(malloc(5 * sizeof(vector_t)), 5); snakePositions.vectors[0] = init_vector(5, 5); @@ -50,7 +59,7 @@ int main(int argc, char **argv) { pthread_create(&listenerID, NULL, get_dir_loop, &threadArg); //main game logic loop while(1) { - // handle ctrl-c interrupt + //handle ctrl-c interrupt if (INT_CTRL_C) { //unhide cursor printf("\e[?25h"); @@ -114,7 +123,7 @@ int main(int argc, char **argv) { } //clear the last getch call from the listener *cancellationToken = 1; - printf("\nYou died. Press any key to exit\n"); + printf("\n%sYou died. Press any key to exit%s\n", RED, COLOR_RESET); printf("\e[?25h"); pthread_join(listenerID, NULL); return 0; -- cgit v1.3.1