-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
50 lines (45 loc) · 937 Bytes
/
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
#include "snake.h"
Snake createSnake(const int x, const int y, const int length, const int size){
int i = 0;
Snake s = {x, y, size};
s.tail = malloc(1000*sizeof(Tail));
for(; i < length; i++){
newTail(&s);
}
return s;
}
void destroySnake(Snake s){
free(s.tail);
}
void newTail(Snake *s){
int x = (s->length == 0) ? s->x : s->tail[s->length-1].x;
int y = (s->length == 0) ? s->y : s->tail[s->length-1].y;
Tail t = {x, y};
s->tail[s->length++] = t;
}
void updateSnake(Snake *s){
int i = s->length-1;
for(; i > 0; i--){
s->tail[i].x = s->tail[i-1].x;
s->tail[i].y = s->tail[i-1].y;
}
switch(s->dir){
case 0:
s->tail[0].x++;
break;
case 1:
s->tail[0].y--;
break;
case 2:
s->tail[0].x--;
break;
case 3:
s->tail[0].y++;
break;
}
}
void drawSnake(Window *w, const Snake s){
int i = 0;
for(; i < s.length; i++)
drawRect(w, s.tail[i].x*s.size, s.tail[i].y*s.size, s.size, s.size);
}