diff --git a/Ball.js b/Ball.js index 5dae42d..6df6b35 100644 --- a/Ball.js +++ b/Ball.js @@ -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)= 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)=y0 && this.y<=y1 && (this.y-this.size)<=x1 && (this.y-this.size)>x0) { - this.bounceL(r); - return true; - } return false; } diff --git a/GamePlay.js b/GamePlay.js index bd1d45f..7ba18b2 100644 --- a/GamePlay.js +++ b/GamePlay.js @@ -3,6 +3,9 @@ class GamePlay extends Board { super(ctx, key); this.controls = { + 'KeyS': ()=>{ + this.nextLevel(++this.level); + }, 'KeyX': ()=>{ let b = new Ball(); b.update(this.ctx, this.bar.x + this.bar.w/2, this.bar.y); diff --git a/Levels.js b/Levels.js index e6b276f..2a11894 100644 --- a/Levels.js +++ b/Levels.js @@ -10,6 +10,15 @@ class Levels { this.row(4, [1, 1, 1, 1, 1, 1, 1, 1]) ); 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: map = [].concat( this.row(0, [1, 1, 1, 1, 1, 1, 1, 1]),