-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5_add_kbhit.c
149 lines (137 loc) · 2.39 KB
/
5_add_kbhit.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
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
struct Snake
{
int x[100];
int y[100];
};
void mov_arr(struct Snake snake, int n);
int kbhit();
int main()
{
initscr();
curs_set(0);
int size_snake = 3;
struct Snake snake;
snake.x[0] = 35;
snake.y[0] = 10;
snake.x[1] = 35;
snake.y[1] = 11;
snake.x[2] = 35;
snake.y[2] = 12;
keypad(stdscr, true);
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (true) {
if (!kbhit()) {
/*30*/ for (int i = 0; i < size_snake; i++) {
snake.y[i]--;
}
for (int i = 0; i < size_snake; i++) {
if (i == 0) {
mvaddch(snake.y[0], snake.x[0], '%');
//unsleep(5000);
}
else {
mvaddch(snake.y[i], snake.x[i], '*');
//sleep(5000);
}
}
}
else {
switch (getch())
{
case KEY_UP:
for (int i = 0; i < size_snake; i++) {
snake.y[i]--;
}
for (int i = 0; i < size_snake; i++) {
if (i == 0) {
mvaddch(snake.y[0], snake.x[0], '%');
}
else {
mvaddch(snake.y[i], snake.x[i], '*');
}
}
break;
case KEY_DOWN:
for (int i = 0; i < size_snake; i++) {
snake.y[i]++;
}
for (int i = 0; i < size_snake; i++) {
if (i == 0) {
mvaddch(snake.y[0], snake.x[0], '%');
}
else {
mvaddch(snake.y[i], snake.x[i], '*');
}
}
break;
case KEY_RIGHT:
for (int i = 0; i < size_snake; i++) {
snake.x[i]++;
}
for (int i = 0; i < size_snake; i++) {
if (i == 0) {
mvaddch(snake.y[0], snake.x[0], '%');
}
else {
mvaddch(snake.y[i], snake.x[i], '*');
}
}
break;
case KEY_LEFT:
for (int i = 0; i < size_snake; i++) {
snake.x[i]--;
}
for (int i = 0; i < size_snake; i++) {
if (i == 0) {
mvaddch(snake.y[0], snake.x[0], '%');
}
else {
mvaddch(snake.y[i], snake.x[i], '*');
}
}
break;
}
}
sleep(2);
}
//sleep(10);
refresh();
getch();
endwin();
return 0;
}
void mov_arr(struct Snake snake, int n)
{
int dop_x1, dop_x2;
for (int i = 0; i < n; i++) {
if (i == 0) {
dop_x1 = snake.x[i];
snake.x[i]++;
}
else if (i % 2 == 0 && i != 0) {
dop_x1 = snake.x[i];
snake.x[i] = dop_x2;
}
else {/*60*/
dop_x2 = snake.x[i];
snake.x[i] = dop_x1;
}
}
}
int kbhit()
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
}
else {
return 0;
}
}