-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnake.js
93 lines (83 loc) · 3.34 KB
/
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
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
// Snake Game
function Snake() {
this.x = width/2; //position along x
this.y = width/2; //positon along y
this.xspeed = 0; //speed in x-axis
this.yspeed = 0; //speed in y-axis
this.total = 0; //total length initally
this.head = [50, 50, 50, 50]
this.tail = []; //array of the tail
//function to check if snake ate the food
this.eat = function(pos) { //if eaten at that position
let d = dist(this.x, this.y, pos.x, pos.y); //then find the distance between them
if (d < 1) { //if the distance is less than 1
this.total++; //increase the size of the snake by 1
return true; //and return true that it has eaten the food
} else {
return false; //else return false
}
};
//direction of the snake
this.dir = function(x, y) {
this.xspeed = x;
this.yspeed = y;
};
this.death = function() {
for (let i = 0; i < this.tail.length; i++) {
let pos = this.tail[i];
let d = dist(this.x, this.y, pos.x, pos.y);
if (d < 1) {
console.log('starting over');
this.total = 0;
this.tail = [];
if (navigator.vibrate) {
window.navigator.vibrate(200);
}
}
}
};
//updating the length and speed of the snake across the axes
this.update = function() {
for (let i = 0; i < this.tail.length - 1; i++) {
this.tail[i] = this.tail[i + 1];
}
if (this.total >= 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,width-scl)
};
//showing the snake on the screen + filling with color etc
this.show = function() {
stroke(0, 0, 0);
strokeWeight(1.5);
fill(255);
for (let i = 0; i < this.tail.length; i++) {
rect(this.tail[i].x, this.tail[i].y, scl, scl, 2, 2, 2, 2);
}
fill(255);
rect(this.x, this.y, scl, scl, this.head[0], this.head[1], this.head[2], this.head[3]);
if(this.xspeed===1){ // RIGHT
ellipse(this.x+(3*(scl/5)), this.y+(scl/3), 2, 2);
line(this.x + scl/2, this.y + (6*(scl/10)), this.x + scl, this.y + (7*(scl/10)));
// triangle(this.x-(this.tail.length*scl), this.y, this.x-(this.tail.length*scl), this.y+scl, this.x-((this.tail.length+1)*scl), this.y+(scl/2));
}
else if(this.xspeed===-1){ // LEFT
ellipse(this.x+(2*(scl/5)), this.y+(scl/3), 2, 2);
line(this.x, this.y + (7*(scl/10)), this.x + scl/2, this.y + (6*(scl/10)));
// triangle(this.x+scl+(this.tail.length*scl), this.y, this.x+scl+(this.tail.length*scl), this.y+scl, this.x+((this.tail.length+2)*scl), this.y+(scl/2));
}
else if(this.yspeed===-1){ // UP
ellipse(this.x+(scl/3), this.y+(2*(scl/5)), 2, 2);
line(this.x + (7*(scl/10)), this.y, this.x + (6*(scl/10)), this.y + scl/2);
// triangle(this.x, this.y+scl+(this.tail.length*scl), this.x+scl, this.y+scl+(this.tail.length*scl), this.x+(scl/2), this.y+((this.tail.length+2)*scl));
}
else if (this.yspeed === 1) { // DOWN
ellipse(this.x+(scl/3), this.y+(3*(scl/5)), 2, 2);
line(this.x + (6*(scl/10)), this.y + scl/2, this.x + (7*(scl/10)), this.y + scl);
// triangle(this.x, this.y - (this.tail.length * scl), this.x+scl, this.y - (this.tail.length * scl), this.x + (scl / 2), this.y - ((this.tail.length + 1) * scl));
}
};
}