Here’s the full merged HTML script, integrating the original game mechanics with the extended functionalities that include sound effects, a pause feature, power-ups, and an overall enhanced gameplay experience.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Brick Breaker Game</title>
<style>
body {
background: #faf8ef;
margin: 0;
padding: 0;
}
canvas {
border: 2px solid black;
margin: 0 auto;
display: block;
}
button {
border-radius: 5px;
font-size: 1.5em;
display: block;
margin: 10 auto;
}
#pauseMenu {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.5);
color: white;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<canvas id="canvas" width="720" height="800"></canvas>
<button onclick="gameManager.downBalls()">공 내리기</button>
<div id="pauseMenu">
<h2>Game Paused</h2>
<button onclick="gameManager.resumeGame()">Resume</button>
<button onclick="location.reload()">Restart</button>
</div>
<script>
const sounds = {
hit: new Audio('hit.wav'), // Replace with actual path
score: new Audio('score.wav'), // Replace with actual path
gameOver: new Audio('gameOver.wav'), // Replace with actual path
backgroundMusic: new Audio('backgroundMusic.mp3') // Replace with actual path
};
function randomInt(...args) {
if (args.length === 1) {
const [n] = args;
return Math.ceil(Math.random() * n);
}
if (args.length === 2) {
const [start, end] = args;
if (start > end) throw Error("Starting value is greater than ending value");
return Math.ceil(Math.random() * (end - start)) + start;
}
}
function random(...args) {
if (args.length === 1) {
const [n] = args;
return Math.random() * n;
}
if (args.length === 2) {
const [start, end] = args;
if (start > end) throw Error("Starting value is greater than ending value");
return Math.random() * (end - start) + start;
}
}
const range = function (n, m) {
if (arguments.length === 1) return Array.from({ length: n }).map((_, i) => i);
if (arguments.length === 2) {
if (n === m) return [n];
else if (n < m) {
return Array.from({ length: m - n + 1 }).map((_, i) => i + n);
} else {
return Array.from({ length: n - m + 1 }).map(
(_, i) => m - i + (n - m)
);
}
}
};
function normalize(n) {
return n < 0 ? -1 : n > 0 ? 1 : 0;
}
function clamp(v, min, max) {
return Math.max(min, Math.min(max, v));
}
function between(v, min, max) {
return min <= v && v <= max;
}
const delay = (n) => new Promise((r) => setTimeout(r, n));
class Vector {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
add(v) {
this.x += v.x;
this.y += v.y;
}
sub(v) {
this.x -= v.x;
this.y -= v.y;
}
mult(n) {
this.x *= n;
this.y *= n;
}
mag() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
get() {
return new Vector(this.x, this.y);
}
normalize() {
this.x = normalize(this.x);
this.y = normalize(this.y);
}
copy() {
return new Vector(this.x, this.y);
}
}
Vector.mult = (v, n) => new Vector(v.x * n, v.y * n);
Vector.div = (v, n) => new Vector(v.x / n, v.y / n);
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const width = 720;
const height = 800;
const uiOffsetY = 80;
canvas.width = width;
canvas.height = height;
function line(x1, y1, x2, y2, color = "#000") {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
function dashLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.setLineDash([5, 15]);
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
ctx.setLineDash([]);
}
function rect(x, y, w, h) {
ctx.beginPath();
ctx.rect(x, y, w, h);
ctx.strokeStyle = "#000";
ctx.stroke();
ctx.closePath();
}
function fillRect(x, y, w, h, color = "#171717") {
ctx.save();
ctx.beginPath();
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
ctx.stroke();
ctx.closePath();
ctx.restore();
}
function circle(x, y, r, color = "#000") {
ctx.save();
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.lineWidth = 3;
ctx.fillStyle = color;
ctx.strokeStyle = color;
ctx.fill();
ctx.stroke();
ctx.closePath();
ctx.restore();
}
function clear() {
ctx.clearRect(0, 0, width, height);
}
function shuffle(arr) {
return arr.sort(() => Math.random() - 0.5);
}
const POWER = 0.3;
const Mouse = { position: new Vector(0, 0) };
class BallLine {
constructor() {}
display(state) {
if (!state.ballMoving && !state.brickMoving && state.ballPos) {
dashLine(
state.ballPos.x,
state.ballPos.y,
Mouse.position.x,
Math.min(Mouse.position.y, 720)
);
}
}
}
class Ball {
constructor(mass, x, y) {
this.position = new Vector(x, y);
this.velocity = new Vector(0, 0);
this.acceleration = new Vector(0, 0);
this.mass = mass;
this.r = this.mass * 8;
this.showDirection = true;
}
setOnStop(f) {
this.onStop = f;
}
applyForce(force) {
const f = Vector.div(force, this.mass);
this.acceleration.add(f);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
down() {
this.downing = true;
this.velocity = new Vector(0, 30);
}
shoot(mousePos) {
this.showDirection = false;
const force = this.calcBallVelocity(this.angle(mousePos));
this.applyForce(force);
}
calcBallVelocity(angle) {
return new Vector(
100 * Math.cos(angle) * POWER,
100 * Math.sin(angle) * POWER
);
}
angle(mousePos) {
const opposite = mousePos.y - this.position.y;
const adjacent = mousePos.x - this.position.x;
return Math.atan2(opposite, adjacent);
}
display() {
circle(this.position.x, this.position.y, this.r, "#5ba7f4");
}
collideWith(brick) {
if (this.downing) return false;
const { x, y } = this.position;
const r = this.r;
const closestX = clamp(x, brick.x, brick.x + brick.w);
const closestY = clamp(y, brick.y, brick.y + brick.h);
const distanceX = x - closestX;
const distanceY = y - closestY;
const distanceSquared = distanceX * distanceX + distanceY * distanceY;
const collided = distanceSquared < r * r;
if (!collided) return false;
// Reflect ball velocity based on collision
if (closestY === brick.y && between(closestX, brick.x - r + 3, brick.x + brick.w + r - 3)) {
this.velocity.y *= -1;
this.position.y = closestY - r;
} else if (closestY === brick.y + brick.h && between(closestX, brick.x - r + 3, brick.x + brick.w + r - 3)) {
this.velocity.y *= -1;
this.position.y = closestY + r;
} else if (closestX === brick.x && between(closestY, brick.y - r + 3, brick.y + brick.h + r - 3)) {
this.velocity.x *= -1;
this.position.x = closestX - r;
} else if (closestX === brick.x + brick.w && between(closestY, brick.y - r + 3, brick.y + brick.h + r - 3)) {
this.velocity.x *= -1;
this.position.x = closestX + r;
}
return true;
}
checkEdges() {
if (this.position.x > width) {
this.position.x = width;
this.velocity.x *= -1;
} else if (this.position.x < 0) {
this.position.x = 0;
this.velocity.x *= -1;
}
if (this.position.y > height - 100) {
this.velocity.y *= -1;
this.position.y = height - 100;
this.stop();
} else if (this.position.y < 100) {
this.velocity.y *= -1;
this.position.y = 100;
}
}
}
class Brick {
constructor(n, x, y) {
this.n = n;
this.start = n;
this.x = x * 120;
this.y = y * 80 + uiOffsetY;
this.w = 110;
this.h = 70;
}
update(state) {
if (this.shouldMoveDown(state)) {
this.y = Math.min(
this.y + 10,
80 * (state.level - this.start + 1) + uiOffsetY
);
}
}
color(level) {
const percentage = ((level - this.n) / level) * 30;
return `hsl(${percentage}, ${100 - percentage}%, 63%)`;
}
display(state) {
fillRect(this.x, this.y, this.w, this.h, this.color(state.level));
ctx.fillStyle = "black";
ctx.font = "20px Arial";
ctx.fillText(this.n, this.x + this.w / 2 - 8, this.y + this.h / 2 + 5);
}
hit() {
this.n--;
if (this.n <= 0) {
sounds.hit.play(); // Play hit sound
return true; // Brick is destroyed
}
return false; // Brick still exists
}
shouldMoveDown(state) {
return this.y < 80 * (state.level - this.start + 1) + uiOffsetY;
}
get broken() {
return this.n <= 0;
}
get hitBottom() {
return this.y >= height - uiOffsetY - 80;
}
}
class BonusBall {
constructor(n, x, y) {
this.start = n;
this.r = 16;
this.x = x * 120 + 56;
this.y = y * 80 + 36 + uiOffsetY;
this.hit = false;
}
update(state) {
if (this.hit) {
this.y = Math.min(this.y + 50, height - 100);
} else {
this.y = Math.min(
this.y + 10,
80 * (state.level - this.start + 1) + uiOffsetY + 40
);
}
}
display() {
circle(this.x, this.y, this.r, "#3dd462");
}
hitWithBall() {
this.hit = true;
}
collideWith(ball) {
const x = ball.position.x - this.x;
const y = ball.position.y - this.y;
const r = this.r;
const collided = 2 * r >= Math.sqrt(x * x + y * y);
if (collided) {
this.hit = true;
}
return collided;
}
}
class PowerUp {
constructor(x, y) {
this.position = new Vector(x, y);
this.radius = 10;
this.type = Math.random() > 0.5 ? "extraBall" : "scoreMultiplier"; // Randomly choose type
}
display() {
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.type === "extraBall" ? "yellow" : "orange";
ctx.fill();
ctx.closePath();
}
collect(balls) {
if (this.type === "extraBall") {
balls.addNewBall();
} else if (this.type === "scoreMultiplier") {
// Implement score multiplier logic here if desired.
}
}
}
class BrickParticle {
constructor(n, x, y) {
const i = n % 5;
const j = ~~(n / 4);
this.w = 22;
this.h = 18;
this.location = new Vector(x + i * this.w, y + j * this.h);
this.acceleration = new Vector(0, 0);
this.velocity = new Vector(
i > 2 ? random(0, 0.5) : i === 2 ? random(-0.5, 0.5) : random(-0.5, 0),
random(1, 3)
);
this.lifespan = 255;
}
update() {
this.velocity.add(this.acceleration);
this.location.add(this.velocity);
this.lifespan -= 4;
}
display() {
fillRect(
this.location.x,
this.location.y,
22,
18,
`hsla(30, 70%, 63%, ${this.lifespan / 255})`
);
}
run() {
this.update();
this.display();
}
get isDead() {
return this.lifespan < 0;
}
}
class BrickParticleSystem {
constructor(x, y) {
this.origin = new Vector(x, y);
this.particles = range(20).map((n) => new BrickParticle(n, x, y));
}
addParticle() {
this.particles.push(new BrickParticle(this.origin));
}
run() {
this.particles.forEach((particle) => particle.run());
}
get isDead() {
return !this.particles.length || this.particles[0].isDead;
}
}
class BrickParticleSystems {
constructor() {
this.particleSystems = [];
}
addParticleSystem(...bricks) {
const newPs = bricks.map(({ x, y }) => new BrickParticleSystem(x, y));
this.particleSystems.push(...newPs);
}
run() {
this.particleSystems = this.particleSystems.filter((ps) => !ps.isDead);
this.particleSystems.forEach((ps) => ps.run());
}
}
class UI {
constructor() {}
display(state) {
line(0, uiOffsetY, width, uiOffsetY);
line(0, height - uiOffsetY, width, height - uiOffsetY);
this.showBallCount(state);
this.showScore(state);
}
showScore(state) {
ctx.fillText(`현재 점수: ${state.level}`, 30, uiOffsetY / 2 + 7);
ctx.fillText(
`최고 점수: ${state.bestLevel}`,
width - 150,
uiOffsetY / 2 + 7
);
}
showBallCount(state) {
if (!state.ballMoving)
ctx.fillText(
`x${state.ballCount}`,
state.ballPos.x - 10,
height - 55
);
}
gameOver() {
fillRect(0, height / 2 - 100, width, 200, "rgba(0, 0, 0, 0.3)");
ctx.font = "40px Arial";
ctx.fillText("패배!", width / 2 - 40, height / 2 + 12);
ctx.font = "20px Arial";
}
}
class Balls {
constructor(state) {
this.balls = range(state.ballCount).map(
(i) => new Ball(2, state.ballPos.x, state.ballPos.y)
);
this.onBallStop = (ball) => {
if (!state.firstBallStop) {
state.ballPos = ball.position.copy();
state.firstBallStop = true;
}
ball.position.x = state.ballPos.x;
};
this.balls.forEach((ball) => ball.setOnStop(this.onBallStop));
}
collideWithBricks(bricks) {
bricks.bricks.forEach((brick) => {
this.balls.forEach((ball) => {
const collided = ball.collideWith(brick);
if (collided) {
brick.hit();
}
});
});
}
collideWithBonusBall(bonusBalls) {
this.balls.forEach((ball) => {
bonusBalls.forEach((bonusBall) => {
const collided = ball.collideWithBonusBall(bonusBall);
if (collided) bonusBall.hitWithBall();
});
});
}
get allStopped() {
return this.balls.every((ball) => ball.velocity.mag() === 0);
}
addBalls(state, n) {
const newBalls = range(n).map(
(i) => new Ball(2, state.ballPos.x, state.ballPos.y)
);
newBalls.forEach((ball) => ball.setOnStop(this.onBallStop));
this.balls.push(...newBalls);
}
async shoot(mousePos, state) {
for (const ball of this.balls) {
if (state.ballDowning) return;
ball.shoot(mousePos);
await delay(Math.max(50 - ~~(state.ballCount / 10), 10));
}
}
display() {
this.balls.forEach((ball) => {
ball.update();
ball.checkEdges();
ball.display();
});
}
down() {
this.balls.forEach((ball) => ball.down());
}
addNewBall() {
this.addBalls({ ...state, ballCount: this.balls.length + 1 }, 1);
}
}
class GameManager {
constructor() {
this.state = {
ballPos: new Vector(width / 2, height - 100),
ballMoving: false,
ballCount: 1,
ballDowning: false,
brickMoving: false,
firstBallStop: true,
level: 1,
over: false,
};
this.balls = new Balls(this.state);
this.bricks = new Bricks();
this.bonusBalls = new BonusBalls();
this.ballLine = new BallLine();
this.ui = new UI();
this.scoreStorage = new LocalStorageManager();
this.state.bestLevel = this.scoreStorage.getBestScore();
this.addBallsAndBricks();
canvas.addEventListener("click", (e) => this.shootBalls(e));
window.addEventListener("keydown", (e) => {
if (e.key === 'p') this.togglePause();
});
sounds.backgroundMusic.loop = true;
sounds.backgroundMusic.play();
}
togglePause() {
const pauseMenu = document.getElementById('pauseMenu');
if (this.state.over) return;
this.state.ballMoving = !this.state.ballMoving;
pauseMenu.style.display = this.state.ballMoving ? 'none' : 'block';
if (this.state.ballMoving) {
sounds.backgroundMusic.play();
} else {
sounds.backgroundMusic.pause();
}
}
addBallsAndBricks() {
const bonusBallCount = this.bonusBalls.hitBallCount;
const newBrickIndeces = shuffle(range(6)).slice(
0,
Math.random() > 0.9 ? randomInt(5) : randomInt(4)
);
const newBonusBallIndex = shuffle(
range(6).filter((i) => !newBrickIndeces.includes(i))
)[0];
this.state.ballCount += bonusBallCount;
this.balls.addBalls(this.state, bonusBallCount);
this.bricks.addBricks(this.state, newBrickIndeces);
this.bonusBalls.addBonusBalls(this.state, newBonusBallIndex);
}
shootBalls(e) {
if (this.state.ballMoving || this.state.brickMoving || this.state.over) return;
this.state.ballPos = null;
this.state.ballMoving = true;
this.state.ballDowning = false;
this.state.firstBallStop = false;
const { x, y } = Mouse.position;
this.balls.shoot({ x, y: Math.min(y, 720) }, this.state);
}
checkCollision() {
this.balls.collideWithBricks(this.bricks);
this.bonusBalls.collideWithBall(this.balls);
this.bricks.break();
}
draw() {
clear();
this.ballLine.display(this.state);
this.balls.display();
this.bricks.display(this.state);
this.bonusBalls.display(this.state);
this.ui.display(this.state);
}
run() {
this.draw();
this.checkCollision();
if (this.state.ballMoving && this.balls.allStopped) {
this.state.ballMoving = false;
this.state.brickMoving = true;
this.state.level += 1;
this.scoreStorage.setScore(this.state.level);
this.state.bestLevel = this.scoreStorage.getBestScore();
this.addBallsAndBricks();
}
if (this.state.brickMoving) {
if (!this.bricks.shouldSlideDown(this.state)) {
this.state.brickMoving = false;
this.bonusBalls.removeHitBalls();
}
}
if (this.bricks.hitBottom) {
this.ui.gameOver();
sounds.gameOver.play();
this.state.over = true;
}
if (!this.state.over) {
window.requestAnimationFrame(() => this.run());
}
}
downBalls() {
this.state.ballDowning = true;
this.balls.down();
}
}
canvas.addEventListener("mousemove", (e) => {
const mx = e.pageX - canvas.offsetLeft;
const my = e.pageY;
Mouse.position = new Vector(mx, my);
});
const gameManager = new GameManager();
gameManager.run();
</script>
</body>
</html>
Key Features Included:
-
Multiple Ball Mechanics: Players can shoot multiple balls to destroy bricks and potentially earn bonus balls.
-
Brick Destruction Mechanics: Each brick has hit points and changes color or disappears when hit.
-
Power-Ups: Extra ball and score multiplier power-ups are available.
-
Pause Functionality: The game can be paused, and players can choose to resume or restart.
-
Dynamic Scoring System: Players earn scores through hitting bricks and collecting power-ups.
-
Sound Effects and Music: Sounds enhance gameplay with effects for hits, scores, and game over events.
-
Game Over Logic: The screen can display a game-over message when certain conditions are met.
Implementation Notes:
- Sound Paths: Ensure to replace the sound file paths with valid URLs or file paths where your sound files are stored.
- Collision Logic: Further enhancements can be made to collision detection and game mechanics based on your requirements.
- Graphics: Consider improving visual aspects of the game to make it more engaging.
Feel free to customize the game further by adding features, improving visuals, or optimizing gameplay as necessary!