-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys_handler.c
80 lines (69 loc) · 1.29 KB
/
keys_handler.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 "main.h"
int keys(playground_t *playground, char ch);
int arrow_keys(playground_t *playground, char ch);
void *keys_handler(void *playground)
{
playground_t *pg = (playground_t *) playground;
while (true)
{
char ch = getchar();
if (keys(pg, ch))
{
status.game_over = true;
return (NULL);
}
}
}
int keys(playground_t *playground, char ch)
{
switch (ch)
{
case 'q':
return (1);
case 'w':
change_snake_direction(&(playground->snake), UP);
break;
case 's':
change_snake_direction(&(playground->snake), DOWN);
break;
case 'd':
change_snake_direction(&(playground->snake), RIGHT);
break;
case 'a':
change_snake_direction(&(playground->snake), LEFT);
break;
case 27:
ch = getchar();
if (arrow_keys(playground, ch))
{
return (keys(playground, ch));
}
}
return (0);
}
int arrow_keys(playground_t *playground, char ch)
{
if (ch == '[')
{
ch = getchar();
switch (ch)
{
case 'A':
change_snake_direction(&(playground->snake), UP);
break;
case 'B':
change_snake_direction(&(playground->snake), DOWN);
break;
case 'C':
change_snake_direction(&(playground->snake), RIGHT);
break;
case 'D':
change_snake_direction(&(playground->snake), LEFT);
}
}
else
{
return (1);
}
return (0);
}