写一段HTML语言的跑酷小游戏
时间: 2023-01-15 22:08:23 浏览: 115
好的,下面是一段用 HTML 语言写的跑酷小游戏的示例代码:
```
<html>
<head>
<title>跑酷小游戏</title>
<style>
body {
background-color: #333;
color: #fff;
font-family: Arial, sans-serif;
}
#game {
width: 500px;
height: 500px;
margin: 0 auto;
position: relative;
}
.obstacle {
position: absolute;
width: 50px;
height: 50px;
background-color: #f00;
}
.character {
position: absolute;
width: 50px;
height: 50px;
background-color: #0f0;
bottom: 0;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div id="game">
<!-- 这里是障碍物 -->
<div class="obstacle" style="top: 100px; left: 0;"></div>
<div class="obstacle" style="top: 200px; left: 100px;"></div>
<div class="obstacle" style="top: 300px; left: 200px;"></div>
<!-- 这里是角色 -->
<div class="character"></div>
</div>
<script>
// 获取游戏区域和角色
var game = document.getElementById('game');
var character = document.querySelector('.character');
// 角色的初始位置
var characterX = 50;
var characterY = 0;
// 角色的速度
var speed = 5;
// 游戏是否开始
var isPlaying = false;
// 游戏循环
function gameLoop() {
if (!isPlaying) {
return;
}
// 更新角色位置
characterY += speed;
character.style.bottom = characterY + 'px';
// 判断是否撞到障碍物
var obstacles = document.querySelectorAll('.obstacle');
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var obstacleX = obstacle.offsetLeft;
var obstacleY = obstacle.offsetTop;
if (characterX < obstacleX + 50 && characterX + 50 > obstacleX && characterY < obstacleY + 50 && characterY + 50 > obstacleY) {
// 游戏结束
isPlaying = false;
阅读全文