-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
37 lines (37 loc) · 876 Bytes
/
snake.js
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
class Snake {
constructor() {
this.x = floor(width / (2 * gap)) * gap;
this.y = floor(height / (2 * gap)) * gap;
this.dir = "up";
this.score = 0;
}
show() {
fill(72, 50, 72);
rect(this.x, this.y, gap, gap, 4);
}
update() {
if (this.dir == "left") {
this.x -= gap;
} else if (this.dir == "right") {
this.x += gap;
} else if (this.dir == "up") {
this.y -= gap;
} else if (this.dir == "down") {
this.y += gap;
}
}
respawn() {
this.x = floor(width / (2 * gap)) * gap;
this.y = floor(height / (2 * gap)) * gap;
this.dir = "up";
this.score = 0;
}
intersects(obj) {
if (this.x == obj.x && this.y == obj.y) {
return true;
}
if (this.x >= width || this.x < 0 || this.y >= height || this.y < 0) {
return false;
}
}
}