breakout/GamePlay.js
2021-11-14 14:56:31 +01:00

43 lines
1.1 KiB
JavaScript

class GamePlay extends Board {
constructor(ctx, key) {
super(ctx, key);
this.controls = {
'KeyX': ()=>{
this.balls.push(new Ball(this.ctx, this.bar));
this.balls[this.balls.length - 1].start();
},
'Space': ()=>{
this.balls[0].moving = true;
}
}
this.score = new Score(ctx);
this.lives = new Lives(ctx);
this.bar = new Bar(ctx, key);
this.newGame();
}
newGame() {
this.lives.reset();
this.score.reset();
this.bar.reset();
this.balls = [];
this.balls.push(new Ball(this.ctx, this.bar));
}
update() {
if(this.lives.get()==0) {
gameOver.update();
} else {
this.balls = this.balls.filter(ball => ball.update());
if (this.balls.length==0) {
if ( !this.lives.lost() ) this.balls.push(new Ball(this.ctx, this.bar));
}
this.bar.update();
}
this.score.update();
this.lives.update();
}
}