Bounce is now better

This commit is contained in:
2021-11-13 00:34:29 +01:00
parent 135f7d8a41
commit ae92c56957
6 changed files with 174 additions and 49 deletions

View File

@ -17,29 +17,38 @@ function init() {
}
// -------------------------------------------------------------------
let gstate = 1;
let gstate = 0;
let key = new Key(controls);
let intro = new Intro(ctx);
let gameOver = new GameOver(ctx);
let score = new Score(ctx);
let lives = new Lives(ctx);
let bar = new Bar(ctx, key);
let balls = [];
newGame();
function controls(e) {
switch (gstate) {
case 0: // Waiting to start
if (e.code=='Space') newGame();
if (e.code == 'Space') newGame();
break;
case 1: // Playing...
if (e.code == 'Space' && balls.length>0) balls[balls.length - 1].start();
else
if (e.code == 'KeyX') { balls.push(new Ball(ctx, bar)); balls[balls.length - 1].start(); }
break;
case 2: // Game Over
if (e.code=='Space') newGame();
if (e.code == 'Space') newGame();
break;
}
}
function newGame() {
gstate = 1;
lives.reset();
score.reset();
bar.reset();
balls.push(new Ball(ctx, bar));
}
function refresh() {
@ -48,51 +57,23 @@ function init() {
intro.update();
break;
case 1: // Playing...
bar.update();
case 2:
if(gstate==2) {
gameOver.update();
} else {
bar.update();
balls = balls.filter(ball => ball.update());
if (balls.length==0) {
if ( lives.lost() ) gstate=2;
else balls.push(new Ball(ctx, bar));
}
}
score.update();
break;
case 2: // Game Over
lives.update();
break;
}
}
run();
}
class ball {
constructor(x, y, speed, angle) {
this.x = x;
this.y = y;
this.speed = speed;
this.angle = angle;
this.color = 'black';
this.size = 20;
}
update() {
this.move();
this.draw();
}
draw() {
this.ctx.fillStyle = this.color;
this.ctx.fillRect(this.x, this.y, this.size, this.size);
}
move() {
this.x += this.speed * Math.cos(this.angle);
this.y += this.speed * Math.sin(this.angle);
if (this.x < 0 || this.x > this.ctx.canvas.width || this.y < 0 || this.y > this.ctx.canvas.height)
this.bounce();
}
bounce() {
this.angle += 180;
}
}