xxxxxxxxxx
150
/*
Create a ball object that bounces off of the side and top walls.
Moving past the bottom wall will cause loss of turn
The ball can also collide with either a brick or the paddle, in which case the ball will move in the opposite y direction.
The ball is represented by an ellipse. It starts in the center of the screen with a given yvelocity and an random xvelocity
*/
class Ball {
constructor() {
this.x = width / 2;
this.y = height / 2 + 30;
this.r = 30;
this.yVel = 3;
this.xVel = random(1, 3);
let flipDirX = random();
if (flipDirX < 0.5) {
this.xVel = -this.xVel;
}
this.xSpeed = 1;
this.ySpeed = 1;
}
display() {
fill("black");
ellipse(this.x, this.y, this.r);
}
move() {
this.x += this.xVel * this.xSpeed;
this.y += this.yVel * this.ySpeed;
}
checkForWalls() {
if (this.x + this.r > width) {
this.xSpeed *= -1;
} else if (this.y < this.r) {
this.ySpeed *= -1;
} else if (this.x < this.r) {
this.xSpeed *= -1;
}
}
intersectBall(obj) {
// temporary variables to set edges for testing
let testX = this.x;
let testY = this.y;
// which edge is closest?
if (this.x < obj.x) testX = obj; // test left edge
else if (this.x > obj.x + obj.w) testX = obj.x + obj.w; // right edge
if (this.y < obj.y) testY = obj.y; // top edge
else if (this.y > obj.y + obj.h) testY = obj.y + obj.h; // bottom edge
// get distance from closest edges
let distX = this.x - testX;
let distY = this.y - testY;
let distance = sqrt((distX * distX) + (distY * distY));
// if the distance is less than the radius, collision!
if (distance <= this.r) {
return true;
}
return false;
}
// return ((this.x > paddle.x && this.x < paddle.x + paddle.w) && (this.y > paddle.y && this.y < paddle.y + paddle.h) )
// return true for left edge,
// false for right edge
isNearLeftEdgeOfPaddle(paddle) {
// which edge is closest?
if (this.x < paddle.x) {
return true;
} else if (this.x > paddle.x + paddle.w) {
return false;
}
}
intersectPaddle(obj) {
// temporary variables to set edges for testing
let testX = this.x;
let testY = this.y;
// which edge is closest?
if (this.x < obj.x) {
testX = obj; // test left edge
} else if (this.x > obj.x + obj.w) {
testX = obj.x + obj.w; // right edge
}
if (this.y < obj.y) testY = obj.y; // top edge
else if (this.y > obj.y + obj.h) testY = obj.y + obj.h; // bottom edge
// get distance from closest edges
let distX = this.x - testX;
let distY = this.y - testY;
let distance = sqrt((distX * distX) + (distY * distY));
// if the distance is less than the radius, collision!
if (distance <= this.r) {
return true;
}
return false;
}
// return ((this.x > paddle.x && this.x < paddle.x + paddle.w) && (this.y > paddle.y && this.y < paddle.y + paddle.h) )
bounceOffObj() {
this.ySpeed *= -1;
}
moveOffScreen() {
this.x = width + 50;
this.y = 200;
}
isOffScreen() {
return (this.y > height + this.r)
}
reset() {
this.x = width / 2;
this.y = height / 2 + 30
this.yVel = 0;
this.xVel = 0;
}
startMoving() {
this.yVel = 3;
this.xVel = random(1, 3);
let flipDirX = random();
if (flipDirX < 0.5) {
this.xVel = -this.xVel;
}
}
}