-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNinjaAvoidanceGame.cpp
322 lines (269 loc) · 6.94 KB
/
NinjaAvoidanceGame.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <string.h>
void print_at_xy(int x, int y, char *val);
void display_score();
void init();
int zero_lives();
void set_game_state_over();
char get_input();
void update_player(char);
void update_wall();
void increment_score();
void decrement_lives();
void draw();
void draw_wall();
void draw_ninja();
void clean_up();
void clear_screen();
void display_message(const char *, int yOffset);
void update_ninja(char ch);
int collides_with_spike();
void display_count_down();
HANDLE _output_handle;
void hidecursor()
{
_output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(_output_handle, &info);
}
const int SCREEN_WIDTH = 12;
const int SCREEN_HEIGHT = 20;
int lives;
int game_state;
int GAME_STATE_OVER;
int GAME_STATE_PLAYING;
int GOAL_POINTS;
int WALL_SPEED;
int score;
char avatar[2];
char game_over_string[30];
char left_wall[60];
char right_wall[60];
char left_spike[3];
char right_spike[3];
char ninja[2];
int wall_y_pos;
int ninja_x;
int ninja_y;
int NINJA_SPEED;
int ninja_delta;
int left_wall_spike;
int right_wall_spike;
int immunity_count_down;
int main(){
init();
printf("\ninit");
system("@cls||clear");
//1000/30
//game loop
while(1){
if(immunity_count_down > 0){
immunity_count_down--;
}
clear_screen();
char ch = get_input();
//clear screen and quit
if(game_state == GAME_STATE_OVER && ch == 'q'){
system("@cls||clear");
break;
}
clear_screen();
update_wall();
update_ninja(ch);
draw();
if(collides_with_spike() && immunity_count_down <= 0){
decrement_lives();
immunity_count_down = 30;
}
if(zero_lives()){
set_game_state_over();
display_message(game_over_string, -2);
display_message("'q' to quit...", 0);
}
Sleep(100);
}
clean_up();
}
void init(){
score = 0;
lives = 3;
GOAL_POINTS = 10;
GAME_STATE_OVER = 1;
GAME_STATE_PLAYING = 2;
WALL_SPEED = 1;
ninja_x = 1;
ninja_y = SCREEN_HEIGHT/2;
NINJA_SPEED = 6;
ninja_delta = 0;
left_wall_spike = 0;
right_wall_spike = 0;
immunity_count_down = 30;
game_state = GAME_STATE_PLAYING;
wall_y_pos = -20;
strcpy(left_spike, "|>");
strcpy(right_spike, "<|");
strcpy(game_over_string, "GAME OVER");
strcpy(ninja, "X");
strcpy(left_wall, "|||>|||||||||||||>>||||||>>||||||>>||||");
strcpy(right_wall, "|||||||||<||||<||||||||<||||||<<||||||<");
hidecursor();
}
int zero_lives(){
if(lives == 0){
return 1;
}
return 0;
}
void set_game_state_over(){
game_state = GAME_STATE_OVER;
}
char get_input(){
char ch = 0;
if(kbhit()){
ch = getch();
}
return ch;
}
void update_player(char ch){
}
void increment_score(){
score += GOAL_POINTS;
}
void decrement_lives(){
lives--;
}
void draw(){
draw_wall();
draw_ninja();
display_score();
display_count_down();
}
void print_at_xy(int x, int y, char *val)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(_output_handle, coord);
printf("%s", (const char *)val);
fflush(stdout);
}
void display_score(){
char buffer[50] = {0};
sprintf(buffer, "SCORE: %4d LIVES: %d", score, lives);
print_at_xy(0, 0, buffer);
}
void clear_screen(){
char buffer[] = " ";
for(int i=0;i<3;i++)
{
print_at_xy(0, i, buffer);
}
}
void display_message(const char *message, int yOffset){
char buffer[100] = {0};
strcpy(buffer, message);
print_at_xy(SCREEN_WIDTH/2 - strlen(message)/2,
(SCREEN_HEIGHT/2 - 1)+yOffset, buffer);
}
void display_count_down(){
if(immunity_count_down > 0){
char buffer[3] = {0};
char *countdown = itoa(immunity_count_down/10, buffer, 10);
strcpy(buffer, countdown);
SetConsoleTextAttribute (_output_handle, FOREGROUND_BLUE);
display_message("GET READY!", -2);
display_message(buffer, 0);
SetConsoleTextAttribute (_output_handle, FOREGROUND_INTENSITY);
}
}
void clean_up(){
printf("Thanks for playing.");
}
void update_wall(){
wall_y_pos += WALL_SPEED;
if(wall_y_pos > 0){
wall_y_pos = -SCREEN_HEIGHT;
}
}
void update_ninja(char ch){
ninja_x += ninja_delta;
if(ninja_x == 1 && ch == 'j' && game_state == GAME_STATE_PLAYING){
ninja_delta = NINJA_SPEED;
ninja_x += ninja_delta;
increment_score();
}
else if(ninja_x == SCREEN_WIDTH-1 && ch == 'j' && game_state == GAME_STATE_PLAYING){
ninja_delta = -NINJA_SPEED;
ninja_x += ninja_delta;
increment_score();
}
else if(ninja_x <= 1){
ninja_delta = 0;
ninja_x = 1;
}
else if(ninja_x >= SCREEN_WIDTH-1){
ninja_delta = 0;
ninja_x = SCREEN_WIDTH-1;
}
if(immunity_count_down > 10 && lives < 3){
ninja_x = SCREEN_WIDTH/2;
ninja_y += 1;
if(ninja_y >= SCREEN_HEIGHT){
ninja_y = SCREEN_HEIGHT;
}
}
if(immunity_count_down < 10 && immunity_count_down > 1){
ninja_x = 1;
ninja_y = SCREEN_HEIGHT / 2;
}
}
int collides_with_spike(){
if(game_state == GAME_STATE_OVER){
return 0;
}
if(ninja_x == 1 && left_wall_spike == 1){
return 1;
}
else if(ninja_x == SCREEN_WIDTH-1 && right_wall_spike == 1){
return 1;
}
return 0;
}
void draw_wall(){
char wall_row[SCREEN_WIDTH+1];
int wall_index = wall_y_pos * -1;
left_wall_spike = 0;
right_wall_spike = 0;
for(int i=2;i<20;i++,wall_index++){
for(int j=1;j<SCREEN_WIDTH;j++){
wall_row[j] = ' ';
}
wall_row[SCREEN_WIDTH+1] = '\0';
wall_row[0] = '|';
wall_row[SCREEN_WIDTH] = '|';
if(left_wall[wall_index] == '>'){
wall_row[1] = '>';
if(i==SCREEN_HEIGHT/2){
left_wall_spike = 1;
}
}
if(right_wall[wall_index] == '<'){
wall_row[SCREEN_WIDTH-1] = '<';
if(i==SCREEN_HEIGHT/2){
right_wall_spike = 1;
}
}
print_at_xy(0, i, wall_row);
}
}
void draw_ninja(){
if(ninja_y >= SCREEN_HEIGHT) return;
SetConsoleTextAttribute (_output_handle, FOREGROUND_RED);
print_at_xy(ninja_x, ninja_y, ninja);
SetConsoleTextAttribute (_output_handle, FOREGROUND_INTENSITY);
}