50 lines, no comments ยท 1,627 tokens, written in 7 seconds for $0.0024


 1  function setup() {
 2    createCanvas(400, 400);
 3    ball = new Ball();
 4  }
 5  
 6  function draw() {
 7    background(50);
 8    ball.update();
 9    ball.checkEdges();
10    ball.checkMouse();
11    ball.display();
12  }
13  
14  class Ball {
15    constructor() {
16      this.pos = createVector(width/2, height/2);
17      this.vel = p5.Vector.random2D();
18      this.vel.mult(random(2, 5));
19      this.size = 30;
20      this.color = color(0, 0, 255);
21    }
22  
23    update() {
24      this.pos.add(this.vel);
25    }
26  
27    checkEdges() {
28      if (this.pos.x < -this.size/2 || this.pos.x > width + this.size/2) {
29        this.vel.x *= -1;
30      }
31      if (this.pos.y < -this.size/2 || this.pos.y > height + this.size/2) {
32        this.vel.y *= -1;
33      }
34    }
35  
36    checkMouse() {
37      let d = dist(this.pos.x, this.pos.y, mouseX, mouseY);
38      if (d < this.size) {
39        this.vel.setHeading(random(0, TWO_PI));
40        this.color = color(random(255), random(255), random(255));
41      }
42    }
43  
44    display() {
45      fill(this.color);
46      noStroke();
47      ellipse(this.pos.x, this.pos.y, this.size);
48    }
49  }
50