66 lines, 8 of them comments ยท 746 tokens, written in 5 seconds for $0.0040


 1  let balls = [];
 2  let numBalls = 8;
 3  
 4  function setup() {
 5    createCanvas(800, 600);
 6    
 7    // Create initial balls
 8    for (let i = 0; i < numBalls; i++) {
 9      balls.push({
10        x: random(50, width - 50),
11        y: random(50, height - 50),
12        vx: random(-4, 4),
13        vy: random(-4, 4),
14        radius: random(15, 35),
15        color: color(random(100, 255), random(100, 255), random(100, 255))
16      });
17    }
18  }
19  
20  function draw() {
21    background(20, 24, 82, 100);
22    
23    // Update and display each ball
24    for (let i = 0; i < balls.length; i++) {
25      let ball = balls[i];
26      
27      // Apply gravity
28      ball.vy += 0.2;
29      
30      // Update position
31      ball.x += ball.vx;
32      ball.y += ball.vy;
33      
34      // Bounce off walls
35      if (ball.x - ball.radius < 0 || ball.x + ball.radius > width) {
36        ball.vx *= -0.95;
37        ball.x = constrain(ball.x, ball.radius, width - ball.radius);
38      }
39      
40      if (ball.y - ball.radius < 0 || ball.y + ball.radius > height) {
41        ball.vy *= -0.95;
42        ball.y = constrain(ball.y, ball.radius, height - ball.radius);
43      }
44      
45      // Check collision with other balls
46      for (let j = i + 1; j < balls.length; j++) {
47        let other = balls[j];
48        let distance = dist(ball.x, ball.y, other.x, other.y);
49        let minDist = ball.radius + other.radius;
50        
51        if (distance < minDist) {
52          // Simple collision response
53          let angle = atan2(other.y - ball.y, other.x - ball.x);
54          ball.vx -= cos(angle) * 2;
55          ball.vy -= sin(angle) * 2;
56          other.vx += cos(angle) * 2;
57          other.vy += sin(angle) * 2;
58        }
59      }
60      
61      // Draw ball
62      fill(ball.color);
63      noStroke();
64      circle(ball.x, ball.y, ball.radius * 2);
65    }
66  }