-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
166 lines (146 loc) · 3.54 KB
/
snake.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
#include "snake.h"
#include "init.h"
snake *sinit(float x, float y, int kierunek, int tab[][200])
{
snake* ptr;
ptr = (snake*)malloc(sizeof(snake));
snake_elem* elem_ptr;
elem_ptr = (snake_elem*)malloc(sizeof(snake_elem)); //glowa
elem_ptr->x = x;
elem_ptr->y = y;
elem_ptr->direction = kierunek;
elem_ptr->last = 0;
elem_ptr->next = NULL;
ptr->head = elem_ptr;
elem_ptr = (snake_elem*)malloc(sizeof(snake_elem)); //ogon
elem_ptr->x = x - SKOK;
elem_ptr->y = y;
elem_ptr->direction = kierunek;
elem_ptr->last = kierunek;
elem_ptr->next = ptr->head;
ptr->last = elem_ptr;
int tabx = x/SKOK, taby = y/SKOK;
tab[tabx][taby] = WAZ;
tab[tabx-1][taby] = WAZ;
return ptr;
}
void przesun(snake* ptr, int kierunek, int tab[][200], int width, bool *wallcollision)
{
float x = ptr->head->x;
float y = ptr->head->y;
switch (kierunek)
{
case GORA:
y -= SKOK;
break;
case DOL:
y += SKOK;
break;
case LEWO:
x -= SKOK;
break;
case PRAWO:
x += SKOK;
break;
}
if (x >= width)
x = 0;
else if (x < 0)
x = width - SKOK;
int tabx = (ptr->last->x) / SKOK, taby = (ptr->last->y) / SKOK; //usuniecie ogona
tab[tabx][taby] = PUSTEPOLE;
ptr->last->x = x;
ptr->last->y = y;
ptr->last->last = 0;
ptr->last->direction = kierunek;
tabx = x / SKOK; taby = y / SKOK; //dodanie glowy
if (tab[tabx][taby] == SCIANA)
*wallcollision = true;
else
*wallcollision = false;
tab[tabx][taby] = WAZ;
if (ptr->last->next != NULL)
{
snake_elem* elem_ptr = ptr->last;
ptr->last = ptr->last->next;
ptr->head->last = kierunek;
ptr->head->next = elem_ptr;
ptr->head = elem_ptr;
ptr->head->next = NULL;
}
}
void dodaj(snake* ptr, int kierunek, int tab[][200])
{
float x = ptr->head->x;
float y = ptr->head->y;
switch (kierunek)
{
case GORA:
y -= SKOK;
break;
case DOL:
y += SKOK;
break;
case LEWO:
x -= SKOK;
break;
case PRAWO:
x += SKOK;
break;
}
int tabx = x / SKOK, taby = y / SKOK;
tab[tabx][taby] = WAZ;
snake_elem* elem_ptr;
elem_ptr = (snake_elem*)malloc(sizeof(snake_elem));
elem_ptr->x = x;
elem_ptr->y = y;
elem_ptr->direction = kierunek;
elem_ptr->next = NULL;
ptr->head->next = elem_ptr;
ptr->head->last = kierunek;
ptr->head = elem_ptr;
}
bool checkapple(snake* ptr, int last, int tab[][200])
{
int nx = (ptr->head->x) / SKOK;
int ny = (ptr->head->y) / SKOK;
switch (last)
{
case GORA:
return tab[nx][ny - 1] == JABLKO;
break;
case DOL:
return tab[nx][ny + 1] == JABLKO;
break;
case PRAWO:
return tab[nx + 1][ny] == JABLKO;
break;
case LEWO:
return tab[nx - 1][ny] == JABLKO;
break;
}
return false;
}
bool checkcollision(snake *ptr, int last, int tab[][200])
{
int nx = (ptr->head->x) / SKOK;
int ny = (ptr->head->y) / SKOK;
int lastnx = (ptr->last->x) / SKOK;
int lastny = (ptr->last->y) / SKOK;
switch (last) // NASTEPNA POZYCJA JEST ŚCIANĄ LUB (JEST WĘŻEM, TAKIM ŻE (NIE JEST TO OGON))
{
case GORA:
return (tab[nx][ny - 1] == SCIANA || (tab[nx][ny-1] == WAZ && ( nx!=lastnx || (ny - 1)!=lastny ) ) );
break;
case DOL:
return (tab[nx][ny + 1] == SCIANA || (tab[nx][ny + 1] == WAZ && ( nx != lastnx || (ny + 1) != lastny ) ) );
break;
case PRAWO:
return (tab[nx + 1][ny] == SCIANA || (tab[nx + 1][ny] == WAZ && ( (nx + 1) != lastnx || ny != lastny ) ) );
break;
case LEWO:
return (tab[nx - 1][ny] == SCIANA || (tab[nx - 1][ny] == WAZ && ( (nx - 1) != lastnx || ny != lastny ) ) );
break;
}
return false;
}