aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--games/gamecatch.c42
-rw-r--r--games/gameclicker.c53
-rw-r--r--games/gameguess.c53
-rw-r--r--games/gamemaze.c52
-rw-r--r--petratc.c110
-rw-r--r--rat.c130
-rw-r--r--rat.h10
8 files changed, 213 insertions, 241 deletions
diff --git a/Makefile b/Makefile
index 3f81ce9..939c4c4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
CC = gcc
CFLAGS = -Wall -Wextra -O2 -pipe
-LDLIBS = -lncursesw -ltinfow
+LDLIBS = -lncurses -ltinfo
TARGET = petratc
-SRC = petratc.c $(wildcard games/*.c)
+SRC = petratc.c rat.c $(wildcard games/*.c)
PREFIX = /usr/local
$(TARGET): $(SRC)
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);
}
diff --git a/petratc.c b/petratc.c
index 440505b..d3171cd 100644
--- a/petratc.c
+++ b/petratc.c
@@ -6,23 +6,16 @@
#include <sys/stat.h>
#include "rat.h"
-#ifdef __APPLE__
- #define ratpath "%s/Library/Application Support/rat"
- #define ratsave "%s/Library/Application Support/rat/rat.txt"
-#else
- #define ratpath "%s/.local/share/rat"
- #define ratsave "%s/.local/share/rat/rat.txt"
-#endif
- char *ratart[] = {
- " .---.\n (\\./) \\.......-\n >' '< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
- " .---.\n-......./ (\\./)\n \"\"\"\"\"\"'.__) >' '<\n _\" \" ` \"",
- " .---.\n (\\./) \\.......-\n >= =< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
- " .---.\n (\\./) \\.......-\n >0 0< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
- " .---.\n (\\./) \\.......-\n >^ ^< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
- };
+char *ratart[] = {
+ " .---.\n (\\./) \\.......-\n >' '< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
+ " .---.\n-......./ (\\./)\n \"\"\"\"\"\"'.__) >' '<\n _\" \" ` \"",
+ " .---.\n (\\./) \\.......-\n >= =< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
+ " .---.\n (\\./) \\.......-\n >0 0< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
+ " .---.\n (\\./) \\.......-\n >^ ^< (__.'\"\"\"\"\"\"\n \" ` \" \"_",
+};
- char *messages[] = {
+char *messages[] = {
"squeak!",
"squeak! !",
"squeak",
@@ -30,78 +23,18 @@
"SQUEAK",
"SQUEAK!!",
"SQUEAAAKKKKK",
- };
+};
- char *funfact[] = {
- "press q to exit games !",
- "rats love cheese!",
- "i am sleepy.."
- };
+char *funfact[] = {
+ "press q to exit games !",
+ "rats love cheese!",
+ "i am sleepy.."
+};
-void addstat(float *guy){
-
- if(*guy < 98.5){
- *guy += 1.5;
- } else{*guy += 0.05;}
-}
Rat rat;
-
-void save(void) {
- char path[256];
- snprintf(path, sizeof(path), ratpath, getenv("HOME"));
- mkdir(path, 0755);
- snprintf(path, sizeof(path), ratsave, getenv("HOME"));
-
- FILE *f = fopen(path, "w");
- if (!f) return;
- fprintf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
- rat.hunger, rat.love, rat.fun, rat.clean, rat.health, rat.age);
- fclose(f);
-}
-
-void load(void) {
- char path[256];
- snprintf(path, sizeof(path), ratsave, getenv("HOME"));
-
- FILE *f = fopen(path, "r");
- if (!f) {
- rat.hunger = 80.0f;
- rat.love = 80.0f;
- rat.fun = 80.0f;
- rat.clean = 80.0f;
- rat.health = 80.0f;
- rat.age = time(NULL);
- return;
- }
- if(fscanf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
- &rat.hunger, &rat.love, &rat.fun, &rat.clean, &rat.health, &rat.age) != 6){
- fprintf(stderr, "error: failed to initialize save data.\n");
- }
- //this should shut the compiler up
-
- fclose(f);
-}
-
-float decay(float current, float dayspassed, float rate) {
- float result = current - (dayspassed * rate);
- return result < 0.0f ? 0.0f : result;
-}
-
-void setstat(){
- int timenow = time(NULL);
- int newage = timenow - rat.age;
- float days = newage / 86400.0f; //one day
-
- rat.hunger = decay(rat.hunger, days, 50.0f); //hungry beast
- rat.love = decay(rat.love, days, 1.1f); //rats love you they won't hate you easly
- rat.fun = decay(rat.fun, days, 25.0f);
- rat.clean = decay(rat.clean, days, 5.0f);//rats are surprisingly clean they clean themselves so slow
- rat.health = decay(rat.health, days, 0.6f);
-
- rat.age = timenow;
-}
-//this is better yeah
+//i was cleaning the code up a bit and what the fuck is this
+//it's so funny Rat rat
void printrat(WINDOW *win){
@@ -151,14 +84,8 @@ int truing = 1;
load();
setstat();
-
- initscr();
- cbreak();
- noecho();
- keypad(stdscr, TRUE);
- curs_set(0);
-
- WINDOW *win = newwin(25, 45, 1, 1);
+
+ WINDOW *win = ratdrawbox();
printrat(win);
while(truing){
@@ -202,4 +129,3 @@ int truing = 1;
printf("bye bye~~\n");
return(0);
}
-
diff --git a/rat.c b/rat.c
new file mode 100644
index 0000000..6c504ba
--- /dev/null
+++ b/rat.c
@@ -0,0 +1,130 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <ncurses.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "rat.h"
+
+
+/*functions used in initilization/main menu (not games)*/
+#ifdef __APPLE__
+ #define ratpath "%s/Library/Application Support/rat"
+ #define ratsave "%s/Library/Application Support/rat/rat.txt"
+#else
+ #define ratpath "%s/.local/share/rat"
+ #define ratsave "%s/.local/share/rat/rat.txt"
+#endif
+
+void save(void) {
+ char path[256];
+ snprintf(path, sizeof(path), ratpath, getenv("HOME"));
+ mkdir(path, 0755);
+ snprintf(path, sizeof(path), ratsave, getenv("HOME"));
+
+ FILE *f = fopen(path, "w");
+ if (!f) return;
+ fprintf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
+ rat.hunger, rat.love, rat.fun, rat.clean, rat.health, rat.age);
+ fclose(f);
+}
+
+void load(void) {
+ char path[256];
+ snprintf(path, sizeof(path), ratsave, getenv("HOME"));
+
+ FILE *f = fopen(path, "r");
+ if (!f) {
+ rat.hunger = 80.0f;
+ rat.love = 80.0f;
+ rat.fun = 80.0f;
+ rat.clean = 80.0f;
+ rat.health = 80.0f;
+ rat.age = time(NULL);
+ return;
+ }
+ if(fscanf(f, "%f\n%f\n%f\n%f\n%f\n%d\n",
+ &rat.hunger, &rat.love, &rat.fun, &rat.clean, &rat.health, &rat.age) != 6){
+ fprintf(stderr, "error: failed to initialize save data.\n");
+ }
+ //this should shut the compiler up
+
+ fclose(f);
+}
+
+void addstat(float *guy){
+
+ if(*guy < 98.5){
+ *guy += 1.5;
+ } else{*guy += 0.05;}
+}
+
+float decay(float current, float dayspassed, float rate) {
+ float result = current - (dayspassed * rate);
+ return result < 0.0f ? 0.0f : result;
+}
+
+void setstat(){
+ int timenow = time(NULL);
+ int newage = timenow - rat.age;
+ float days = newage / 86400.0f; //one day
+
+ rat.hunger = decay(rat.hunger, days, 50.0f); //hungry beast
+ rat.love = decay(rat.love, days, 1.1f); //rats love you they won't hate you easly
+ rat.fun = decay(rat.fun, days, 25.0f);
+ rat.clean = decay(rat.clean, days, 5.0f);//rats are surprisingly clean they clean themselves so slow
+ rat.health = decay(rat.health, days, 0.6f);
+
+ rat.age = timenow;
+}
+
+WINDOW *ratdrawbox(void){
+
+ static int initialized = 0;
+ if(!initialized){ initscr(); }else{ refresh(); }
+ ++initialized;
+
+ noecho();
+ cbreak();
+ nodelay(stdscr, TRUE);
+ keypad(stdscr, TRUE);
+ curs_set(0);
+
+ int winh = getmaxy(stdscr) * 0.97;
+ int winw = getmaxx(stdscr) * 0.97;
+ int boxy = (getmaxy(stdscr) - winh) / 2;
+ int boxx = (getmaxx(stdscr) - winw) / 2;
+
+ return newwin(winh, winw, boxy, boxx);
+}
+
+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;
+}
diff --git a/rat.h b/rat.h
index d887ccb..4093af1 100644
--- a/rat.h
+++ b/rat.h
@@ -1,6 +1,8 @@
#ifndef rat_h
#define rat_h
+#include <ncurses.h>
+
typedef struct {
float hunger;
float love;
@@ -11,10 +13,18 @@ typedef struct {
} Rat;
extern char *ratart[];
+extern char *funfact[];
extern char *messages[];
+
extern Rat rat;
+
+void load(void);
+void save(void);
+void setstat(void);
void addstat(float *guy);
+WINDOW *ratdrawbox(void);
+int wrapprint(WINDOW *win, int row, int col, int maxwidth, const char *msg);
void gameguess(void);
void gamecatch(void);