forked from uriid1/ascii-c-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
750 lines (672 loc) · 25.4 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
///******************************//#
///# Author: by uriid1 #//#
///# license: GNU GPL #//#
///# telegram: uriid1 #//#
///# Mail: appdurov@gmail.com #//#
//##****************************####
#include <stdio.h>
#include <string.h>
#include <ncurses.h>
// Const
#define nil -1
// Key Kode
#define vk_space 32
#define vk_enter 10
int key_pressed;
// Const Color
const short c_wall = 1;
const short c_apple = 2;
const short c_door = 3;
const short c_space = 4;
const short c_life = 5;
const short c_player = 6;
const short c_bullet = 6;
const short c_enemy = 7;
const short c_box = 8;
// Game Global var
bool EXIT = false;
short level = 1;
short score = 0;
short lifes = 3;
int bullet_shoot = false;
// index map object
#define i_wall 1
#define i_apple 2
#define i_door 3
#define i_space 4
#define i_life 5
#define i_exit 6
#define i_enemy 7
#define i_box 8
/////////////////////// MENU
// MENU
bool menu = true;
// logo
char menu_logo[] = "\t e Y8b 888 Y8b Y8b Y888P \n\t d8b Y8b 888 88e 888 88e 888 ,e e, Y8b Y8b Y8P ,8Y88b 888*,8, \n\t d888b Y8b 888 888b 888 888b 888 d88 88b Y8b Y8b Y 8 888 888 88 \n\t d888888888b 888 888P 888 888P 888 888 Y8b Y8b ee 888 888 \n\t d8888888b Y8b 888 88. 888 88. 888 .YeeP. Y8P Y 88 888 888 \n\t 888 888 \n\t 888 888 \n\t";
/////////////////////// LEVEL MAPS
// Map
short arr_size_x;
#define s_wall "###"
#define s_apple " @ "
#define s_empty " "
#define s_door "-^-"
#define s_life "(+)"
#define s_enemy "<^>"
#define s_box "|=|"
// Lvl 1
#define lvl_one_x 27
#define lvl_one_y 20
short level_one_size = lvl_one_x;
short lvl_one[lvl_one_y][lvl_one_x] = {
// Level one
{ 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 4, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 },
{ 4, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 2, 2, 2, 1 },
{ 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 },
{ 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 },
{ 4, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 },
{ 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
//
};
/////////////////////// COLORSHEME
// Set color
void SetColor() {
start_color();
init_pair(c_wall, COLOR_BLUE, COLOR_BLACK);
init_pair(c_apple, COLOR_WHITE, COLOR_BLACK);
init_pair(c_door, COLOR_RED, COLOR_BLACK);
init_pair(c_space, COLOR_BLACK, COLOR_BLACK);
init_pair(c_life, COLOR_GREEN, COLOR_BLACK);
init_pair(c_player, COLOR_MAGENTA, COLOR_BLACK);
init_pair(c_bullet, COLOR_GREEN, COLOR_BLACK);
init_pair(c_enemy, COLOR_BLACK, COLOR_RED);
init_pair(c_box, COLOR_YELLOW, COLOR_BLACK);
}
// Draw colored instance
void draw_instance(int color, char name[]) {
attron(COLOR_PAIR(color));
printw(name);
attroff(COLOR_PAIR(color));
}
/////////////////////// OBJECT
// Class
struct class_obj {
char symbol[20];
int x;
int y;
int direction;
};
// Create objects
struct class_obj player = {};
struct class_obj bullet = {};
struct class_obj enemy[5]={};
// Enemy movement
void enemy_move(short current_lvl[][arr_size_x], int index) {
switch(index) {
// hsp
case 0:
case 1:
case 2:
if (current_lvl[enemy[index].y][enemy[index].x] == i_wall ||
current_lvl[enemy[index].y][enemy[index].x] == i_door ||
current_lvl[enemy[index].y][enemy[index].x] == i_box)
{
enemy[index].direction *= -1;
}
enemy[index].y = enemy[index].y + enemy[index].direction;
break;
// vsp
case 3:
case 4:
case 5:
if (current_lvl[enemy[index].y][enemy[index].x] == i_wall ||
current_lvl[enemy[index].y][enemy[index].x] == i_door ||
current_lvl[enemy[index].y][enemy[index].x] == i_box)
{
enemy[index].direction *= -1;
}
enemy[index].x = enemy[index].x + enemy[index].direction;
break;
}
}
// Enemy clear
void clear_enemy() {
for (int i = 0; i < 5; i++) {
enemy[i].y = 0;
enemy[i].x = 0;
enemy[i].direction = 0;
}
}
// set obj Parametrs
void obj_init(struct class_obj *obj, int x, int y, int dir, char *objname) {
obj->x = x;
obj->y = y;
obj->direction = dir;
strcpy(obj->symbol, objname);
}
/////////////////////// PLAYER
// Playes method move
int dir_x;
int dir_y;
int dir_shoot;
void player_move(int key) {
// Key check
int key_left = ( key == KEY_LEFT ) ? 1 : 0;
int key_right = ( key == KEY_RIGHT ) ? 1 : 0;
int key_down = ( key == KEY_DOWN ) ? 1 : 0;
int key_up = ( key == KEY_UP ) ? 1 : 0;
// key dir
dir_x = key_right - key_left;
dir_y = key_down - key_up;
// Animation and direction shoot
if (dir_x == 0 && dir_y == 0) {
strcpy(player.symbol, "|0|");
} else {
if ( dir_x == 1 ) { dir_shoot = 1; strcpy(player.symbol, "|0>" );}
if ( dir_x == -1 ) { dir_shoot = -1; strcpy(player.symbol, "<0|" );}
if ( dir_y == -1 ) { dir_shoot = -2; strcpy(player.symbol, "/0\\" );}
if ( dir_y == 1 ) { dir_shoot = 2; strcpy(player.symbol, "\\0/" );}
}
player.x = player.x + dir_x;
player.y = player.y + dir_y;
}
// Collsiion
void player_collision(short current_lvl[][arr_size_x]) {
switch(current_lvl[player.y][player.x]) {
case i_wall: // wall
case i_box: // box
case i_door: // door
case i_space: // space
player.x = player.x - dir_x;
player.y = player.y - dir_y;
break;
// Apple collision
case i_apple:
current_lvl[player.y][player.x] = 0;
score = score - 1;
break;
// key
case i_life:
current_lvl[player.y][player.x] = 0;
lifes = lifes + 1;
break;
}
// Enemy collision
for (int i = 0; i < 5; i++) {
if (player.y == enemy[i].y && player.x == enemy[i].x) {
lifes = lifes - 1;
}
}
}
/////////////////////// BULLET
// Bullet
void bulet_collision(short current_lvl[][arr_size_x]) {
switch(current_lvl[bullet.y][bullet.x]) {
case i_wall:
case i_door:
case i_space:
bullet_shoot = false;
break;
}
// Kill Box
if (current_lvl[bullet.y][bullet.x] == i_box) {
current_lvl[bullet.y][bullet.x] = 0;
bullet_shoot = false;
}
// Kill Enemy
if (bullet_shoot == true) {
for (int i = 0; i < 5; i++) {
if (bullet.y == enemy[i].y && bullet.x == enemy[i].x) {
enemy[i].y = 0;
enemy[i].x = 0;
enemy[i].direction = 0;
bullet_shoot = false;
}
}
}
}
/////////////////////// LEVELs
// Set enemy and calck apple
void set_lvl_param(short current_lvl[][arr_size_x], int clx, int cly) {
static int i = 0;
for (int y = 0; y < cly; y++) {
for (int x = 0; x < clx; x++) {
if (current_lvl[y][x] == i_apple) {
score = score + 1;
}
if (current_lvl[y][x] == i_enemy) {
obj_init(&enemy[i], x, y, 1, "");
i++;
if (i >=5) { i = 0; }
}
}
}
}
// Check next lvl
bool next_lvl(short current_lvl[][arr_size_x]) {
if (current_lvl[player.y][player.x] == i_exit) {
score = 0;
level = level + 1;
return true;
}
return false;
}
// Draw Current Level
int current_lvl_x;
int current_lvl_y;
//
void draw_level(short lvl[][arr_size_x]) {
// Enemy move
for (int i = 0; i < 5; i++) { enemy_move(lvl, i); }
//
for (int y = 0; y < current_lvl_y; y++) {
for (int x = 0; x < current_lvl_x; x++) {
switch(lvl[y][x]) {
case i_wall: draw_instance(c_wall, s_wall); break;
case i_box: draw_instance(c_box, s_box); break;
case i_apple: draw_instance(c_apple, s_apple); break;
case i_door: draw_instance(c_door, s_door); break;
case i_space: draw_instance(c_space, s_empty); break;
case i_life: draw_instance(c_life, s_life); break;
default:
// Draw dynamic
if (x == player.x && y == player.y) {
draw_instance(c_player, player.symbol);
} else if (x == bullet.x && y == bullet.y) {
if (bullet_shoot) {
draw_instance(c_space, " ");
draw_instance(c_bullet, bullet.symbol);
draw_instance(c_space, " ");
} else {
draw_instance(c_space, s_empty);
}
} else if (x == enemy[0].x && y == enemy[0].y) {
draw_instance(c_enemy, s_enemy);
} else if (x == enemy[1].x && y == enemy[1].y) {
draw_instance(c_enemy, s_enemy);
} else if (x == enemy[2].x && y == enemy[2].y) {
draw_instance(c_enemy, s_enemy);
} else if (x == enemy[3].x && y == enemy[3].y) {
draw_instance(c_enemy, s_enemy);
} else if (x == enemy[4].x && y == enemy[4].y) {
draw_instance(c_enemy, s_enemy);
} else if (x == enemy[5].x && y == enemy[5].y) {
draw_instance(c_enemy, s_enemy);
} else {
draw_instance(c_space, s_empty);
}
break;
} // End switch
// Open door
if (score == 0) {
if (lvl[y][x] == i_door) {
lvl[y][x] = 0;
}
}
}
printw("\n");
}
}
/////////////////////// GAME
// Game Over
void game_over() {
EXIT = true;
endwin();
printf("Game Over!\n");
}
/// Update game
void game(int key, short current_lvl[][arr_size_x]) {
// Player
player_move(key);
player_collision(current_lvl);
// Bullet
bulet_collision(current_lvl);
// Draw map
draw_level(current_lvl);
// Over
if (lifes <= 0) { game_over(); }
}
// Init lvl
void level_init(short index_lvl) {
static bool init = true;
if (init == false) {
if (index_lvl == 1) { init = next_lvl(lvl_one); game(key_pressed, lvl_one); }
if (index_lvl == 2) { init = next_lvl(lvl_two); game(key_pressed, lvl_two); }
if (index_lvl == 3) { init = next_lvl(lvl_three); game(key_pressed, lvl_three); }
if (index_lvl == 4) { init = next_lvl(lvl_fo); game(key_pressed, lvl_fo); }
if (index_lvl == 5) { init = next_lvl(lvl_five); game(key_pressed, lvl_five); }
if (index_lvl == 6) { init = next_lvl(lvl_six); game(key_pressed, lvl_six); }
if (index_lvl == 7) { init = next_lvl(lvl_win); game(key_pressed, lvl_win); }
} else {
switch(index_lvl) {
case 1:
player.x = 8;
player.y = 16;
current_lvl_x = lvl_one_x;
current_lvl_y = lvl_one_y;
arr_size_x = level_one_size;
clear_enemy();
set_lvl_param(lvl_one, current_lvl_x, current_lvl_y);
init = false;
break;
case 2:
player.x = 3;
player.y = 2;
current_lvl_x = lvl_two_x;
current_lvl_y = lvl_two_y;
arr_size_x = level_two_size;
clear_enemy();
set_lvl_param(lvl_two, current_lvl_x, current_lvl_y);
init = false;
break;
case 3:
player.x = 2;
player.y = 6;
current_lvl_x = lvl_three_x;
current_lvl_y = lvl_three_y;
arr_size_x = level_three_size;
clear_enemy();
set_lvl_param(lvl_three, current_lvl_x, current_lvl_y);
init = false;
break;
case 4:
player.x = 22;
player.y = 1;
current_lvl_x = lvl_fo_x;
current_lvl_y = lvl_fo_y;
arr_size_x = level_fo_size;
clear_enemy();
set_lvl_param(lvl_fo, current_lvl_x, current_lvl_y);
init = false;
break;
case 5:
player.x = 4;
player.y = 1;
current_lvl_x = lvl_five_x;
current_lvl_y = lvl_five_y;
arr_size_x = level_five_size;
clear_enemy();
set_lvl_param(lvl_five, current_lvl_x, current_lvl_y);
init = false;
break;
case 6:
player.x = 8;
player.y = 13;
current_lvl_x = lvl_six_x;
current_lvl_y = lvl_six_y;
arr_size_x = level_six_size;
clear_enemy();
set_lvl_param(lvl_six, current_lvl_x, current_lvl_y);
init = false;
break;
case 7:
player.x = 23;
player.y = 5;
current_lvl_x = lvl_win_x;
current_lvl_y = lvl_win_y;
arr_size_x = level_win_size;
clear_enemy();
set_lvl_param(lvl_win, current_lvl_x, current_lvl_y);
init = false;
break;
}
}
}
/////////////////////// MAIN
// main func
int main(void) {
// init obj
// Player
obj_init(&player, 5, 5, 0, "|O|");
// Bullet
obj_init(&bullet, player.x, player.x, 0, "*");
// Start curses mode
initscr();
keypad(stdscr, TRUE);
// if not support color
if (has_colors() == false) {
endwin();
printf("Your terminal does not support color\n");
}
// Main loop
while (!EXIT) {
clear();
SetColor();
static int menu_item = 0;
if (menu == false) {
// GAME
printw("\n apples: %d lifes: %d level: %d \n\n", score, lifes, level);
level_init(level);
// Shoot
if (!bullet_shoot) {
bullet.x = player.x;
bullet.y = player.y;
if (key_pressed == vk_space) {
bullet.direction = dir_shoot;
bullet_shoot = true;
}
} else {
switch(bullet.direction) {
case 1: bullet.x = bullet.x + 1; break;
case -1: bullet.x = bullet.x - 1; break;
case 2: bullet.y = bullet.y + 1; break;
case -2: bullet.y = bullet.y - 1; break;
}
}
} else {
// MENU
if (key_pressed == KEY_UP) menu_item--;
if (key_pressed == KEY_DOWN) menu_item++;
if (menu_item >= 2) { menu_item = 2; }
if (menu_item <= 0) { menu_item = 0; }
printw("\n\n");
draw_instance(c_wall, menu_logo);
printw("\n\n\n\n");
printw("\t\t\t\t %s \n\n", menu_item == 0 ? " > START GAME <" : " start game");
printw("\t\t\t\t\t %s \n\n", menu_item == 1 ? "> INFO <" : " info");
printw("\t\t\t\t\t %s \n\n", menu_item == 2 ? "> EXIT <" : " exit");
printw("\n\n\n\n\n\n");
printw(" %s", "Develop: uriid1");
if (key_pressed == vk_enter) {
switch(menu_item) {
case 0:
menu = false;
break;
case 1:
break;
case 2:
endwin();
EXIT = true;
break;
}
}
}
//
key_pressed = getch();
if (key_pressed == 'q') { menu = true; }
}
// End curses mode
endwin();
return 0;
}