你知道chrome浏览器的小恐龙游戏吗,现在用html页面去实现
- User
你知道chrome浏览器的小恐龙游戏吗,现在用html页面去实现
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
你知道chrome浏览器的小恐龙游戏吗,现在用html页面去实现
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
好的,我们来用 HTML、CSS 和 JavaScript 实现 Chrome 浏览器的小恐龙游戏。这是一个简化版本,主要关注核心游戏逻辑。
1. HTML (index.html)
<!DOCTYPE html>
<html>
<head>
<title>Chrome Dino Game</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="game-container">
<div id="dino"></div>
<div id="obstacles"></div>
</div>
<div id="score">Score: 0</div>
<script src="script.js"></script>
</body>
</html>
2. CSS (style.css)
#game-container {
width: 600px;
height: 200px;
border: 1px solid black;
position: relative;
overflow: hidden; /* 隐藏超出容器的障碍物 */
background-color: #77c3ff; /* 天空颜色 */
}
#dino {
width: 50px;
height: 50px;
background-image: url('dino.png'); /* 替换为恐龙图片 */
background-size: cover;
position: absolute;
bottom: 0;
left: 50px;
}
.obstacle {
width: 30px;
height: 50px;
background-image: url('cactus.png'); /* 替换为仙人掌图片 */
background-size: cover;
position: absolute;
bottom: 0;
right: -30px; /* 初始位置在容器右侧 */
}
#score {
font-size: 20px;
margin-top: 10px;
}
3. JavaScript (script.js)
const dino = document.getElementById('dino');
const obstaclesContainer = document.getElementById('obstacles');
const scoreElement = document.getElementById('score');
let dinoY = 0;
let gravity = 0.5;
let jumpHeight = -15;
let isJumping = false;
let gameSpeed = 5;
let score = 0;
let gameInterval;
// 恐龙跳跃
function jump() {
if (!isJumping) {
isJumping = true;
dinoY = jumpHeight;
function animateJump() {
dinoY += gravity;
dino.style.bottom = dinoY + 'px';
if (dinoY >= 0) {
dinoY = 0;
isJumping = false;
clearInterval(jumpInterval);
}
}
const jumpInterval = setInterval(animateJump, 20);
}
}
// 创建障碍物
function createObstacle() {
const obstacle = document.createElement('div');
obstacle.classList.add('obstacle');
obstaclesContainer.appendChild(obstacle);
let obstacleX = 600;
obstacle.style.right = obstacleX + 'px';
function moveObstacle() {
obstacleX -= gameSpeed;
obstacle.style.right = obstacleX + 'px';
// 检测碰撞
if (obstacleX < 50 && obstacleX > 0 && dinoY === 0) {
gameOver();
}
// 移除超出屏幕的障碍物
if (obstacleX < -30) {
obstacle.remove();
score++;
scoreElement.textContent = 'Score: ' + score;
}
}
const obstacleInterval = setInterval(moveObstacle, 20);
return obstacleInterval;
}
// 游戏结束
function gameOver() {
clearInterval(gameInterval);
clearInterval(obstacleInterval);
alert('Game Over! Score: ' + score);
// 可以添加重置游戏的功能
}
// 游戏主循环
function startGame() {
gameInterval = setInterval(() => {
createObstacle();
}, 1500); // 每 1.5 秒创建一个障碍物
}
// 监听键盘事件
document.addEventListener('keydown', (event) => {
if (event.key === ' ') {
jump();
}
});
// 开始游戏
startGame();
说明:
dino.png 和 cactus.png 为实际的图片路径。jump(): 实现恐龙跳跃的逻辑。createObstacle(): 创建新的障碍物,并将其添加到障碍物容器中。moveObstacle(): 移动障碍物,并检测与恐龙的碰撞。gameOver(): 游戏结束时停止游戏循环,并显示游戏结束信息。startGame(): 启动游戏主循环,定期创建障碍物。jump() 函数。如何运行:
index.html、style.css 和 script.js 文件。dino.png) 和仙人掌图片 (cactus.png),并将它们放在与 HTML、CSS 和 JavaScript 文件相同的目录下。index.html 文件。改进方向:
这个例子提供了一个基本的游戏框架。你可以根据自己的想法进行扩展和改进,创造出更丰富有趣的小恐龙游戏。 记住,要替换 dino.png 和 cactus.png 为你自己的图片。