-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.c
117 lines (93 loc) · 2 KB
/
playground.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
#include "main.h"
int init_playground(playground_t *playground)
{
if (init_snake(&playground->snake))
return (1);
random_food(&playground->food);
return (0);
}
void clear_playground(playground_t *playground)
{
for (int row = 0; row < ROWS; row++)
{
if (row == 0 || row == ROWS - 1)
{
for (int column = 0; column < COLUMNS; column++)
(playground->grid)[row][column] = BLOCK;
continue;
}
(playground->grid)[row][0] = BLOCK;
for (int column = 1; column < COLUMNS - 1; column++)
(playground->grid)[row][column] = '\0';
(playground->grid)[row][COLUMNS - 1] = BLOCK;
}
}
void refresh_playground(playground_t *playground)
{
snake_t *snake = &playground->snake;
pointnode_t *tail = snake->tail;
point_t *food = &playground->food;
clear_playground(playground);
while (tail)
{
point_t *point = &(tail->point);
if (tail->prev)
(playground->grid)[point->y][point->x] = SNAKE_BODY;
else
{
switch ((playground->grid)[point->y][point->x])
{
case SNAKE_BODY:
case BLOCK:
status.game_over = true;
return;
default:
(playground->grid)[point->y][point->x] = SNAKE_HEAD;
}
}
tail = tail->prev;
}
while ((playground->grid)[food->y][food->x])
{
if ((playground->grid)[food->y][food->x] == SNAKE_HEAD)
{
status.score++;
increase_snake(snake);
}
random_food(food);
}
(playground->grid)[food->y][food->x] = FOOD;
}
void render_playground(playground_t *playground)
{
refresh_playground(playground);
if (!status.game_over)
{
system("clear");
print_number(status.score);
for (int row = 0; row < ROWS; row++)
{
for (int column = 0; column < COLUMNS; column++)
{
switch ((playground->grid)[row][column])
{
case SNAKE_HEAD:
printf(BOX_LIGHT_3);
break;
case SNAKE_BODY:
printf(BOX_LIGHT_2);
break;
case BLOCK:
printf(BOX);
break;
case FOOD:
printf(GRN BOX_LIGHT_3 RESET);
break;
default:
printf(BOX_LIGHT_1);
}
}
printf("\n");
}
}
}