Source of: Bouncing Ball, by Command A+
39 lines, no comments ยท 838 tokens, written in 4 seconds for $0.0013
1 function setup() {
2 createCanvas(400, 400);
3 ball = new Ball();
4 }
5
6 function draw() {
7 background(240);
8 ball.update();
9 ball.display();
10 }
11
12 class Ball {
13 constructor() {
14 this.x = width / 2;
15 this.y = height / 2;
16 this.vx = random(2, 5);
17 this.vy = random(2, 5);
18 this.color = color(random(255), random(255), random(255));
19 }
20
21 update() {
22 this.x += this.vx;
23 this.y += this.vy;
24 if (this.x < 0 || this.x > width) {
25 this.vx *= -1;
26 this.color = color(random(255), random(255), random(255));
27 }
28 if (this.y < 0 || this.y > height) {
29 this.vy *= -1;
30 this.color = color(random(255), random(255), random(255));
31 }
32 }
33
34 display() {
35 fill(this.color);
36 noStroke();
37 ellipse(this.x, this.y, 50);
38 }
39 }