let lives = 3;
let showLifeLostText = false;Extra lives / notebook
Extra lives
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function drawStatus() {
ctx.font = "18px Arial";
ctx.fillStyle = "#0095dd";
ctx.textBaseline = "top";
ctx.textAlign = "left";
ctx.fillText(`Points: ${score}`, 5, 5);
ctx.textAlign = "right";
ctx.fillText(`Lives: ${lives}`, canvas.width - 5, 5);
if (showLifeLostText) {
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(
"Life lost, click to continue",
canvas.width / 2,
canvas.height / 2,
);
}
}if (ballIsOutOfBounds) {
// Game over logic
location.reload();
return;
}if (ballIsOutOfBounds) {
ballLeaveScreen();
return;
}function ballLeaveScreen() {
lives--;
if (lives === 0) {
// Game over logic
location.reload();
return;
}
paddle.pos.x = canvas.width / 2;
ball.pos.x = paddle.pos.x;
ball.pos.y = paddle.hitbox.top - ball.size.h / 2;
ball.vel = { x: 0, y: 0 };
showLifeLostText = true;
canvas.addEventListener(
"pointerdown",
() => {
showLifeLostText = false;
ball.vel = { x: 150, y: -150 };
lastTimestamp = null;
},
{ once: true },
);
}