diff options
| author | Preston Pan <preston@nullring.xyz> | 2023-02-06 01:10:38 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-06 01:18:08 -0800 |
| commit | e9459fc46af4f934faaee5f806df0b73bd494124 (patch) | |
| tree | 012962b05d0c968ebf1e56d41f4828f4d0fdcc9c /snake.c | |
| parent | 0e1eed88f1adf24923d03c120186f131dbc020fd (diff) | |
idk what I added but i added something
Diffstat (limited to 'snake.c')
| -rw-r--r-- | snake.c | 23 |
1 files changed, 16 insertions, 7 deletions
@@ -3,29 +3,38 @@ #include <time.h> #include <pthread.h> #include <signal.h> //handling ctrl-c +#include <sys/ioctl.h> //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; |
