-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
244 lines (195 loc) · 5.07 KB
/
app.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
#define W 600
#define H 600
#define SNAKE_WIDTH 10
#define SNAKE_MAXSIZE 100
#define BORDER_WIDTH 10
extern int jsSetInterval(void (*callback)(), int timeout);
extern void jsClearInterval(int index);
void runCallback(void (*callback)()) {
return callback();
}
extern void print(int message);
extern void alert(int message);
extern void jsFillRect(int x, int y, int w, int h, int color);
extern void jsClearRect(int x, int y, int w, int h);
extern int random(int min, int max);
typedef struct Rect {
int x;
int y;
int w;
int h;
} Rect;
typedef enum Type {
WALL,
SNAKE,
} Type;
typedef struct Object {
Type t;
Rect r; // Bounds
} Object;
/***
*
* Snake (as stack) start
*
*/
typedef struct Snake {
int end_index;
int start_index;
// 0 - right, 1 down, 2 left, 3 top
int direction;
Rect rects[SNAKE_MAXSIZE];
} Snake;
Snake snake = {-1, -1};
void snake_insert(int x, int y)
{
if (snake.end_index == SNAKE_MAXSIZE - 1) {
// rewind queue
for (int i = 0; i < SNAKE_MAXSIZE; i++) {
snake.rects[i] = snake.rects[i + snake.start_index];
}
snake.end_index = snake.end_index - snake.start_index;
snake.start_index = 0;
}
if (snake.end_index < SNAKE_MAXSIZE - 1) {
if (snake.start_index == - 1) {
snake.start_index = 0;
}
snake.end_index = snake.end_index + 1;
Rect rect = {x, y, SNAKE_WIDTH, SNAKE_WIDTH};
snake.rects[snake.end_index] = rect;
} else {
print(1);
}
} /* End of insert() */
void snake_delete()
{
if (snake.start_index != - 1 && snake.start_index < snake.end_index)
{
snake.start_index = snake.start_index + 1;
}
} /* End of delete() */
Rect get_snake_head() {
return snake.rects[snake.end_index];
}
void snake_growth() {
Rect head = get_snake_head();
switch (snake.direction) {
case 0: // right
snake_insert(head.x + SNAKE_WIDTH, head.y);
break;
case 1: // bottom
snake_insert(head.x, head.y + SNAKE_WIDTH);
break;
case 2: // left
snake_insert(head.x - SNAKE_WIDTH, head.y);
break;
case 3: // top
snake_insert(head.x, head.y - SNAKE_WIDTH);
break;
}
}
void snake_change_direction (new_direction) {
int delta = new_direction - snake.direction;
if (delta != 2 && delta != -2) {
snake.direction = new_direction;
}
}
/**
* Snake end
**/
// border
Rect borders[4] = {
{ 0, 0, W, BORDER_WIDTH }, // Top Wall
{ 0, H - BORDER_WIDTH, W, BORDER_WIDTH }, // Bottom Wall
{ 0, 0, BORDER_WIDTH, H }, // Left Wall
{ W - BORDER_WIDTH, 0, BORDER_WIDTH, H }, // Right Wall
};
Rect foods[100] = {
{ 400, 300, SNAKE_WIDTH, SNAKE_WIDTH}
};
food_count = 1;
void remove_food(int index) {
for (int i = index; i < food_count; i++)
foods[i] = foods[i+1];
food_count = food_count - 1;
}
int board_point_count_w = (W - BORDER_WIDTH * 2) / SNAKE_WIDTH - 1;
int board_point_count_h = (H - BORDER_WIDTH * 2) / SNAKE_WIDTH - 1;
void add_food_randomly() {
int x = random(0, board_point_count_w) * SNAKE_WIDTH + BORDER_WIDTH;
int y = random(0, board_point_count_h) * SNAKE_WIDTH + BORDER_WIDTH;
Rect food = {x, y, SNAKE_WIDTH, SNAKE_WIDTH};
foods[food_count] = food;
food_count++;
}
void draw() {
jsClearRect(0, 0, W, H);
for (int i = 0; i < 4; i++) {
Rect r = borders[i];
jsFillRect(r.x, r.y, r.w, r.h, 0);
}
for (int i = 0; i < food_count; i++) {
Rect r = foods[i];
jsFillRect(r.x, r.y, r.w, r.h, 0xFFA500);
}
for (int i = snake.start_index; i <= snake.end_index; i++) {
Rect r = snake.rects[i];
jsFillRect(r.x, r.y, r.w, r.h, 0xFF0000);
}
}
int valueInRange(int value, int min, int max) {
return (value >= min) && (value < max);
}
int mainInterval;
void move_snake() {
snake_growth();
snake_delete();
Rect head = get_snake_head();
int border_touched = 0;
for (int i = 0; i < 4; i++) {
Rect border = borders[i];
if (valueInRange(head.x, border.x, border.x + border.w) && valueInRange(head.y, border.y, border.y + border.h)) {
border_touched = 1;
break;
}
}
if (border_touched) {
alert(1);
jsClearInterval(mainInterval);
return;
}
for (int i = 0; i < food_count; i++) {
Rect food = foods[i];
if (head.x == food.x && head.y == food.y) {
remove_food(i);
if (food_count < 10) {
add_food_randomly();
}
if (food_count < 7) {
add_food_randomly();
}
snake_growth();
break;
}
}
draw();
}
void handleKeyPress(int direction_code) {
snake_change_direction(direction_code);
}
void handleClick(int x, int y) {
// Object point = {.r = {x, y, 10, 10}}
// map[world_count] = point;
// world_count++;
// draw();
}
int main() {
snake_insert(W / 2, H / 2);
snake_growth();
snake_growth();
snake_growth();
snake_growth();
draw();
mainInterval = jsSetInterval(move_snake, 200);
return 0;
}