Checking the levels

This commit is contained in:
2021-11-15 00:10:53 +01:00
parent 7493713529
commit 7cf0bfbdbd
3 changed files with 43 additions and 28 deletions

59
Ball.js
View File

@ -6,7 +6,7 @@ class Ball {
this.moving = false;
this.speed = 7;
// this.angle = 90;
this.setAngle(180 +60, 360 - 60);
this.setAngle(180 + 60, 360 - 60);
this.color = 'red';
this.limits = null;
@ -30,7 +30,7 @@ class Ball {
b: ctx.canvas.height
};
if (this.move(x,y)) {
if (this.move(x, y)) {
this.draw(ctx);
return true;
}
@ -47,7 +47,7 @@ class Ball {
ctx.stroke();
}
move(x,y) {
move(x, y) {
if (this.moving) {
this.x += this.speed * Math.cos(this.angle);
this.y += this.speed * Math.sin(this.angle);
@ -67,25 +67,25 @@ class Ball {
}
bounceL(r) {
if(this.angle<=this.angleBL)
if (this.angle <= this.angleBL)
this.setAngle(0 + r, 90 - r);
else
this.setAngle(270 + r, 360 - r);
}
bounceR(r) {
if(this.angle<=this.angleBR)
if (this.angle <= this.angleBR)
this.setAngle(90 + r, 180 - r);
else
this.setAngle(180 + r, 270 - r);
}
bounceT(r) {
if(this.angle<=this.angleTL)
if (this.angle <= this.angleTL)
this.setAngle(90 + r, 180 - r);
else
this.setAngle(0 + r, 90 - r);
}
bounceB(r) {
if(this.angle<=this.angleBR)
if (this.angle <= this.angleBR)
this.setAngle(270 + r, 360 - r);
else
this.setAngle(180 + r, 270 - r);
@ -93,19 +93,19 @@ class Ball {
collideWalls(x0, y0, x1, y1) {
let r = 20;
if ( this.x <= x0 ) {
if (this.x <= x0) {
this.bounceL(r);
return true;
}
if (this.x >= x1 ) {
if (this.x >= x1) {
this.bounceR(r);
return true;
}
if (this.y <= y0 ) {
if (this.y <= y0) {
this.bounceT(r);
return true;
}
if (this.y >= y1 ) {
if (this.y >= y1) {
this.bounceB(r);
return true;
}
@ -124,28 +124,31 @@ class Ball {
T
*/
collide(x0,y0,x1,y1) { // 0 = hit Left/Right, 1 = hit Up/Down
collide(x0, y0, x1, y1) { // 0 = hit Left/Right, 1 = hit Up/Down
let r = 20;
if (this.x>=x0 && this.x<=x1 && (this.y+this.size)>=y0 && (this.y+this.size)<y1) {
this.bounceB(r);
return true;
if (this.x >= x0 && this.x <= x1) {
if ((this.y + this.size) >= y0 && (this.y + this.size) < y1) {
this.bounceB(r);
return true;
}
if ((this.y - this.size) <= y1 && (this.y - this.size) > y0) {
this.bounceT(r);
return true;
}
}
if (this.x>=x0 && this.x<=x1 && (this.y-this.size)<=y1 && (this.y-this.size)>y0) {
this.bounceT(r);
return true;
if (this.y >= y0 && this.y <= y1) {
if ((this.x + this.size) >= x0 && (this.x + this.size) < x1) {
this.bounceR(r);
return true;
}
if ((this.x - this.size) <= x1 && (this.x - this.size) > x0) {
this.bounceL(r);
return true;
}
}
if (this.y>=y0 && this.y<=y1 && (this.y+this.size)>=x0 && (this.y-this.size)<x1) {
this.bounceR(r);
return true;
}
if (this.y>=y0 && this.y<=y1 && (this.y-this.size)<=x1 && (this.y-this.size)>x0) {
this.bounceL(r);
return true;
}
return false;
}