xxxxxxxxxx
47
class movingLine {
constructor(xPos, yPos, xSpeed, ySpeed) {
this.xPos = xPos;
this.yPos = yPos;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
move() {
// move the ball
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
}
checkForCollisions() {
// check first for left and right wall
if (this.xPos <= 15 || this.xPos >= width - 15) {
this.xSpeed = this.xSpeed *-1;
}
// do the same for the ceiling and the floor
if (this.yPos <= 15 || this.yPos >= height - 15) {
this.ySpeed = -this.ySpeed;
}
}
draw() {
fill(random(255), random(255), random(255));
square(this.xPos, this.yPos, 30);
}
}
let lines;
function setup() {
createCanvas(400, 400);
lines = new movingLine(30, 30, 5, 0);
}
function draw() {
background(240);
lines.move();
lines.checkForCollisions();
lines.draw();
lines.changeColor;
}