-
Notifications
You must be signed in to change notification settings - Fork 0
/
food.js
41 lines (35 loc) · 930 Bytes
/
food.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
// Daniel Shiffman
// https://www.kadenze.com/courses/the-nature-of-code
// http://natureofcode.com/
// Session 5: Evolutionary Computing
// Evolution EcoSystem
// A collection of food in the world
function Food(num) {
// Start with some food
this.food = [];
for (var i = 0; i < num; i++) {
this.food.push(createVector(random(width),random(height)));
}
// Add some food at a location
this.add = function(l) {
this.food.push(l.copy());
}
// Display the food
this.run = function() {
for (var i = 0; i < this.food.length; i++) {
var f = this.food[i];
rectMode(CENTER);
stroke(0);
fill(127);
rect(f.x,f.y,8,8);
}
// There's a small chance food will appear randomly
if (random(1) < 0.001) {
this.food.push(createVector(random(width),random(height)));
}
}
// Return the list of food
this.getFood = function() {
return this.food;
}
}