diff options
Diffstat (limited to 'games')
| -rw-r--r-- | games/gamecatch.c | 42 | ||||
| -rw-r--r-- | games/gameclicker.c | 53 | ||||
| -rw-r--r-- | games/gameguess.c | 53 | ||||
| -rw-r--r-- | games/gamemaze.c | 52 |
4 files changed, 53 insertions, 147 deletions
diff --git a/games/gamecatch.c b/games/gamecatch.c index 239c42e..0680027 100644 --- a/games/gamecatch.c +++ b/games/gamecatch.c @@ -34,13 +34,13 @@ // i just realized this is basically cmatrix with a little guy at the bottom */ -#define width 40 -#define height getmaxy(stdscr) #define maxdrops 70 static int running; static int won; static int howmany; +static int width; +static int height; typedef struct{ @@ -85,17 +85,11 @@ static void stop() { void gamecatch(){ srand(time(NULL)); //@sagucs hey - - static int initialized = 0; - if(initialized == 0){ initscr();}else{refresh();} // turns out you don't wanna call initscr() many times per session so do this because it glitches otherwise - ++initialized; - howmany = 0; - noecho(); - cbreak(); - nodelay(stdscr, TRUE); - keypad(stdscr, TRUE); - curs_set(0); + WINDOW *win = ratdrawbox(); + howmany = 0; + width = 40; + height = getmaxy(win) - 2; cursor curs = {width / 2, {"(>.<)_", "_(>.<)"}, 0}; @@ -123,18 +117,24 @@ void gamecatch(){ if (!running) break; if (rand() % 10 < 3) spawndrops(); - clear(); - - mvprintw(0, width + 2, "caught: %d/30", howmany); - mvprintw(1, width + 2, "(catch 30 to win!)"); - mvprintw(2, width + 2, "move with A and D"); - mvprintw(3, width + 2, "or the or the corresponding arrows"); + werase(win); + box(win, 0, 0); + + mvwvline(win, 1, width + 1, ACS_VLINE, height); + mvwaddch(win, 0, width + 1, ACS_TTEE); + mvwaddch(win, height + 1, width + 1, ACS_BTEE); + //this should do it + + mvwprintw(win, 1, width + 3, "caught: %d/30", howmany); + mvwprintw(win, 2, width + 3, "(catch 30 to win!)"); + mvwprintw(win, 3, width + 3, "move with A and D"); + mvwprintw(win, 4, width + 3, "or the or the corresponding arrows"); for (int i = 0; i < maxdrops; i++) { if (droop[i].active) { - mvaddstr(droop[i].posy, droop[i].posx, droop[i].symbol); + mvwaddstr(win, droop[i].posy + 1, droop[i].posx + 1, droop[i].symbol); droop[i].posy++; if (droop[i].posy >= height) droop[i].active = 0; @@ -148,9 +148,9 @@ void gamecatch(){ } } - mvaddstr(height - 1, curs.posx, curs.face[curs.direction]); + mvwaddstr(win, height, curs.posx + 1, curs.face[curs.direction]); - refresh(); + wrefresh(win); usleep(100000); } diff --git a/games/gameclicker.c b/games/gameclicker.c index 548ffd7..4dff0b6 100644 --- a/games/gameclicker.c +++ b/games/gameclicker.c @@ -12,9 +12,6 @@ i lowkey stole the art of the little rat and the cheese from the internet lol https://asciiart.website/art/752 */ -#define width getmaxx(stdscr) -#define height getmaxy(stdscr) - int running; static void stop() { @@ -188,37 +185,6 @@ static char *bignumprint(bignum b) { } -static int wrapprint(WINDOW *win, int row, int col, int maxwidth, const char *msg){ - - char copy[256]; - strncpy(copy, msg, sizeof(copy)); - copy[sizeof(copy)-1] = '\0'; - - char line[256] = ""; - char *word = strtok(copy, " "); - while(word){ - int linelen = strlen(line); - int wordlen = strlen(word); - int extra = (linelen > 0) ? 1 : 0; - if(linelen + extra + wordlen > maxwidth){ - mvwprintw(win, row++, col, "%s", line); - line[0] = '\0'; - linelen = 0; - extra = 0; - } - - if(linelen > 0) strcat(line, " "); - strcat(line, word); - - word = strtok(NULL, " "); - } - if(strlen(line) > 0){ - mvwprintw(win, row++, col, "%s", line); - } - - return row; -} - //wide = art and score side by side //narrow = art on topand score below typedef enum { layoutnarrow, layoutwide } layoutmode; @@ -291,28 +257,11 @@ static void drawwin(WINDOW *win, bignum cheeses){ void gameclicker(){ srand(time(NULL)); - static int initialized = 0; - if(!initialized){ initscr();}else{refresh();} // the way i wrote this reminds me of :(){ :|:& };: - ++initialized; - noecho(); - cbreak(); - nodelay(stdscr, TRUE); - keypad(stdscr, TRUE); - curs_set(0); + WINDOW *win = ratdrawbox(); mousemask(BUTTON1_PRESSED, NULL); - mouseinterval(0); //don't wait to resolve single vs double click - int winh = height * 0.97; - int winw = width * 0.97; - int boxy = (height - winh) / 2; - int boxx = (width - winw) / 2; - WINDOW *win = newwin(winh, winw, boxy, boxx); - - //should i wrap all this into a macro in rat.h to not have to write all of this every time i make a new game? - //or just a function in something like rat.c yeah - running = 1; bignum cheeses = {0.0, 0}; diff --git a/games/gameguess.c b/games/gameguess.c index 7d56caf..fc29839 100644 --- a/games/gameguess.c +++ b/games/gameguess.c @@ -5,41 +5,8 @@ #include <ncurses.h> #include "../rat.h" -#define boxw 45 -#define boxh 25 #define maxhistory 4 -static int wrapprint(WINDOW *win, int row, int col, int maxwidth, const char *msg){ - - char copy[256]; - strncpy(copy, msg, sizeof(copy)); - copy[sizeof(copy)-1] = '\0'; - - char line[256] = ""; - char *word = strtok(copy, " "); - while(word){ - int linelen = strlen(line); - int wordlen = strlen(word); - int extra = (linelen > 0) ? 1 : 0; - if(linelen + extra + wordlen > maxwidth){ - mvwprintw(win, row++, col, "%s", line); - line[0] = '\0'; - linelen = 0; - extra = 0; - } - - if(linelen > 0) strcat(line, " "); - strcat(line, word); - - word = strtok(NULL, " "); - } - if(strlen(line) > 0){ - mvwprintw(win, row++, col, "%s", line); - } - - return row; -} - static void drawwin(WINDOW *win, int artpick, const char *msg, int history[], int histcount){ werase(win); @@ -57,9 +24,9 @@ static void drawwin(WINDOW *win, int artpick, const char *msg, int history[], in } row++; - wrapprint(win, row, 2, boxw - 4, msg); + wrapprint(win, row, 2, getmaxx(win) - 4, msg); - int inputrow = boxh - 3; + int inputrow = getmaxy(win) - 3; for(int i = 0; i < histcount; i++){ @@ -80,15 +47,7 @@ void gameguess(){ int history[maxhistory]; int histcount = 0; - static int initialized = 0; - if(initialized == 0){ initscr(); }else{ refresh(); } - ++initialized; - - noecho(); - cbreak(); - curs_set(0); - - WINDOW *win = newwin(boxh, boxw, 1, 1); + WINDOW *win = ratdrawbox(); char msg[96]; snprintf(msg, sizeof(msg), "let's play a game, squeak!~ you gues a number between 0 and %d!", range); @@ -100,7 +59,7 @@ void gameguess(){ echo(); char buf[16]; - mvwgetnstr(win, boxh - 3, 2, buf, sizeof(buf) - 1); + mvwgetnstr(win, getmaxy(win) - 3, 2, buf, sizeof(buf) - 1); noecho(); if((buf[0] == 'q' || buf[0] == 'Q') && buf[1] == '\0'){ @@ -108,10 +67,10 @@ void gameguess(){ echo(); char confirm[16]; - mvwgetnstr(win, boxh - 3, 2, confirm, sizeof(confirm) - 1); + mvwgetnstr(win, getmaxy(win) - 3, 2, confirm, sizeof(confirm) - 1); noecho(); - if((confirm[0] == 'q' || confirm[0] == 'Q' || confirm[0] == 'y' || confirm[0] == 'Y') && confirm[1] == '\0'){ + if((confirm[0] == 'q' || confirm[0] == 'Q' || confirm[0] == 'y' || confirm[0] == 'Y')){ quitgame = 1; break; } else if((confirm[0] == 'n' || confirm[0] == 'N') && confirm[1] == '\0'){ diff --git a/games/gamemaze.c b/games/gamemaze.c index 5dbce8f..1468475 100644 --- a/games/gamemaze.c +++ b/games/gamemaze.c @@ -17,12 +17,12 @@ // you start at the top and end at the bottom if i ever wanna automate taking care of the rat */ -#define width getmaxx(stdscr) -#define height getmaxy(stdscr) - static int running; static int won; static int found; +static int width; +static int height; +static WINDOW *win; typedef struct{ @@ -197,8 +197,10 @@ static void properrestart(cursor *curs, int *W, int *H) { for (int y = 0; y < *H; y++) free(grid[y]); free(grid); - *W = getmaxx(stdscr); - *H = getmaxy(stdscr); + width = getmaxx(win) - 2; + height = getmaxy(win) - 2; + *W = width; + *H = height; grid = malloc(*H * sizeof(char *)); for (int y = 0; y < *H; y++) @@ -215,12 +217,13 @@ static void properrestart(cursor *curs, int *W, int *H) { void gamemaze(){ srand(time(NULL)); - static int initialized = 0; - if(initialized == 0){ initscr();}else{refresh();} - ++initialized; - int W = getmaxx(stdscr); - int H = getmaxy(stdscr); + win = ratdrawbox(); + width = getmaxx(win) - 2; + height = getmaxy(win) - 2; + + int W = width; + int H = height; grid = malloc(H * sizeof(char *)); //those who allocate memory for (int y = 0; y < H; y++) @@ -231,12 +234,6 @@ void gamemaze(){ found = 0; - noecho(); - cbreak(); - nodelay(stdscr, TRUE); - keypad(stdscr, TRUE); - curs_set(0); - cursor curs = {1, 1, {"@"}}; running = 1; @@ -273,20 +270,21 @@ void gamemaze(){ checkexit(&curs); if (!running) break; - clear(); + werase(win); + box(win, 0, 0); - mvprintw(0, width/3+1, "move around the maze and find food!"); - mvprintw(1, width/3+1, "you've collected %d/5 cheeses", found); - mvprintw(2, width/3+1, "(only if you find food your rat will be less hungry)"); - mvprintw(height/1.5-2, width/3+1, "btw sometimes the maze is impossible just press R"); - mvprintw(height/1.5-1, width/3+1, "if that's the case"); + mvwprintw(win, 1, width/3+2, "move around the maze and find food!"); + mvwprintw(win, 2, width/3+2, "you've collected %d/5 cheeses", found); + mvwprintw(win, 3, width/3+2, "(only if you find food your rat will be less hungry)"); + mvwprintw(win, height/1.5-2+1, width/3+2, "btw sometimes the maze is impossible just press R"); + mvwprintw(win, height/1.5-1+1, width/3+2, "if that's the case"); for (int y = 0; y < H; y++) for (int x = 0; x < W; x++){ - if (grid[y][x] == '#') mvaddch(y, x, '#'); + if (grid[y][x] == '#') mvwaddch(win, y+1, x+1, '#'); else if (grid[y][x] == 'O') { int dist = abs(y - curs.posy) + abs(x - curs.posx); - if (dist <= 5) mvaddch(y, x, 'O'); + if (dist <= 5) mvwaddch(win, y+1, x+1, 'O'); } } @@ -294,11 +292,11 @@ void gamemaze(){ int hmsg = ((height/2) - 1) / 2; int msgw = 3 * wmsg + 1; int msgh = 3 * hmsg + 1; - mvprintw(msgh, msgw-2, "^ leave through here"); + mvwprintw(win, msgh+1, msgw-2+1, "^ leave through here"); - mvaddstr(curs.posy, curs.posx, curs.face[0]); + mvwaddstr(win, curs.posy+1, curs.posx+1, curs.face[0]); - refresh(); + wrefresh(win); usleep(50000); } |
