-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgame.c
1054 lines (896 loc) · 34.8 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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
####--------------------------------####
#--# Author: by uriid1 #--#
#--# License: GNU GPLv3 #--#
#--# Telegram: @main_moderator #--#
#--# E-mail: appdurov@gmail.com #--#
####--------------------------------####
*/
// Includes
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ncurses.h>
// Key Kode
#define vk_space 32
#define vk_enter 10
int key_pressed = 0;
// 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;
const short c_hud = 1;
// Const state
bool EXIT = false;
// Game Global var
short level = 1;
short score = 0;
short lifes = 3;
int apples_in_level = 0;
int bullet_shoot = false;
// Level size
int current_lvl_x;
int current_lvl_y;
// Window width & Height
int w, h;
// 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_v 7
#define i_box 8
#define i_enemy_h 9
////////////////////
// Menu
///////////////////
const char *menu_logo[7] = {
" e Y8b 888 Y8b Y8b Y888P",
" d8b Y8b 888 88e 888 88e 888 ,e e, Y8b Y8b Y8P ,8Y88b 888*,8,",
" d888b Y8b 888 888b 888 888b 888 d88 88b Y8b Y8b Y 8 888 888 88",
" d888888888b 888 888P 888 888P 888 888 Y8b Y8b ee 888 888",
" d8888888b Y8b 888 88. 888 88. 888 .YeeP. Y8P Y 88 888 888",
" 888 888",
" 888 888"
};
// Get char len
int str_len(const char* str) {
int size = 0;
while(*str++) ++size;
return size;
}
// Get logo size
int logo_h_size = sizeof(menu_logo)/sizeof(menu_logo[0]);
// Get logo len
int get_logo_w_size(void) {
int logo_w_size = 1;
for (int i = 0; i < logo_h_size; i++) {
int len = str_len(menu_logo[i]);
if (len > logo_w_size) {
logo_w_size = str_len(menu_logo[i]);
}
}
return logo_w_size;
}
// Draw the logo
int logo_w_size = 1;
void draw_logo(int h, int w) {
// Get w size
if (logo_w_size == 1) {
logo_w_size = get_logo_w_size() / 2;
}
// Draw
attron(COLOR_PAIR(c_hud));
for (int i = 0; i < logo_h_size; i++) {
mvprintw(3 + i /* Logo Y pos */, w/2 - logo_w_size, menu_logo[i]);
}
attroff(COLOR_PAIR(c_hud));
}
/////////////////
// LEVEL MAPS
////////////////
// Map
short arr_size_x;
#define s_wall "///"
#define s_exit "***"
#define s_apple "(`)"
#define s_empty " "
#define s_door "-^-"
#define s_life "(+)"
#define s_enemy "(-)"
#define s_box "[=]"
#define s_space "..."
////////////////
// Level one
///////////////
#define lvl_one_x 27
#define lvl_one_y 20
#define level_one_size lvl_one_x
short lvl_one[lvl_one_y][lvl_one_x] = {
{ 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, 1 },
{ 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, 0, 1 },
{ 1, 0, 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 },
{ 1, 0, 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 },
{ 1, 0, 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 },
{ 1, 0, 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 },
{ 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, 0, 1 },
{ 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 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2, 1, 2, 2, 2, 2, 2, 2, 1 },
{ 1, 2, 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 },
{ 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, 2, 1 },
{ 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, 2, 1 },
{ 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, 2, 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, 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_RED, COLOR_BLACK);
init_pair(c_box, COLOR_YELLOW, COLOR_BLACK);
}
// Draw colored instance
#define td_indent 2 // Top & down ident
#define symbol_count 3
void draw_instance(int y, int x, int color, char name[]) {
attron(COLOR_PAIR(color));
// Win offset
int win_xoffset = w/2;
int win_yoffset = h/2;
// Level offset
int lvl_xoffset = (current_lvl_x/2)*symbol_count+(current_lvl_x%2);
int lvl_yoffset = (current_lvl_y+(td_indent*2))/2-(1 /* +1 indent hud */+(current_lvl_y%2));
mvprintw(
/* Y pos */ ceil(win_yoffset - lvl_yoffset) + (y+td_indent),
/* X pos */ ceil(win_xoffset - lvl_xoffset) + (x*symbol_count),
/* S pos */ name
);
attroff(COLOR_PAIR(color));
}
//////////////
// OBJECT
//////////////
// Class
struct class_obj {
char symbol[20];
int hsp, vsp;
int x, 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) {
if (enemy[index].direction == 1 || enemy[index].direction == -1) {
// VSP
if (current_lvl[enemy[index].y + enemy[index].vsp][enemy[index].x] == i_wall ||
current_lvl[enemy[index].y + enemy[index].vsp][enemy[index].x] == i_door ||
current_lvl[enemy[index].y + enemy[index].vsp][enemy[index].x] == i_box)
{
enemy[index].direction *= -1;
}
enemy[index].vsp = 1 * enemy[index].direction;
enemy[index].y += enemy[index].vsp;
return;
}
if (enemy[index].direction == 2 || enemy[index].direction == -2){
// HSP
if (current_lvl[enemy[index].y][enemy[index].x + enemy[index].hsp] == i_wall ||
current_lvl[enemy[index].y][enemy[index].x + enemy[index].hsp] == i_door ||
current_lvl[enemy[index].y][enemy[index].x + enemy[index].hsp] == i_box)
{
enemy[index].direction *= -1;
}
enemy[index].hsp = 1 * enemy[index].direction;
enemy[index].x += enemy[index].hsp;
return;
}
}
// Enemy update
void enemy_update(short current_lvl[][arr_size_x]) {
for (int i = 0; i < sizeof(enemy)/sizeof(enemy[0]); i++) {
enemy_move(current_lvl, i);
}
}
// Enemy clear
void clear_enemy() {
for (int i = 0; i < sizeof(enemy)/sizeof(enemy[0]); 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.hsp = 1 * dir_x;
player.vsp = 1 * dir_y;
if (player.hsp != 0) {
player.vsp = 0;
} else if (player.vsp != 0) {
player.hsp = 0;
}
player.x += player.hsp;
player.y += player.vsp;
}
// Collsiion
void player_collision(short current_lvl[][arr_size_x]) {
switch(current_lvl[player.y][player.x]) {
// Collision
case i_wall: // wall
case i_box: // box
case i_door: // door
case i_space: // space
player.x -= player.hsp;
player.y -= player.vsp;
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 (short i = 0; i < sizeof(enemy)/sizeof(enemy[0]); i++) {
if (player.y == enemy[i].y &&
player.x == enemy[i].x)
{
lifes = lifes - 1;
}
}
}
/////////////
// BULLET
////////////
void bullet_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) {
for (int i = 0; i < sizeof(enemy)/sizeof(enemy[0]); i++) {
// Vertival collision
if (enemy[i].vsp != 0) {
if (bullet.y == enemy[i].y &&
bullet.x == enemy[i].x ||
bullet.y - enemy[i].direction == enemy[i].y &&
bullet.x == enemy[i].x)
{
enemy[i].y = 0;
enemy[i].x = 0;
enemy[i].direction = 0;
bullet_shoot = false;
break;
}
}
// Horizontal collision
if (enemy[i].hsp != 0) {
if (bullet.y == enemy[i].y &&
bullet.x == enemy[i].x ||
bullet.y == enemy[i].y &&
bullet.x - enemy[i].direction == enemy[i].x)
{
enemy[i].y = 0;
enemy[i].x = 0;
enemy[i].direction = 0;
bullet_shoot = false;
break;
}
}
}
}
}
/////////////
// 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) {
apples_in_level = apples_in_level + 1;
}
if (current_lvl[y][x] == i_enemy_v) {
obj_init(&enemy[i], x, y, 1, "");
i++;
} else if (current_lvl[y][x] == i_enemy_h) {
obj_init(&enemy[i], x, y, 2, "");
i++;
}
if (i >= sizeof(enemy)/sizeof(enemy[0])) {
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;
apples_in_level = 0;
level = level + 1;
return true;
}
return false;
}
// Draw Current Level
void draw_level(short lvl[][arr_size_x]) {
for (int y = 0; y < current_lvl_y; y++) {
for (int x = 0; x < current_lvl_x; x++) {
switch(lvl[y][x]) {
// Draw static object
case i_wall: draw_instance(y, x, c_wall, s_wall); break;
case i_box: draw_instance(y, x, c_box, s_box); break;
case i_apple: draw_instance(y, x, c_apple, s_apple); break;
case i_door: draw_instance(y, x, c_door, s_door); break;
case i_space: draw_instance(y, x, c_wall, s_space); break;
case i_life: draw_instance(y, x, c_life, s_life); break;
case i_exit: draw_instance(y, x, c_life, s_exit); break;
// Draw dynamic object
default:
// Draw player
if (x == player.x && y == player.y) {
draw_instance(y, x, c_player, player.symbol);
break;
}
// Draw bullet
if (x == bullet.x && y == bullet.y) {
if (!bullet_shoot) {
break;
}
draw_instance(y, x, c_bullet, bullet.symbol);
}
// Draw enemy
for (int i = 0; i < 5; i++) {
if (x == enemy[i].x && y == enemy[i].y) {
draw_instance(y, x, c_enemy, s_enemy);
break;
}
}
break;
}
// Open door
if (score == apples_in_level) {
if (lvl[y][x] == i_door) {
lvl[y][x] = 0;
}
}
}
}
}
//////////////
// GAME
/////////////
// Hud
void draw_hud() {
if (score == 0) {
mvprintw(1, 2, "apples: %d%% lifes: %d level: %d\n", score, lifes, level);
return;
}
mvprintw(1, 2, "apples: %d%% lifes: %d level: %d\n", ((score * 100) / apples_in_level), lifes, level);
}
// Game Over
void game_over() {
EXIT = true;
endwin();
printf("Game Over!\n");
}
void bullet_update(void) {
// 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.hsp = 1;
bullet.vsp = 0;
break;
case -1:
bullet.hsp = -1;
bullet.vsp = 0;
break;
case 2:
bullet.hsp = 0;
bullet.vsp = 1;
break;
case -2:
bullet.hsp = 0;
bullet.vsp = -1;
break;
}
bullet.x += bullet.hsp;
bullet.y += bullet.vsp;
}
}
// Update game
void game_update(int key, short current_lvl[][arr_size_x]) {
// Player
player_move(key);
player_collision(current_lvl);
// Enemy
enemy_update(current_lvl);
// Bullet
bullet_update();
bullet_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) {
if (index_lvl == 1) { init = next_lvl(lvl_one); game_update(key_pressed, lvl_one); }
if (index_lvl == 2) { init = next_lvl(lvl_two); game_update(key_pressed, lvl_two); }
if (index_lvl == 3) { init = next_lvl(lvl_three); game_update(key_pressed, lvl_three); }
if (index_lvl == 4) { init = next_lvl(lvl_fo); game_update(key_pressed, lvl_fo); }
if (index_lvl == 5) { init = next_lvl(lvl_five); game_update(key_pressed, lvl_five); }
if (index_lvl == 6) { init = next_lvl(lvl_six); game_update(key_pressed, lvl_six); }
if (index_lvl == 7) { init = next_lvl(lvl_win); game_update(key_pressed, lvl_win); }
return;
}
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
//////////////
int main(void) {
// Start curses mode
initscr();
keypad(stdscr, TRUE);
savetty();
cbreak();
noecho();
timeout(0);
leaveok(stdscr, TRUE);
curs_set(0);
// if not support color
if (!has_colors()) {
endwin();
printf("Your terminal does not support color\n");
}
////////////////////
// Enum game state
///////////////////
typedef enum {
STATE_MENU,
STATE_INFO,
STATE_GAME,
STATE_EXIT,
} game_states;
// Init current state
game_states current_state;
current_state = STATE_MENU;
//////////////
// init obj
//////////////
// Player
obj_init(&player, 5 /* x pos */, 5 /* y pos */, 0, "|O|");
// Bullet
obj_init(&bullet, player.x, player.x, 0, " * ");
////////////////
// Main loop
///////////////
// Menu item
// Item start game
const char *item_start_game[2] = {
"> START GAME <",
"start game",
};
// Item info
const char *item_info[2] = {
"> INFO <",
"info",
};
// Item exit
const char *item_exit[2] = {
"> EXIT <",
"exit",
};
while (!EXIT) {
// Color
SetColor();
// Get window width & Height
getmaxyx(stdscr, h, w);
// Menu state
static int menu_item = 0;
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;
// In menu state
switch(current_state) {
// Menu
case STATE_MENU:
// Logo
draw_logo(h, w);
///////////
// Items
//////////
// Item start game
int select_start_game = menu_item == 0 ? 0 : 1;
mvprintw(h/2 - logo_h_size + 9, w/2 - str_len(item_start_game[select_start_game])/2, item_start_game[select_start_game]);
// Item info
int select_info = menu_item == 1 ? 0 : 1;
mvprintw(h/2 - logo_h_size + 11, w/2 - str_len(item_info[select_info])/2, item_info[select_info]);
// Item exit
int select_exit = menu_item == 2 ? 0 : 1;
mvprintw(h/2 - logo_h_size + 13, w/2 - str_len(item_exit[select_exit])/2, item_exit[select_exit]);
// By dev
mvprintw(h-2, 2, "Develop: uriid1");
// Draw box
attron(COLOR_PAIR(c_hud));
box(stdscr, 0, 0);
attron(COLOR_PAIR(c_hud));
// Click handler
if (key_pressed == vk_enter) {
switch(menu_item) {
case 0:
current_state = STATE_GAME;
break;
case 1:
// Info page is dev
current_state = STATE_INFO;
break;
case 2:
current_state = STATE_EXIT;
break;
}
}