xxxxxxxxxx
96
// Declare a variable for the horizontal position of the ball
//let x1, x2, x3;
//let y1, y2, y3;
//let xs = [x1, x2, x3];
//let ys = [y1, y2, y3];
let xs = [100, 120, 150];
let ys = [200, 220, 250];
// Declare a variable for the horizontal speed of the ball
//let x1speed, x2speed, x3speed;
//let y1speed, y2speed, y3speed;
//let xspeeds = [x1speed, x2speed, x3speed];
//let yspeeds = [y1speed, y2speed, y3speed];
let xspeeds = [1, 1.1, 1.2];
let yspeeds = [1, 1.1, 1.2];
function setup() {
createCanvas(300, 400);
// Initialize the position in the center
//x1 = x2 = x3 = width / 2;
//y1 = y2 = y3 = height/2
//x1speed = 1;
//x2speed = 1.1;
//x3speed = 1.2;
//y1speed = 1;
//y2speed = 1.1;
//y3speed = 1.2;
}
function draw() {
background(220);
// Draw the ball
for(let i = 0; i < xs.length; i++) {
for(let j = 0; j < ys.length; j++) {
ellipse(xs[i], ys[j], 50, 50);
console.log(i, j)
}
}
for(let i = 0; i < xspeeds.length; i++) {
for(let j = 0; j < yspeeds.length; j++) {
xs[i] += xspeeds[i];
ys[j] += yspeeds[j];
}
}
/*
// Move the ball ball, either right or left
x1 += x1speed;
y1 += y1speed;
x2 += x2speed;
y2 += y2speed;
x3 += x3speed;
y3 += y3speed;
// Simplified Version:
// When the ball is at either left or right border
if (x1 <= 0 || x1 >= width) {
// Flip the sign of the direction
x1speed *= -1;
}
if (y1 <= 0 || y1 >= height) {
// Flip the sign of the direction
y1speed *= -1;
}
// When the ball is at either left or right border
if (x2 <= 0 || x2 >= width) {
// Flip the sign of the direction
x2speed *= -1;
}
if (y2 <= 0 || y2 >= height) {
// Flip the sign of the direction
y2speed *= -1;
}
// When the ball is at either left or right border
if (x3 <= 0 || x3 >= width) {
// Flip the sign of the direction
x3speed *= -1;
}
if (y3 <= 0 || y3 >= height) {
// Flip the sign of the direction
y3speed *= -1;
}
// Draw the ball
ellipse(x1, y1, 50, 50);
ellipse(x2, y2, 50, 50);
ellipse(x3, y3, 50, 50);
*/
}