-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathcompute.c
237 lines (227 loc) · 5.5 KB
/
compute.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <ncurses.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
struct user {
char name[20];
char lastName[20];
char age[3];
};
void startEngine(int highScore, struct user firstUser);
int computePrize(int score, int usedPrize);
void endGame(int score, int highScore, int diY, int diX, struct user firstUser);
void showDinasour(int diY, int diX);
void startMenu();
int computeTime(int delayTime);
int checkGame(int y, int x, int diY, int diX);
// Check if the game is going to be finished in this turn of loop
int checkGame(int y, int x, int diY, int diX) {
if (diY == y) {
if (abs((diX+14)-x) <= 4) {
return 0;
}
}
return 1;
}
// Make game faster
int computeTime(int delayTime) {
if (delayTime >= 250000) {
delayTime -= 900;
}
else if (delayTime >= 200000) {
delayTime -= 600;
}
else {
delayTime -= 200;
}
return delayTime;
}
// The very first menu
void startMenu() {
struct user firstUser;
int highScore;
// Read high score from file
FILE *highScoreFile;
highScoreFile = fopen("./highScore.txt", "r");
fscanf(highScoreFile, "%d", &highScore);
fclose(highScoreFile);
int maxX = getmaxx(stdscr)/2;
int maxY = getmaxy(stdscr)/2;
init_pair(3,COLOR_GREEN,COLOR_BLACK);
attron(COLOR_PAIR(3));
showTrex(maxY, maxX);
attroff(COLOR_PAIR(3));
// Get information from user
mvprintw(maxY+1, maxX-28, "Write inputs and press Enter to start Game.");
mvprintw(maxY+2, maxX-26, "When you had prize, fire it with 'k' key!");
mvprintw(maxY+3, maxX-21, "You can jump with space key!");
mvprintw(maxY+4, maxX-15, "Name: ");
getstr(firstUser.name);
mvprintw(maxY+5, maxX-15, "Last name: ");
getstr(firstUser.lastName);
mvprintw(maxY+6, maxX-15, "Age: ");
getstr(firstUser.age);
noecho();
startEngine(highScore, firstUser);
}
// Which dinosaur should be printed
void showDinasour(int diY, int diX) {
static int counter = 0;
if (counter == 0) {
dinasour1(diY, diX);
counter++;
}
else {
dinasour2(diY, diX);
counter--;
}
}
// Finishing function
void endGame(int score, int highScore, int diY, int diX, struct user firstUser) {
nodelay(stdscr, FALSE);
init_pair(2,COLOR_RED,COLOR_BLACK);
// Save high score
if (score > highScore) {
highScore = score;
FILE *highScoreFile;
highScoreFile = fopen("./highScore.txt", "w");
fprintf(highScoreFile, "%d", highScore);
fclose(highScoreFile);
}
int maxX = getmaxx(stdscr)/2;
int maxY = getmaxy(stdscr)/2;
attron(COLOR_PAIR(2));
showLoss(maxY, maxX);
mvprintw(diY-4, diX, " X-X ");
mvprintw(diY, diX, " ||");
char keyToExit = getch();
// Exit or reset game
if (keyToExit == 'r') {
attroff(COLOR_PAIR(2));
startEngine(highScore, firstUser);
}
else if (keyToExit == 'q') {
return;
}
else {
endGame(score, highScore, diY, diX, firstUser);
}
}
// Give user the arrow
int computePrize(int score, int usedPrize) {
if (score >= 20 && score <= 40 && usedPrize == 0) {
return 1;
}
else if (score >= 60 && score <= 80 && usedPrize <= 1) {
return 1;
}
else if (score >= 100 && score <= 120 && usedPrize <= 2) {
return 1;
}
return 0;
}
// The main engine!
void startEngine(int highScore, struct user firstUser) {
srand(time(NULL));
int x, y, diX=5, prize=0, usedPrize=0, score=0, delayTime = 300000
, gameStatus=1, cactusNum=0;
int maxX=getmaxx(stdscr);
x = maxX-20;
y = getmaxy(stdscr)-6;
int diY = y;
int arrowX=(diX+15), arrowY=(diY-3);
char userInput;
bool fire=FALSE;
int jumping=-1;
clear();
nodelay(stdscr, TRUE);
init_pair(1,COLOR_WHITE,COLOR_BLACK);
init_pair(4,COLOR_BLUE,COLOR_BLACK);
while (gameStatus == 1) {
userInput = getch();
// Show day or night
if ((score >= 20 && score <= 40) || (score >= 70 && score <= 90) || (score >= 120 && score <= 150)) {
clear();
attron(COLOR_PAIR(4));
moon(10, (maxX/2)-10);
}
else {
attron(COLOR_PAIR(1));
sun(10, (maxX/2)-10);
}
// clear arrow
if (fire) {
mvprintw(arrowY, arrowX-2, " ");
}
score++;
// Show informations
mvprintw(1, 6, "%s %s %s", firstUser.name, firstUser.lastName, firstUser.age);
mvprintw(1, getmaxx(stdscr)-9, "%d:%d", highScore, score);
// Use prize to destroy cactus
prize = computePrize(score, usedPrize);
mvprintw(3, 6, "Prize: %d", prize);
if (prize == 1) {
if (userInput == 'k') {
usedPrize++;
fire = TRUE;
}
}
if (fire) {
mvprintw(arrowY, arrowX, "*");
arrowX += 2;
}
if ((x+4)-arrowX <= 1 && fire) {
clearCactus1(y, x-1);
mvprintw(arrowY, arrowX-2, " ");
x = getmaxx(stdscr)-20;
fire = FALSE;
arrowX = diX+15;
}
// ----------
box(stdscr, ACS_VLINE, ACS_HLINE);
//for clearing screen
clearDinasourUp(diY, diX);
if (x <= 7) {
x = getmaxx(stdscr)-20;
cactusNum = rand() % 2;
}
// if input is equal to ' ' then jump
if (userInput == ' ' && jumping<0) {
diY -= 7;
jumping = 3;
}
showDinasour(diY, diX);
if (userInput == ' ') {
clearDinasourDown(diY, diX);
}
if (x-diX <= 7) {
x -= 10;
}
if (cactusNum == 0) {
cactus1(y, x);
}
else {
cactus2(y, x);
}
if (x-diX <= 7) {
x += 10;
}
gameStatus = checkGame(y, x, diY, diX);
// Bring back dinosaur
jumping--;
if (jumping==0) {
diY += 7;
}
mvhline(y+1, 1, '-', getmaxx(stdscr)-3);
refresh();
clearCactus1(y, x);
refresh();
usleep(delayTime);
x -= 7;
delayTime = computeTime(delayTime);
userInput = 'q';
}
// When code reaches here, means that user is loss
endGame(score, highScore, diY, diX, firstUser);
attroff(COLOR_PAIR(1));
}