diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-06 01:07:52 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-06 01:07:52 -0800 |
| commit | bc9372c5ceffc7229afa5963e0227a1d3c0a15d9 (patch) | |
| tree | 64d1318625d7f056d0ddcb10b8e0f16d99f1e40c /snake.c | |
| parent | 3e86fe9d5916e7574e630a35639bc2bae68d6f27 (diff) | |
board automatically shrinks if terminal is too small
Diffstat (limited to 'snake.c')
| -rw-r--r-- | snake.c | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -3,6 +3,7 @@ #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" @@ -14,13 +15,21 @@ void interrupt_handler(int sig) { INT_CTRL_C = 1; } -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) { + //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 |
