"use strict"; document.addEventListener('DOMContentLoaded', init); function init() { let ctx, canvas = document.createElement("canvas"); canvas.width = window.innerWidth canvas.height = window.innerHeight ctx = canvas.getContext('2d'); document.body.insertBefore(canvas, document.body.childNodes[0]); function run() { ctx.clearRect(0, 0, canvas.width, canvas.height); refresh(); requestAnimationFrame(run); } // ------------------------------------------------------------------- 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(); 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(); break; } } function newGame() { gstate = 1; lives.reset(); score.reset(); bar.reset(); balls.push(new Ball(ctx, bar)); } function refresh() { switch (gstate) { case 0: // Waiting to start intro.update(); break; case 1: // Playing... 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(); lives.update(); break; } } run(); }