-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (65 loc) · 1.71 KB
/
Copy pathscript.js
File metadata and controls
73 lines (65 loc) · 1.71 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
62
63
64
65
66
67
68
69
70
71
72
73
function Snake (){
this.body = [];
this.length = 0;
this.x = 0;
this.y = 0;
this.xSpeed = 1;
this.ySpeed = 0;
this.move = function() {
this.x = this.x + this.xSpeed * s;
this.y = this.y + this.ySpeed * s;
// this.x = constrain(this.x, 0, width-s);
// this.y = constrain(this.y, 0, height-s);
if (this.x <= -s)
this.x = width-s;
if (this.x >= width)
this.x = 0;
if (this.y <= -s)
this.y = height-s;
if (this.y >=height)
this.y = 0;
}
this.show = function() {
fill(255);
for (var i = 0; i < this.body.length; i++) {
rect(this.body[i].x, this.body[i].y, s, s);
}
rect(this.x, this.y, s, s);
}
this.eat = function () {
var distance = dist(this.x, this.y, food.x, food.y);
if (distance<1){
win.play();
this.length +=1;
score.elt.innerHTML = parseInt(score.elt.innerHTML) +1;
return true;
}else{
return false;
}
}
this.grow = function () {
for (var i = 0; i < this.body.length-1; i++) {
this.body[i] = this.body[i+1];
}
if (this.length >= 1) {
this.body[this.length-1] = createVector(this.x, this.y);
}
}
this.check_dead = function () {
for (var i = 0; i < this.body.length-1; i++) {
var distance = dist(this.x, this.y, this.body[i].x, this.body[i].y);
if(distance < 1){
lose.play();
running = !running;
this.length = 0;
this.body = [];
if (parseInt(score.elt.innerHTML) >= localStorage.best_score){
localStorage.best_score = parseInt(score.elt.innerHTML);
console.log(localStorage.best_score);
}
score.elt.innerHTML = 0;
speed = 8;
}
}
}
}