Source of: Colorful Bouncing Balls, by GPT-4.1
58 lines, 1 of them comments ยท 488 tokens, written in 3 seconds for $0.0041
1 let balls = [];
2 let numBalls = 15;
3
4 function setup() {
5 createCanvas(600, 400);
6 for (let i = 0; i < numBalls; i++) {
7 let r = random(18, 35);
8 let b = new Ball(
9 random(r, width - r),
10 random(r, height - r),
11 r
12 );
13 balls.push(b);
14 }
15 noStroke();
16 }
17
18 function draw() {
19 background(30, 30, 50);
20 for (let b of balls) {
21 b.move();
22 b.bounce();
23 b.display();
24 }
25 }
26
27 class Ball {
28 constructor(x, y, r) {
29 this.x = x;
30 this.y = y;
31 this.r = r;
32 this.vx = random(-3, 3);
33 this.vy = random(-3, 3);
34 this.color = color(random(255), random(255), random(255));
35 }
36 move() {
37 this.x += this.vx;
38 this.y += this.vy;
39 // Change color slightly as it moves
40 this.color = color(
41 (red(this.color) + random(-2, 2)) % 255,
42 (green(this.color) + random(-2, 2)) % 255,
43 (blue(this.color) + random(-2, 2)) % 255
44 );
45 }
46 bounce() {
47 if (this.x < this.r || this.x > width - this.r) {
48 this.vx *= -1;
49 }
50 if (this.y < this.r || this.y > height - this.r) {
51 this.vy *= -1;
52 }
53 }
54 display() {
55 fill(this.color);
56 ellipse(this.x, this.y, this.r * 2);
57 }
58 }