Source of: Bouncing Balls, by Claude Opus 3
45 lines, no comments ยท collected by hand from claude.ai
1 let balls = [];
2
3 function setup() {
4 createCanvas(400, 400);
5 for (let i = 0; i < 20; i++) {
6 balls.push(new Ball());
7 }
8 }
9
10 function draw() {
11 background(0, 20);
12 for (let ball of balls) {
13 ball.move();
14 ball.display();
15 }
16 }
17
18 class Ball {
19 constructor() {
20 this.x = random(width);
21 this.y = random(height);
22 this.xspeed = random(-2, 2);
23 this.yspeed = random(-2, 2);
24 this.r = random(10, 30);
25 this.c = color(random(255), random(255), random(255), 100);
26 }
27
28 move() {
29 this.x += this.xspeed;
30 this.y += this.yspeed;
31
32 if (this.x < 0 || this.x > width) {
33 this.xspeed *= -1;
34 }
35 if (this.y < 0 || this.y > height) {
36 this.yspeed *= -1;
37 }
38 }
39
40 display() {
41 noStroke();
42 fill(this.c);
43 ellipse(this.x, this.y, this.r);
44 }
45 }