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

View File

@ -3,6 +3,9 @@ class GamePlay extends Board {
super(ctx, key); super(ctx, key);
this.controls = { this.controls = {
'KeyS': ()=>{
this.nextLevel(++this.level);
},
'KeyX': ()=>{ 'KeyX': ()=>{
let b = new Ball(); let b = new Ball();
b.update(this.ctx, this.bar.x + this.bar.w/2, this.bar.y); b.update(this.ctx, this.bar.x + this.bar.w/2, this.bar.y);

View File

@ -10,6 +10,15 @@ class Levels {
this.row(4, [1, 1, 1, 1, 1, 1, 1, 1]) this.row(4, [1, 1, 1, 1, 1, 1, 1, 1])
); );
break; break;
case 2:
map = [].concat(
this.row(0, [1, 1, 1, 0, 1, 1, 0, 0]),
this.row(1, [0, 0, 1, 0, 1, 0, 1, 0]),
this.row(2, [0, 0, 1, 0, 1, 0, 1, 0]),
this.row(3, [1, 0, 1, 0, 1, 0, 1, 0]),
this.row(4, [0, 1, 1, 0, 1, 1, 0, 0])
);
break;
default: default:
map = [].concat( map = [].concat(
this.row(0, [1, 1, 1, 1, 1, 1, 1, 1]), this.row(0, [1, 1, 1, 1, 1, 1, 1, 1]),