-
Notifications
You must be signed in to change notification settings - Fork 12
/
draw.c
416 lines (370 loc) · 10.7 KB
/
draw.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
#include "draw.h"
static int selectedStyle = FANCY;
static short foodColor = COLOR_GREEN;
static short snakeColor = -1;
static short snakeHeadColor = COLOR_YELLOW;
static short junkColor = COLOR_RED;
static short wallColor = -1;
static short drawWalls = 0;
static short drawScore = 0;
static Point tailLastPoint = {-1, -1};
static Point foodLastPoint = {-1, -1};
static int minX_, minY_, maxX_, maxY_, rows, cols;
static int snakeSize = 0;
int init_scr(int selStyle, int *maxX, int *maxY, short arcadeMode,
short dScore) {
selectedStyle = selStyle;
drawScore = dScore;
maxX_ = *maxX;
maxY_ = *maxY;
minX_ = 0;
minY_ = 0;
setlocale(LC_ALL, "");
// cbreak();
initscr();
if (has_colors() == FALSE) {
endwin();
printf("Your terminal does not support color\n");
return 0;
}
getmaxyx(stdscr, rows, cols);
if (maxX_ || maxY_)
drawWalls = 1;
// The width of the game board is half of the columns because I use two
// characters to represent one point ("██" or "▀ ")
if (maxX_ == 0)
maxX_ = cols / 2;
else
minX_ = (cols - maxX_ * 2) / 2;
if (maxY_ == 0)
maxY_ = rows - drawScore * 2;
else
minY_ = (rows - maxY_) / 2;
// maxX = 10;
// maxY = 10;
if (arcadeMode) {
minX_ = (cols - 60) / 2;
minY_ = (rows - 24) / 2;
maxX_ = 30;
maxY_ = 22;
drawScore = 1;
drawWalls = 1;
} // else if (selectedMode == SCREENSAVER || selectedMode == AUTOPILOT)
// teleport = 0;
if (cols < maxX_ * 2 || rows < maxY_) {
endwin();
if (arcadeMode)
printf("The arcade mode requires a minimum of 64 colums by 22 rows\n");
else
printf("Terminal is too small for the inputed dimensions");
return 0;
}
noecho();
keypad(stdscr, TRUE);
start_color();
use_default_colors();
init_pair(1, snakeColor, -1);
init_pair(2, foodColor, -1);
init_pair(3, junkColor, -1);
init_pair(4, wallColor, -1);
init_pair(5, snakeHeadColor, -1);
curs_set(0);
*maxX = maxX_;
*maxY = maxY_;
return 1;
}
// this funtion only draws the head and deletes the tail
// the rest of the body is not redraw unless the fancy or ascii mode are active
// in that case the head and the second section of the body are draw
void draw_snake(Snake *snake) {
SnakePart *sPart = snake->head;
SnakePart *sPart2 = snake->tail;
// deletes last point where the tail was
if (tailLastPoint.x != sPart2->x || tailLastPoint.y != sPart2->y) {
if (tailLastPoint.x != -1 && tailLastPoint.y != -1)
draw_point(tailLastPoint.x, tailLastPoint.y, 0, 7);
tailLastPoint.x = sPart2->x;
tailLastPoint.y = sPart2->y;
}
switch (selectedStyle) {
case ASCII:
// draw the head
draw_point(sPart->x, sPart->y, 0, 9);
// draw the second section of the body
draw_point(sPart->next->x, sPart->next->y, 0, 8);
// draw tail
draw_point(sPart2->x, sPart2->y, 0, 18);
break;
case FANCY:
// draw the head
if (sPart->next->x == sPart->x + 1 && sPart->next->y == sPart->y) {
draw_point(sPart->x, sPart->y, 0, 2);
} else if (sPart->next->x == sPart->x - 1 && sPart->next->y == sPart->y) {
draw_point(sPart->x, sPart->y, 0, 1);
} else if (sPart->next->x == sPart->x && sPart->next->y == sPart->y + 1) {
draw_point(sPart->x, sPart->y, 0, 3);
} else if (sPart->next->x == sPart->x && sPart->next->y == sPart->y - 1) {
draw_point(sPart->x, sPart->y, 0, 1);
}
if (snake->teleport) {
if ((sPart->next->x == 0 && sPart->x == maxX_ - 1 &&
sPart->next->y == sPart->y) ||
(sPart->next->x == maxX_ - 1 && sPart->x == 0 &&
sPart->next->y == sPart->y) ||
(sPart->next->x == sPart->x && sPart->next->y == 0 &&
sPart->y == maxY_ - 1) ||
(sPart->next->x == sPart->x && sPart->next->y == maxY_ - 1 &&
sPart->y == 0)) {
draw_point(sPart->x, sPart->y, 0, 1);
}
}
// draw the tail
if (sPart2->prev->x == sPart2->x + 1 && sPart2->prev->y == sPart2->y) {
draw_point(sPart2->x, sPart2->y, 0, 2);
} else if (sPart2->prev->x == sPart2->x - 1 &&
sPart2->prev->y == sPart2->y) {
draw_point(sPart2->x, sPart2->y, 0, 1);
} else if (sPart2->prev->x == sPart2->x &&
sPart2->prev->y == sPart2->y + 1) {
draw_point(sPart2->x, sPart2->y, 0, 3);
} else if (sPart2->prev->x == sPart2->x &&
sPart2->prev->y == sPart2->y - 1) {
draw_point(sPart2->x, sPart2->y, 0, 1);
}
// draw the second section of the body
if (snake->length > 2) {
SnakePart *sPart3 = sPart->next;
if (sPart3->prev->x == sPart3->x + 1 && sPart3->prev->y == sPart3->y &&
sPart3->next->x == sPart3->x && sPart3->next->y == sPart3->y + 1) {
draw_point(sPart3->x, sPart3->y, 0, 4);
} else if (sPart3->prev->x == sPart3->x &&
sPart3->prev->y == sPart3->y + 1 &&
sPart3->next->x == sPart3->x + 1 &&
sPart3->next->y == sPart3->y) {
draw_point(sPart3->x, sPart3->y, 0, 4);
} else if (sPart3->prev->x == sPart3->x + 1 &&
sPart3->prev->y == sPart3->y) {
draw_point(sPart3->x, sPart3->y, 0, 2);
} else if (sPart3->prev->x == sPart3->x &&
sPart3->prev->y == sPart3->y + 1) {
draw_point(sPart3->x, sPart3->y, 0, 3);
} else if (sPart3->next->x == sPart3->x + 1 &&
sPart3->next->y == sPart3->y) {
draw_point(sPart3->x, sPart3->y, 0, 2);
} else if (sPart3->next->x == sPart3->x &&
sPart3->next->y == sPart3->y + 1) {
draw_point(sPart3->x, sPart3->y, 0, 3);
} else {
draw_point(sPart3->x, sPart3->y, 0, 1);
}
}
break;
case FULL:
// Draw the head
draw_point(sPart->x, sPart->y, 0, 0);
// The tail is drawn like a half block to avoid the Ouroboros.
if (sPart2->prev->x == sPart2->x + 1 && sPart2->prev->y == sPart2->y) {
draw_point(sPart2->x, sPart2->y, 0, 6);
} else if (sPart2->prev->x == sPart2->x - 1 &&
sPart2->prev->y == sPart2->y) {
draw_point(sPart2->x, sPart2->y, 0, 3);
} else if (sPart2->prev->x == sPart2->x &&
sPart2->prev->y == sPart2->y + 1) {
draw_point(sPart2->x, sPart2->y, 0, 5);
} else if (sPart2->prev->x == sPart2->x &&
sPart2->prev->y == sPart2->y - 1) {
draw_point(sPart2->x, sPart2->y, 0, 2);
}
break;
case DOTS:
// draw the head
draw_point(sPart->x, sPart->y, 5, 1);
// draw the second section of the body
draw_point(sPart->next->x, sPart->next->y, 0, 1);
break;
}
}
void draw_food(Point food) {
if ((foodLastPoint.x != food.x || foodLastPoint.y != food.y) && food.x >= 0) {
switch (selectedStyle) {
case ASCII:
draw_point(food.x, food.y, 2, 11);
break;
case DOTS:
case FANCY:
draw_point(food.x, food.y, 2, 1);
break;
case FULL:
draw_point(food.x, food.y, 2, 0);
break;
}
foodLastPoint.x = food.x;
foodLastPoint.y = food.y;
}
}
void draw_junk(List *junkList) {
for (Node *it = junkList->first; it != NULL; it = it->next) {
Point *jpoint = (Point *)it->data;
switch (selectedStyle) {
case ASCII:
draw_point(jpoint->x, jpoint->y, 3, 10);
break;
case DOTS:
case FANCY:
draw_point(jpoint->x, jpoint->y, 3, 1);
break;
case FULL:
draw_point(jpoint->x, jpoint->y, 3, 0);
break;
}
}
}
/*
All the snake games in the terminal that I found use ascii characters
and lets be honest, they are kinda ugly.
So I play around using unicode blocks:
To get the spacing in the fancy mode we use this two unicode characters
▀ and █.
A point/SnakePart is represented in two characters.
We have to fill the gaps to connect the body parts
You may understand it better like this:
length | not gap filled | gap filled
----------|-----------------|-----------
1 | ▀ | ▀
2 | ▀ ▀ | ▀▀▀
3 | ▀ ▀ ▀ | ▀▀▀▀▀
so from this two characters ▀ and █ we have this two extra combination to fill
the gaps in the snake body ▀▀ and █▀
And like this we can have a really cool snake.
▀▀▀▀█
█▀▀▀▀ █
▀▀▀▀▀▀▀
This is the "sexy" part in the name sssnake!!!.
*/
void draw_point(int x, int y, short color, int type) {
int ty = y + minY_;
int tx = 2 * x + minX_;
if (tx > cols || tx < 0 || ty > rows || ty < 0)
return;
move(ty, tx);
attron(COLOR_PAIR(color));
switch (type) {
case 0:
addwstr(L"██");
break;
case 1:
addwstr(L"▀ ");
break;
case 2:
addwstr(L"▀▀");
break;
case 3:
addwstr(L"█ ");
break;
case 4:
addwstr(L"█▀");
break;
case 5:
addwstr(L"▄▄");
break;
case 6:
addwstr(L" █");
break;
case 7:
addstr(" ");
break;
case 8:
attron(A_BOLD);
addstr("o ");
attroff(A_BOLD);
break;
case 9:
attron(A_BOLD);
addstr("@ ");
attroff(A_BOLD);
break;
case 10:
attron(A_BOLD);
addstr("x ");
attroff(A_BOLD);
break;
case 11:
attron(A_BOLD);
addstr("8 ");
attroff(A_BOLD);
break;
case 12:
addstr("--");
break;
case 13:
addstr("| ");
break;
case 14:
addstr(" |");
break;
case 16:
addwstr(L"▚ ");
break;
case 17:
addwstr(L"▚▀");
break;
case 18:
attron(A_BOLD);
addstr(". ");
attroff(A_BOLD);
break;
}
attroff(COLOR_PAIR(color));
// move(rows, cols);
}
void draw_score(Snake *snake) {
move(minY_ + maxY_ + 1, minX_);
// attron(COLOR_PAIR(4));
if (snakeSize != snake->length) {
snakeSize = snake->length;
printw("Size %i ", snake->length);
}
// attroff(COLOR_PAIR(4));
}
void draw_walls() {
switch (selectedStyle) {
case DOTS:
case FULL:
case FANCY:
if (drawScore || drawWalls) {
for (int i = 0; i < maxX_; i++)
draw_point(i, maxY_, 4, 2);
}
if (drawWalls) {
for (int i = -1; i < maxX_ + 1; i++) {
draw_point(i, -1, 4, 2);
}
for (int i = 0; i < maxY_; i++) {
draw_point(-1, i, 4, 16);
}
for (int i = -1; i < maxY_ + 1; i++) {
draw_point(maxX_, i, 4, 16);
}
draw_point(-1, -1, 4, 17);
draw_point(-1, maxY_, 4, 17);
}
break;
case ASCII:
if (drawScore || drawWalls) {
for (int i = 0; i < maxX_; i++)
draw_point(i, maxY_, 4, 12);
}
if (drawWalls) {
for (int i = -1; i < maxX_ + 1; i++) {
draw_point(i, -1, 4, 12);
}
for (int i = -1; i < maxY_ + 1; i++) {
draw_point(-1, i, 4, 14);
}
for (int i = -1; i < maxY_ + 1; i++) {
draw_point(maxX_, i, 4, 13);
}
}
break;
}
}