-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
60 lines (41 loc) · 1.11 KB
/
sketch.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
var collectibles = [];
function setup() {
createCanvas(windowWidth, windowHeight);
for(var i = 0; i < 1000; i++) {
collectibles.push(new Collectible());
}
}
function draw() {
background(255);
// run thru every collectible in reverse
for(var i = collectibles.length-1; i>=0; i--) {
collectibles[i].update();
var theDistance = dist(mouseX, mouseY, collectibles[i].x, collectibles[i].y);
if(theDistance < collectibles[i].diameter / 2){
//the mouse is inside the radius of this object
//color changes
collectibles[i].col = color(255,0,0);
//remove it from array
collectibles.splice(i,1);
}
}
}
// create a collectibles class
function Collectible() {
// spawn at random location
this.x = random(width);
this.y = random(height);
this.diameter = random(20, 50);
// random blueish color
this.col = color(50, 100, random(100,255));
// internal function for object
this.update = function() {
//move section
this.x += random(-5,5);
this.y += random(-5,5);
// draw section
noStroke();
fill(this.col);
ellipse(this.x, this.y, this.diameter, this.diameter);
}
}