-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
80 lines (66 loc) · 1.26 KB
/
main.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
#include "utils.h"
#include "game.h"
#include "screen.h"
direction get_direction(char input)
{
direction direction;
switch (input) {
case 'w':
case 'W':
direction = DIR_UP;
break;
case 'd':
case 'D':
direction = DIR_RIGHT;
break;
case 's':
case 'S':
direction = DIR_DOWN;
break;
case 'a':
case 'A':
direction = DIR_LEFT;
break;
default:
direction = DIR_UNKNOWN;
}
return direction;
}
int main(int argc, char *argv[])
{
char input;
direction direction;
screen_title();
screen_init();
while (1) {
screen_reinit();
game_init(screen_draw_cell);
screen_draw();
screen_draw_score();
while(1) {
game_draw();
if (game_is_finsihed()) {
if ( game_won() ) {
screen_draw_you_won();
} else {
screen_draw_game_over();
}
wait_kbhit();
break;
}
input = wait_kbhit();
if ( 'q' == input || 'Q' == input ) {
if (screen_draw_and_ask_restart()) {
break;
}
}
direction = get_direction(input);
game_move(direction);
screen_draw_score();
if (game_moved()) {
game_add_random_tile();
}
}
}
return 0;
}