gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
創建一個跑酷遊戲
- User
創建一個跑酷遊戲
- Assistant
當然可以!以下是一個簡單的 HTML5 跑酷遊戲範例,使用 JavaScript 和 Canvas。這個遊戲主角是一個方塊,可以跳躍來避開障礙物。
你可以將以下程式碼複製到一個
.html檔案中,然後用瀏覽器打開即可遊玩。
<!DOCTYPE html> <html lang="zh-Hant"> <head> <meta charset="UTF-8"> <title>簡易跑酷遊戲</title> <style> body { background: #222; } canvas { background: #fff; display: block; margin: 40px auto; border: 2px solid #333; } #score { color: #fff; text-align: center; font-size: 24px; } </style> </head> <body> <div id="score">分數: 0</div> <canvas id="game" width="600" height="300"></canvas> <script> const canvas = document.getElementById('game'); const ctx = canvas.getContext('2d'); const scoreDiv = document.getElementById('score'); // 主角 const player = { x: 50, y: 220, width: 40, height: 40, color: '#3498db', dy: 0, jumpPower: -12, gravity: 0.6, onGround: true }; // 障礙物 let obstacles = []; let obstacleTimer = 0; let obstacleInterval = 90; // 幾幀出現一個障礙物 // 分數 let score = 0; let gameOver = false; function resetGame() { player.y = 220; player.dy = 0; player.onGround = true; obstacles = []; score = 0; gameOver = false; obstacleTimer = 0; scoreDiv.textContent = '分數: 0'; loop(); } function drawPlayer() { ctx.fillStyle = player.color; ctx.fillRect(player.x, player.y, player.width, player.height); } function drawObstacles() { ctx.fillStyle = '#e74c3c'; obstacles.forEach(ob => { ctx.fillRect(ob.x, ob.y, ob.width, ob.height); }); } function updatePlayer() { player.dy += player.gravity; player.y += player.dy; // 地面判斷 if (player.y >= 220) { player.y = 220; player.dy = 0; player.onGround = true; } else { player.onGround = false; } } function updateObstacles() { obstacles.forEach(ob => { ob.x -= 6; }); // 移除離開畫面的障礙物 obstacles = obstacles.filter(ob => ob.x + ob.width > 0); } function addObstacle() { const height = 40 + Math.random() * 30; obstacles.push({ x: canvas.width, y: 260 - height, width: 30, height: height }); } function checkCollision() { for (let ob of obstacles) { if ( player.x < ob.x + ob.width && player.x + player.width > ob.x && player.y < ob.y + ob.height && player.y + player.height > ob.y ) { return true; } } return false; } function loop() { if (gameOver) return; ctx.clearRect(0, 0, canvas.width, canvas.height); // 更新 updatePlayer(); updateObstacles(); // 障礙物生成 obstacleTimer++; if (obstacleTimer > obstacleInterval) { addObstacle(); obstacleTimer = 0; // 隨機調整障礙物間隔 obstacleInterval = 60 + Math.random() * 60; } // 碰撞檢查 if (checkCollision()) { gameOver = true; scoreDiv.textContent = `遊戲結束!分數: ${score},按空白鍵重新開始`; return; } // 分數 score++; scoreDiv.textContent = `分數: ${score}`; // 繪製 drawPlayer(); drawObstacles(); requestAnimationFrame(loop); } // 跳躍控制 document.addEventListener('keydown', function(e) { if (e.code === 'Space') { if (gameOver) { resetGame(); } else if (player.onGround) { player.dy = player.jumpPower; } } }); // 開始遊戲 loop(); </script> </body> </html>
玩法說明:
- 按下空白鍵(Space)讓主角跳躍,避開紅色障礙物。
- 撞到障礙物會遊戲結束,再按一次空白鍵重新開始。
你可以根據需要進一步美化或擴充功能!如果想要更進階的功能(如雙跳、道具、音效等),歡迎再詢問!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.