-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
142 lines (136 loc) · 4.19 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdlib.h>
#include <ncurses.h>
#include "highscores.h"
#include "inGame.h"
#include "save.h"
void game(gameState *save) {
int move, remainingTime = 2;
clock_t initialTime;
WINDOW * game = newwin(20, 50, 2, 2);
WINDOW * info = newwin(20, 25, 2, 53);
nodelay(game, true);
refresh();
keypad(game, true);
while (save->level <= 3 && save->blocked < save->level + 2 - save->found && remainingTime > 0) {
if (save->level == 1)
remainingTime = 90;
else if (save->level == 2)
remainingTime = 60;
else if (save->level == 3)
remainingTime = 45;
initialTime = clock();
startCharacter(&save->player, game);
setWindow(game, save);
wrefresh(game);
wrefresh(info);
box(info, 0, 0);
infoUpdate(info, save);
wrefresh(info);
while (save->found < save->level + 2 && save->blocked < save->level + 2 - save->found && remainingTime > 0) {
move = movePlayer(save, game);
if (move == (int)'\t'){
delwin(game);
delwin(info);
save->scenario[((save->player.y / 2) * 26) + save->player.x / 2] = '1';
return;
}
if (((double)(clock() - initialTime) / CLOCKS_PER_SEC) > 1) {
initialTime = clock();
save->timeSpent[save->level - 1]++;
remainingTime--;
}
infoUpdate(info, save);
wrefresh(info);
}
wclear(info);
if (save->blocked < save->level + 2 - save->found && remainingTime > 0) {
save->level++;
save->found = 0;
save->blocked = 0;
createLevel(save);
}
}
werase(info);
werase(game);
delwin(game);
delwin(info);
move(2, 0);
clrtobot();
refresh();
if (save->blocked < save->level + 2 - save->found && remainingTime > 0) {
save->score = transformToPlay (save->score.name, save->timeSpent[0] + save->timeSpent[1] + save->timeSpent[2], save->movement[0] + save->movement[1] + save->movement[2], save->level - 1);
appendPlay(save->score);
mvprintw(10, 15, "YOU WIN!!!!");
} else {
mvprintw(10, 15, "GAME OVER!");
addUnfinishedPlay(save);
} if (save->level > 1) {
mvprintw(11, 15, "%.2f POINTS!", save->score.score);
}
refresh();
save->level = -1;
}
/*
simple menu.
loops until user type a key associated with one of the options;
*/
void menu() {
char menuOption;
gameState save;
save.level = -1;
do {
menuOption = getch();
switch(menuOption) {
case 'N':
case 'n':
newGame(&save);
searchPlayer(&save.score);
game(&save);
break;
case 'S':
case 's':
saveGame(save);
break;
case 'l':
case 'L':
loadGame(&save);
game(&save);
break;
case 'P':
case 'p':
pauseGame(save);
while (getch() != 'p');
save = unpauseGame();
break;
case 'H':
case 'h':
showHighscores();
refresh();
break;
case 'Q':
case 'q':
addUnfinishedPlay(&save);
break;
case '\t':
if (save.level != -1) {
move(2, 0);
clrtobot();
game(&save);
}
break;
default:
break;
}
} while(menuOption != 'q' && menuOption != 'Q');
}
int main() {
srand(time(NULL));
initscr(); // initializes ncurses screen
cbreak(); // sets terminal to cbreak mode, a mode between raw and cooked which gives us instantly access to input, but still maintain some terminal processing features like ctrl+c.
noecho(); // disables echoing of typed characters
mvprintw(0, 11, "[N]ew game | [S]ave | [L]oad | [P]ause | [H]ighscore | [Q]uit");
refresh();
menu();
endwin(); // end ncurses
return 0;
}