xxxxxxxxxx
54
// Motion with a conditional and logical operator 2
// In this sketch we make the ball bounce along both axis
let brittany;
let adam;
let x;
let y;
let speedx;
let speedy;
let rad = 50; // radius of the ellipse
function setup() {
createCanvas(1000, 1000);
strokeWeight(2);
stroke(255);
noFill();
brittany = new Baller(100, 200, rad, 2, 2);
adam = new Baller(600, 400, 10, 5, 5);
}
function draw() {
background(150);
brittany.letsBall();
adam.letsBall();
}
class Baller {
constructor(tempX, tempY, tempDiam, tempSpeedX, tempSpeedY) {
this.x = tempX;
this.y = tempY;
this.diam = tempDiam;
this.speedX = tempSpeedX;
this.speedY = tempSpeedY;
}
letsBall() {
this.x += this.speedX;
this.y += this.speedY;
ellipse(this.x, this.y, this.diam, this.diam);
if (this.x < 0 + this.diam || this.x > width - this.diam) {
print("turningX");
this.speedX = this.speedX * -1;
}
if (this.y < 0 + this.diam || this.y > height - this.diam) {
print("turningY");
this.speedY = this.speedY * -1;
}
}
}