-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.js
More file actions
61 lines (53 loc) · 1.4 KB
/
Snake.js
File metadata and controls
61 lines (53 loc) · 1.4 KB
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
class Snake {
constructor() {
this.body = [];
this.body[0] = createVector(floor(wid/2), floor(hig/2));
this.xdir = 0;
this.ydir = 0;
this.len = 0;
}
setDir(x, y) {
this.xdir = x;
this.ydir = y;
}
update() {
var head = this.body[this.body.length-1].copy();
this.body.shift();
head.x += this.xdir;
head.y += this.ydir;
this.body.push(head);
}
grow() {
var segment = this.body[this.body.length-1].copy();
this.len++;
this.body.push(segment);
}
gameOver() {
var x = this.body[this.body.length-1].x;
var y = this.body[this.body.length-1].y;
if(x > wid-1 || x < 0 || y > hig-1 || y < 0) {
return true;
}
for(var i = 0; i < this.body.length-1; i++) {
var part = this.body[i];
if(part.x == x && part.y == y) {
return true;
}
}
}
eat(item) {
var x = this.body[this.body.length-1].x;
var y = this.body[this.body.length-1].y;
if(x == item.x && y == item.y) {
this.grow();
return true;
}
}
display() {
for(var i = 0; i < this.body.length; i++) {
fill(250,250,250);
noStroke();
rect(this.body[i].x, this.body[i].y, 1, 1)
}
}
}