31 lines, 4 of them comments ยท 247 tokens, written in 2 seconds for $0.018


 1  // Ball properties
 2  let ball = {
 3    x: 300,
 4    y: 200,
 5    size: 50,
 6    xSpeed: 4,
 7    ySpeed: 3
 8  };
 9  
10  function setup() {
11    createCanvas(600, 400);
12  }
13  
14  function draw() {
15    background(220);
16  
17    // Draw the ball
18    ellipse(ball.x, ball.y, ball.size);
19  
20    // Update the ball's position
21    ball.x += ball.xSpeed;
22    ball.y += ball.ySpeed;
23  
24    // Check for collisions with canvas edges
25    if (ball.x - ball.size / 2 <= 0 || ball.x + ball.size / 2 >= width) {
26      ball.xSpeed *= -1;
27    }
28    if (ball.y - ball.size / 2 <= 0 || ball.y + ball.size / 2 >= height) {
29      ball.ySpeed *= -1;
30    }
31  }