Write a long HTML/CSS/JS code following the specs:
Breakout game.
The paddle is a XP-like progressbar that can be dragged horizontally.
The bricks can be blue, yellow and red, and are randomly generated.
When one brick is broken, a rectangular segment matching the color of the broken brick will start to slowly fall down vertically, and that can be collected by the progressbar.
etc...
Assistant
Here is the complete HTML, CSS, and JavaScript code for a Breakout game that meets your specifications:
The paddle is an XP-like progress bar that can be dragged horizontally.
The bricks are randomly generated in blue, yellow, and red.
When a brick is broken, a rectangular segment matching the color of the broken brick slowly falls down vertically.
The falling segments can be collected by the progress bar to fill it up.
<script>
// Get the canvas element and context
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Game variables
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
// Paddle variables
const paddleHeight = 20;
const paddleWidth = 200;
let paddleX = (canvasWidth - paddleWidth) / 2;
let paddleProgress = 0; // Progress bar fill (0 to 100)
// Ball variables
const ballRadius = 10;
let x = canvasWidth / 2;
let y = canvasHeight - 30;
let dx = 4;
let dy = -4;
// Brick variables
const brickRowCount = 5;
const brickColumnCount = 8;
const brickWidth = 75;
const brickHeight = 20;
const brickPadding = 10;
const brickOffsetTop = 60;
const brickOffsetLeft = 35;
// Colors for bricks
const brickColors = ["#007bff", "#ffc107", "#dc3545"]; // Blue, Yellow, Red
// Bricks array
let bricks = [];
for (let c = 0; c < brickColumnCount; c++) {
bricks[c] = [];
for (let r = 0; r < brickRowCount; r++) {
const color = brickColors[Math.floor(Math.random() * brickColors.length)];
bricks[c][r] = { x: 0, y: 0, status: 1, color: color };
}
}
// Falling segments array
let segments = [];
// Mouse controls
let isDragging = false;
let dragOffsetX = 0;
// Event listeners for mouse and touch controls
canvas.addEventListener("mousedown", mouseDownHandler, false);
canvas.addEventListener("mouseup", mouseUpHandler, false);
canvas.addEventListener("mousemove", mouseMoveHandler, false);
canvas.addEventListener("touchstart", touchStartHandler, false);
canvas.addEventListener("touchend", touchEndHandler, false);
canvas.addEventListener("touchmove", touchMoveHandler, false);
// Mouse event handlers
function mouseDownHandler(e) {
if (isOverPaddle(e.clientX, e.clientY)) {
isDragging = true;
dragOffsetX = e.clientX - paddleX;
}
}
function mouseUpHandler(e) {
isDragging = false;
}
function mouseMoveHandler(e) {
if (isDragging) {
paddleX = e.clientX - dragOffsetX;
if (paddleX < 0) paddleX = 0;
if (paddleX + paddleWidth > canvasWidth) paddleX = canvasWidth - paddleWidth;
}
}
// Touch event handlers
function touchStartHandler(e) {
const touch = e.touches[0];
if (isOverPaddle(touch.clientX, touch.clientY)) {
isDragging = true;
dragOffsetX = touch.clientX - paddleX;
}
}
function touchEndHandler(e) {
isDragging = false;
}
function touchMoveHandler(e) {
if (isDragging) {
const touch = e.touches[0];
paddleX = touch.clientX - dragOffsetX;
if (paddleX < 0) paddleX = 0;
if (paddleX + paddleWidth > canvasWidth) paddleX = canvasWidth - paddleWidth;
}
}
// Check if the mouse is over the paddle
function isOverPaddle(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
const mouseX = clientX - rect.left;
const mouseY = clientY - rect.top;
return mouseY > canvasHeight - paddleHeight && mouseX > paddleX && mouseX < paddleX + paddleWidth;
}
// Draw the paddle as a progress bar
function drawPaddle() {
// Draw paddle background
ctx.fillStyle = "#cccccc";
ctx.fillRect(paddleX, canvasHeight - paddleHeight, paddleWidth, paddleHeight);
// Draw progress bar fill
ctx.fillStyle = "#4caf50";
const fillWidth = (paddleProgress / 100) * paddleWidth;
ctx.fillRect(paddleX, canvasHeight - paddleHeight, fillWidth, paddleHeight);
// Draw paddle border
ctx.strokeStyle = "#000";
ctx.strokeRect(paddleX, canvasHeight - paddleHeight, paddleWidth, paddleHeight);
}
// Draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "#ffffff";
ctx.fill();
ctx.closePath();
}
// Draw the bricks
function drawBricks() {
for (let c = 0; c < brickColumnCount; c++) {
for (let r = 0; r < brickRowCount; r++) {
const b = bricks[c][r];
if (b.status === 1) {
const brickX = c * (brickWidth + brickPadding) + brickOffsetLeft;
const brickY = r * (brickHeight + brickPadding) + brickOffsetTop;
b.x = brickX;
b.y = brickY;
ctx.fillStyle = b.color;
ctx.fillRect(brickX, brickY, brickWidth, brickHeight);
}
}
}
}
// Draw the falling segments
function drawSegments() {
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
ctx.fillStyle = segment.color;
ctx.fillRect(segment.x, segment.y, segment.width, segment.height);
}
}
// Collision detection
function collisionDetection() {
for (let c = 0; c < brickColumnCount; c++) {
for (let r = 0; r < brickRowCount; r++) {
const b = bricks[c][r];
if (b.status === 1) {
if (
x > b.x &&
x < b.x + brickWidth &&
y > b.y &&
y < b.y + brickHeight
) {
dy = -dy;
b.status = 0;
// Create a new falling segment
segments.push({
x: b.x + brickWidth / 2 - 10,
y: b.y,
width: 20,
height: 10,
color: b.color,
speed: 1
});
}
}
}
}
}
// Update falling segments
function updateSegments() {
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
segment.y += segment.speed;
// Check if the segment is collected by the paddle
if (
segment.y + segment.height >= canvasHeight - paddleHeight &&
segment.x + segment.width > paddleX &&
segment.x < paddleX + paddleWidth
) {
// Increase paddle progress
paddleProgress += 5;
if (paddleProgress > 100) paddleProgress = 100;
// Remove the collected segment
segments.splice(i, 1);
i--;
} else if (segment.y > canvasHeight) {
// Remove segment if it falls off the canvas
segments.splice(i, 1);
i--;
}
}
}
// Draw the game
function draw() {
// Clear the canvas
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// Draw elements
drawBricks();
drawBall();
drawPaddle();
drawSegments();
// Collision detection
collisionDetection();
// Update positions
x += dx;
y += dy;
// Ball collision with walls
if (x + dx > canvasWidth - ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if (y + dy < ballRadius) {
dy = -dy;
} else if (y + dy > canvasHeight - ballRadius - paddleHeight) {
if (x > paddleX && x < paddleX + paddleWidth) {
dy = -dy;
} else if (y + dy > canvasHeight - ballRadius) {
// Game over condition
alert("GAME OVER");
document.location.reload();
}
}
// Update segments
updateSegments();
requestAnimationFrame(draw);
}
// Start the game
draw();
</script>
Explanation of the Code
HTML Structure: A simple HTML document with a <canvas> element where the game will be rendered.
CSS Styling: Basic styles to set the canvas in the center and set the background colors.
JavaScript Logic:
Initialization: Set up the canvas, context, and game variables including the paddle, ball, bricks, and falling segments.
Event Handlers: Add event listeners for mouse and touch controls to drag the paddle horizontally.
Game Functions:
drawPaddle(): Draws the paddle as a progress bar with a filled portion representing the collected segments.
drawBall(): Draws the game ball.
drawBricks(): Draws the bricks with random colors (blue, yellow, red).
drawSegments(): Draws the falling segments.
collisionDetection(): Handles collision between the ball and bricks, and initiates the creation of falling segments when a brick is broken.
updateSegments(): Updates the position of falling segments and checks for collection by the paddle.
Game Loop:
Clears the canvas.
Calls the drawing functions.
Updates the ball's position and handles wall collisions.
Recursively calls draw() using requestAnimationFrame for smooth animation.
Note: This code is designed to be a starting point and may require additional enhancements for features like scoring, lives, and level progression. Be sure to test the game in a modern web browser that supports HTML5 Canvas.