-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
48 lines (41 loc) · 1012 Bytes
/
snake.js
File metadata and controls
48 lines (41 loc) · 1012 Bytes
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
function Snake(){
this.x = 0;
this.y = 0;
this.xspeed = 100;
this.yspeed = 100;
this.total = 0;
this.tail = [];
this.eat = function(food){
var d = dist(this.x, this.y, 20*food.x, 20*food.y);
if(d < 1){
this.total ++;
return true;
}
else
return false;
}
this.dir = function(x, y){
this.xspeed = x;
this.yspeed = y;
}
this.update = function(){
if(this.total === this.tail.length){
for(var i = 0; i < this.tail.length-1; i++){
this.tail[i] = this.tail[i+1];
}
}
this.tail[this.total-1] = createVector(this.x, this.y);
this.x = this.x + this.xspeed*scl;
this.y = this.y + this.yspeed*scl;
this.x = constrain(this.x, 0, width-scl);
this.y = constrain(this.y, 0, height-scl);
}
this.show = function(){
fill(255);
for(var i = 0; i < this.tail.length; i++){
rect(this.tail[i].x, this.tail[i].y, scl, scl);
}
fill(255);
rect(this.x, this.y, scl, scl);
}
}