napisz jakąs fajna gierkę do pogrania w html
- User
napisz jakąs fajna gierkę do pogrania w html
Assistant
Oczywiście! Oto prosty przykład gry w HTML, CSS i JavaScript. Będzie to gra "Przełamanie bloków" (Breakout), w której gracz kontroluje paletkę, aby odbić piłkę i zniszczyć wszystkie bloki.
HTML (index.html)
<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Przełamanie bloków</title> <link rel="stylesheet" href="styles.css"> </head> <body> <canvas id="gameCanvas" width="800" height="600"></canvas> <script src="script.js"></script> </body> </html>CSS (styles.css)
body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #000; } canvas { border: 1px solid #fff; }JavaScript (script.js)
const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const paddleWidth = 100; const paddleHeight = 10; const ballRadius = 10; let paddleX = (canvas.width - paddleWidth) / 2; let ballX = canvas.width / 2; let ballY = canvas.height - 30; let ballSpeedX = 2; let ballSpeedY = -2; let rightPressed = false; let leftPressed = false; const brickRowCount = 5; const brickColumnCount = 9; const brickWidth = 75; const brickHeight = 20; const brickPadding = 10; const brickOffsetTop = 30; const brickOffsetLeft = 30; const 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 }; } } document.addEventListener('keydown', keyDownHandler, false); document.addEventListener('keyup', keyUpHandler, 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 drawPaddle() { ctx.beginPath(); ctx.rect(paddleX, canvas.height - paddleHeight, paddleWidth, paddleHeight); ctx.fillStyle = '#0095DD'; ctx.fill(); ctx.closePath(); } function drawBall() { ctx.beginPath(); ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2); ctx.fillStyle = '#0095DD'; 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) { const brickX = (c * (brickWidth + brickPadding)) + brickOffsetLeft; const 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 = '#0095DD'; ctx.fill(); ctx.closePath(); } } } } 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 (ballX > b.x && ballX < b.x + brickWidth && ballY > b.y && ballY < b.y + brickHeight) { ballSpeedY = -ballSpeedY; b.status = 0; } } } } } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawBricks(); drawBall(); drawPaddle(); collisionDetection(); if (ballX + ballSpeedX > canvas.width - ballRadius || ballX + ballSpeedX < ballRadius) { ballSpeedX = -ballSpeedX; } if (ballY + ballSpeedY < ballRadius) { ballSpeedY = -ballSpeedY; } else if (ballY + ballSpeedY > canvas.height - ballRadius) { if (ballX > paddleX && ballX < paddleX + paddleWidth) { ballSpeedY = -ballSpeedY; } else { document.location.reload(); } } if (rightPressed && paddleX < canvas.width - paddleWidth) { paddleX += 7; } else if (leftPressed && paddleX > 0) { paddleX -= 7; } ballX += ballSpeedX; ballY += ballSpeedY; requestAnimationFrame(draw); } draw();Ten kod tworzy prostą grę "Przełamanie bloków", w której gracz kontroluje paletkę za pomocą klawiszy strzałek, aby odbić piłkę i zniszczyć wszystkie bloki. Gdy piłka uderzy w blok, zmienia kierunek, a blok znika. Gdy piłka uderzy w dolną krawędź ekranu, gra się restartuje.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.