Added hitfeed; ClearAnimation 0 offset fix
This commit is contained in:
60
hangman.c
60
hangman.c
@@ -34,13 +34,16 @@ int main(int argc, char **argv) {
|
|||||||
initscr();
|
initscr();
|
||||||
atexit(quitProgram);
|
atexit(quitProgram);
|
||||||
game_state *gs;
|
game_state *gs;
|
||||||
|
hitfeed *hf;
|
||||||
|
|
||||||
gs = malloc(sizeof(game_state));
|
gs = malloc(sizeof(game_state));
|
||||||
|
hf = malloc(sizeof(hitfeed)); //zeroes the whole struct
|
||||||
initCoordinates(gs);
|
initCoordinates(gs);
|
||||||
gs->allowedMoves = DEFAULTTRIES;
|
gs->allowedMoves = DEFAULTTRIES;
|
||||||
gs->moves = 0;
|
gs->moves = 0;
|
||||||
gs->guesses = 0;
|
gs->guesses = 0;
|
||||||
gs->trollEnabled = 0;
|
gs->trollEnabled = 0;
|
||||||
|
hf->besthit = 0;
|
||||||
keypad(stdscr, FALSE);
|
keypad(stdscr, FALSE);
|
||||||
nonl(); //No new line. Prevents the new line when hitting enter
|
nonl(); //No new line. Prevents the new line when hitting enter
|
||||||
|
|
||||||
@@ -86,21 +89,23 @@ int main(int argc, char **argv) {
|
|||||||
initAlphabet(gs);
|
initAlphabet(gs);
|
||||||
drawAlphabet(gs, 0);
|
drawAlphabet(gs, 0);
|
||||||
drawFigure(gs, 0);
|
drawFigure(gs, 0);
|
||||||
startGame(gs);
|
startGame(gs, hf);
|
||||||
|
|
||||||
nl(); //enable newline again
|
nl(); //enable newline again
|
||||||
while (getch() != 10);
|
while (getch() != 10);
|
||||||
clear();
|
clear();
|
||||||
free(gs);
|
free(gs);
|
||||||
|
free(hf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void startGame(game_state *gs) {
|
void startGame(game_state *gs, hitfeed *hf) {
|
||||||
noecho(); /* Don't show the player input */
|
noecho(); /* Don't show the player input */
|
||||||
time(&gs->startTime);
|
time(&gs->startTime);
|
||||||
while (checkWin(gs)) {
|
while (checkWin(gs)) {
|
||||||
updateScreen(gs);
|
updateScreen(gs);
|
||||||
playerInput(gs);
|
printHitFeed(gs, hf);
|
||||||
|
playerInput(gs, hf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +249,7 @@ void drawGuessWord(game_state *gs) {
|
|||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
int playerInput(game_state *gs) {
|
int playerInput(game_state *gs, hitfeed *hf) {
|
||||||
int inp;
|
int inp;
|
||||||
int i, found = 0;
|
int i, found = 0;
|
||||||
inp = getch();
|
inp = getch();
|
||||||
@@ -260,7 +265,7 @@ int playerInput(game_state *gs) {
|
|||||||
if (fillCurrentWord(gs, inp)) {
|
if (fillCurrentWord(gs, inp)) {
|
||||||
//gs->moves++;
|
//gs->moves++;
|
||||||
gs->guesses++;
|
gs->guesses++;
|
||||||
trollHitScreen(gs, found);
|
trollHitScreen(gs, hf, found);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* no valid character found */
|
/* no valid character found */
|
||||||
@@ -338,7 +343,7 @@ void printGameStats(game_state *gs) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (z = 0; z <= gs->wordRows; z++) {
|
for (z = 0; z <= gs->wordRows; z++) {
|
||||||
animateLineClear(gs, gs->centery + z, -1); //-1 to speed up the animation
|
animateLineClear(gs, gs->centery + z, 0); //-1 to speed up the animation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,7 +399,7 @@ void readRandomLine(char *file, char *result) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void trollHitScreen(game_state *gs, int hits) {
|
void trollHitScreen(game_state *gs, hitfeed *hf, int hits) {
|
||||||
if (gs->trollEnabled) {
|
if (gs->trollEnabled) {
|
||||||
char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \
|
char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \
|
||||||
"Ultra Hit", "Monster Hit", "Rampage", \
|
"Ultra Hit", "Monster Hit", "Rampage", \
|
||||||
@@ -415,11 +420,50 @@ void trollHitScreen(game_state *gs, int hits) {
|
|||||||
flash(); /* flash the screen */
|
flash(); /* flash the screen */
|
||||||
mvprintw(5, centerDiff(gs->centerx, strings[i]), strings[i]);
|
mvprintw(5, centerDiff(gs->centerx, strings[i]), strings[i]);
|
||||||
refresh();
|
refresh();
|
||||||
|
/* Put the new streak to the hit feed */
|
||||||
|
addHitToFeed(hf, strings[i], hits);
|
||||||
animateLineClear(gs, 5, i);
|
animateLineClear(gs, 5, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addHitToFeed(hitfeed *hf, char *streak, int hit) {
|
||||||
|
/* Shift the array to put the new string to the first slot */
|
||||||
|
int i;
|
||||||
|
for (i = HITFEEDSLOTS - 1; i > 0; i--) {
|
||||||
|
strcpy(hf->history[i], hf->history[i - 1]);
|
||||||
|
}
|
||||||
|
/* Set the new first item */
|
||||||
|
strcpy(hf->history[0], streak);
|
||||||
|
if (hit > hf->besthit) {
|
||||||
|
strcpy(hf->beststreak, streak);
|
||||||
|
hf->besthit = hit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printHitFeed(game_state *gs, hitfeed *hf) {
|
||||||
|
if (gs->trollEnabled) {
|
||||||
|
/* print the stats at the bootom right corner */
|
||||||
|
int newMaxy = gs->maxy - HITFEEDSLOTS;
|
||||||
|
int newMaxx = gs->maxx - (gs->centerx / 2);
|
||||||
|
int i, z;
|
||||||
|
for (i = 0; i < HITFEEDSLOTS; i++) {
|
||||||
|
for (z = 0; z < gs->maxx - newMaxx; z++) {
|
||||||
|
mvprintw(newMaxy + i, newMaxx + z, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i = 0; i < HITFEEDSLOTS; i++) {
|
||||||
|
mvprintw(newMaxy + i, newMaxx, hf->history[i]);
|
||||||
|
}
|
||||||
|
/* Print best score above history feed */
|
||||||
|
char message[100];
|
||||||
|
sprintf(message, "Best: %s (%i hits)", hf->beststreak, hf->besthit);
|
||||||
|
mvprintw(newMaxy - 1, newMaxx, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void drawFigure(game_state *gs, int drawNext) {
|
void drawFigure(game_state *gs, int drawNext) {
|
||||||
int row = 6;
|
int row = 6;
|
||||||
int length = 8;
|
int length = 8;
|
||||||
@@ -442,7 +486,7 @@ void drawFigure(game_state *gs, int drawNext) {
|
|||||||
void animateLineClear(game_state *gs, int line, int offsetMultiplier) {
|
void animateLineClear(game_state *gs, int line, int offsetMultiplier) {
|
||||||
/* sleep and vanish */
|
/* sleep and vanish */
|
||||||
int j = 0, k = gs->maxx, usec;
|
int j = 0, k = gs->maxx, usec;
|
||||||
usec = (ANIM_DURATION * 1000000 + (100000 * offsetMultiplier)) / gs->maxx;
|
usec = ((ANIM_DURATION * 1000000) + (100000 * offsetMultiplier)) / gs->maxx;
|
||||||
|
|
||||||
for (j = 0; j <= gs->centerx; j++, k--) {
|
for (j = 0; j <= gs->centerx; j++, k--) {
|
||||||
mvprintw(line, j, "!");
|
mvprintw(line, j, "!");
|
||||||
|
14
hangman.h
14
hangman.h
@@ -10,6 +10,7 @@
|
|||||||
#define ALPHABET_NUM 36
|
#define ALPHABET_NUM 36
|
||||||
#define ANIM_DURATION 1
|
#define ANIM_DURATION 1
|
||||||
#define LINEBREAK 16 //Linebreak offset
|
#define LINEBREAK 16 //Linebreak offset
|
||||||
|
#define HITFEEDSLOTS 5
|
||||||
|
|
||||||
/* Data structures */
|
/* Data structures */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -36,6 +37,11 @@ typedef struct word {
|
|||||||
struct word *next;
|
struct word *next;
|
||||||
} word;
|
} word;
|
||||||
|
|
||||||
|
typedef struct hitfeed {
|
||||||
|
char history[HITFEEDSLOTS][50]; //Prints the last 3 streaks
|
||||||
|
char beststreak[50]; //Prints the best streak
|
||||||
|
int besthit; //Prints the best hit number e.g. Godlike with 15 hits
|
||||||
|
} hitfeed;
|
||||||
|
|
||||||
/* Function prototypes */
|
/* Function prototypes */
|
||||||
void quitProgram(void);
|
void quitProgram(void);
|
||||||
@@ -45,17 +51,19 @@ void initGuessWord(game_state *gs, char *filename);
|
|||||||
void initAlphabet(game_state *gs);
|
void initAlphabet(game_state *gs);
|
||||||
void drawAlphabet(game_state *gs, char usedchar);
|
void drawAlphabet(game_state *gs, char usedchar);
|
||||||
void drawGuessWord(game_state *gs);
|
void drawGuessWord(game_state *gs);
|
||||||
int playerInput(game_state *gs);
|
int playerInput(game_state *gs, hitfeed *hf);
|
||||||
int fillCurrentWord(game_state *gs, char validchar);
|
int fillCurrentWord(game_state *gs, char validchar);
|
||||||
int stackWrongCharacter(game_state *gs, char wrongchar);
|
int stackWrongCharacter(game_state *gs, char wrongchar);
|
||||||
int checkWin(game_state *gs);
|
int checkWin(game_state *gs);
|
||||||
void printGameStats(game_state *gs);
|
void printGameStats(game_state *gs);
|
||||||
void toLowerCase(char *str);
|
void toLowerCase(char *str);
|
||||||
void showStartScreen(game_state *gs);
|
void showStartScreen(game_state *gs);
|
||||||
void startGame(game_state *gs);
|
void startGame(game_state *gs, hitfeed *hf);
|
||||||
void showHelp(game_state *gs);
|
void showHelp(game_state *gs);
|
||||||
int centerDiff(int coordinate, char *str);
|
int centerDiff(int coordinate, char *str);
|
||||||
void readRandomLine(char *file, char *result);
|
void readRandomLine(char *file, char *result);
|
||||||
void trollHitScreen(game_state *gs, int hits);
|
void trollHitScreen(game_state *gs, hitfeed *hf, int hits);
|
||||||
|
void addHitToFeed(hitfeed *hf, char *streak, int hit);
|
||||||
void drawFigure(game_state *gs, int drawNext);
|
void drawFigure(game_state *gs, int drawNext);
|
||||||
void animateLineClear(game_state *gs, int line, int offsetMultiplier);
|
void animateLineClear(game_state *gs, int line, int offsetMultiplier);
|
||||||
|
void printHitFeed(game_state *gs, hitfeed *hf);
|
||||||
|
Reference in New Issue
Block a user