WEBLEB
Accueil
Éditeur
Chat
Défi
Connexion
Pro
Français
English
Français
Español
Éclater
256
0
Jeux
TacoGamer
Suivre
0
Remixer
Ouvrir dans l'éditeur
Plein écran
Discuter
Publiez votre code
0
HTML
Copy
Breakout Game
BREAKOUT
Use Left/Right arrows or Move Mouse to play
CSS
Copy
* { margin: 0; padding: 0; box-sizing: border-box; } body { background-color: #1a1a2e; color: #ffffff; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; overflow: hidden; } #game-container { text-align: center; } h1 { font-size: 2.5rem; margin-bottom: 10px; letter-spacing: 5px; color: #00fff0; } canvas { background: #16213e; display: block; margin: 0 auto; border: 4px solid #00fff0; border-radius: 8px; box-shadow: 0 0 20px rgba(0, 255, 240, 0.5); } p { margin-top: 15px; font-size: 0.9rem; color: #8a8a8a; }
JS
Copy
const canvas = document.getElementById("gameCanvas"); const ctx = canvas.getContext("2d"); // Ball Properties let ballRadius = 8; let x = canvas.width / 2; let y = canvas.height - 30; let dx = 2; let dy = -2; // Paddle Properties let paddleHeight = 10; let paddleWidth = 75; let paddleX = (canvas.width - paddleWidth) / 2; let rightPressed = false; let leftPressed = false; // Brick Properties let brickRowCount = 3; let brickColumnCount = 5; let brickWidth = 75; let brickHeight = 20; let brickPadding = 10; let brickOffsetTop = 30; let brickOffsetLeft = 30; // Game State let score = 0; let lives = 3; // Initialize Bricks Array let bricks = []; for (let c = 0; c < brickColumnCount; c++) { bricks[c] = []; for (let r = 0; r < brickRowCount; r++) { bricks[c][r] = { x: 0, y: 0, status: 1 }; } } // Event Listeners document.addEventListener("keydown", keyDownHandler, false); document.addEventListener("keyup", keyUpHandler, false); document.addEventListener("mousemove", mouseMoveHandler, false); function keyDownHandler(e) { if (e.key === "Right" || e.key === "ArrowRight") rightPressed = true; else if (e.key === "Left" || e.key === "ArrowLeft") leftPressed = true; } function keyUpHandler(e) { if (e.key === "Right" || e.key === "ArrowRight") rightPressed = false; else if (e.key === "Left" || e.key === "ArrowLeft") leftPressed = false; } function mouseMoveHandler(e) { const relativeX = e.clientX - canvas.getBoundingClientRect().left; if (relativeX > 0 && relativeX < canvas.width) { paddleX = relativeX - paddleWidth / 2; } } // Collision Detection function collisionDetection() { for (let c = 0; c < brickColumnCount; c++) { for (let r = 0; r < brickRowCount; r++) { let 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; score++; if (score === brickRowCount * brickColumnCount) { alert("YOU WIN, CONGRATULATIONS!"); document.location.reload(); } } } } } } // Drawing Functions function drawBall() { ctx.beginPath(); ctx.arc(x, y, ballRadius, 0, Math.PI * 2); ctx.fillStyle = "#ff007f"; ctx.fill(); ctx.closePath(); } function drawPaddle() { ctx.beginPath(); ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight); ctx.fillStyle = "#00fff0"; ctx.fill(); ctx.closePath(); } function drawBricks() { for (let c = 0; c < brickColumnCount; c++) { for (let r = 0; r < brickRowCount; r++) { if (bricks[c][r].status === 1) { let brickX = (c * (brickWidth + brickPadding)) + brickOffsetLeft; let brickY = (r * (brickHeight + brickPadding)) + brickOffsetTop; bricks[c][r].x = brickX; bricks[c][r].y = brickY; ctx.beginPath(); ctx.rect(brickX, brickY, brickWidth, brickHeight); ctx.fillStyle = "#e2b411"; ctx.fill(); ctx.closePath(); } } } } function drawScore() { ctx.font = "16px Arial"; ctx.fillStyle = "#ffffff"; ctx.fillText("Score: " + score, 8, 20); } function drawLives() { ctx.font = "16px Arial"; ctx.fillStyle = "#ffffff"; ctx.fillText("Lives: " + lives, canvas.width - 65, 20); } // Main Game Loop function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear screen for redraw drawBricks(); drawBall(); drawPaddle(); drawScore(); drawLives(); collisionDetection(); // Wall Collision (Left/Right) if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) { dx = -dx; } // Wall Collision (Top) if (y + dy < ballRadius) { dy = -dy; } // Bottom Collision (Paddle or Game Over) else if (y + dy > canvas.height - ballRadius) { if (x > paddleX && x < paddleX + paddleWidth) { dy = -dy; } else { lives--; if (!lives) { alert("GAME OVER"); document.location.reload(); } else { // Reset positions x = canvas.width / 2; y = canvas.height - 30; dx = 2; dy = -2; paddleX = (canvas.width - paddleWidth) / 2; } } } // Move Paddle if (rightPressed && paddleX < canvas.width - paddleWidth) { paddleX += 5; } else if (leftPressed && paddleX > 0) { paddleX -= 5; } // Move Ball x += dx; y += dy; requestAnimationFrame(draw); } // Start Game Loop draw();
Commentaires
0
Connectez-vous pour participer à la discussion
…