-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruitManager.js
43 lines (37 loc) · 1.04 KB
/
fruitManager.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
function FruitManager() {
return {
fruitSize: 25,
myDeliciousFruits: [],
//Updates the fruit manager by checking if the snake's head has eaten any of its delicious fruit
update: function(head) {
var numCollisions = this.checkForCollision(head);
if(this.myDeliciousFruits.length === 0) {
this.addFruit();
}
return numCollisions;
},
draw: function() {
this.myDeliciousFruits.forEach(function(point) {
point.draw();
});
},
checkForCollision: function(head) {
var currFruit,
numCollisions = 0;
for(var i = this.myDeliciousFruits.length - 1; i >= 0; i--) {
currFruit = this.myDeliciousFruits[i];
if(head.collides(currFruit)) {
this.myDeliciousFruits.splice(i, 1);
numCollisions++;
}
}
return numCollisions;
},
addFruit: function() {
var randX = getRandomInt(50, width - 50),
randY = getRandomInt(50, height - 50),
fruitColor = color(0, 100, 100, 1);
this.myDeliciousFruits.push(Point(randX, randY, this.fruitSize, this.fruitSize, fruitColor, true));
}
};
};