写一段贪吃蛇游戏的html代码
时间: 2023-11-14 18:38:08 浏览: 201
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇游戏</title>
<style type="text/css">
#game-board {
width: 400px;
height: 400px;
border: 1px solid black;
position: relative;
}
.snake {
width: 20px;
height: 20px;
background-color: green;
position: absolute;
}
.food {
width: 20px;
height: 20px;
background-color: red;
position: absolute;
}
</style>
</head>
<body>
<div id="game-board">
<!-- 初始的蛇和食物 -->
<div class="snake" id="snake-head" style="left: 200px; top: 200px;"></div>
<div class="food" id="food" style="left: 100px; top: 100px;"></div>
</div>
<script type="text/javascript">
// 定义全局变量
var snake = document.getElementById('snake-head');
var food = document.getElementById('food');
var gameBoard = document.getElementById('game-board');
var snakeSpeed = 20;
var snakeDirection = 'right';
var score = 0;
// 随机生成食物位置
function generateFood() {
var x = Math.floor(Math.random() * (gameBoard.offsetWidth / 20)) * 20;
var y = Math.floor(Math.random() * (gameBoard.offsetHeight / 20)) * 20;
food.style.left = x + 'px';
food.style.top = y + 'px';
}
// 移动蛇
function moveSnake() {
// 根据方向移动蛇头
switch(snakeDirection) {
case 'right':
snake.style.left = parseInt(snake.style.left) + snakeSpeed + 'px';
break;
case 'left':
snake.style.left = parseInt(snake.style.left) - snakeSpeed + 'px';
break;
case 'up':
snake.style.top = parseInt(snake.style.top) - snakeSpeed + 'px';
break;
case 'down':
snake.style.top = parseInt(snake.style.top) + snakeSpeed + 'px';
break;
}
// 判断是否吃到食物
if (snake.offsetLeft === food.offsetLeft && snake.offsetTop === food.offsetTop) {
// 生成新的食物
generateFood();
// 增加分数
score++;
// 在蛇尾添加一个新的方块
var newSnakePart = document.createElement('div');
newSnakePart.className = 'snake';
gameBoard.appendChild(newSnakePart);
}
// 判断是否撞到边界或自己
if (snake.offsetLeft < 0 || snake.offsetLeft >= gameBoard.offsetWidth ||
snake.offsetTop < 0 || snake.offsetTop >= gameBoard.offsetHeight ||
checkCollision()) {
// 游戏结束
alert('Game Over! Your score is ' + score);
location.reload();
}
// 移动蛇的每一节身体
var snakeParts = document.getElementsByClassName('snake');
for (var i = snakeParts.length - 1; i > 0; i--) {
snakeParts[i].style.left = snakeParts[i - 1].offsetLeft + 'px';
snakeParts[i].style.top = snakeParts[i - 1].offsetTop + 'px';
}
// 更新蛇头的位置
snakeParts[0].style.left = snake.style.left;
snakeParts[0].style.top = snake.style.top;
}
// 检查蛇是否撞到自己
function checkCollision() {
var snakeParts = document.getElementsByClassName('snake');
for (var i = 1; i < snakeParts.length; i++) {
if (snake.offsetLeft === snakeParts[i].offsetLeft && snake.offsetTop === snakeParts[i].offsetTop) {
return true;
}
}
return false;
}
// 监听键盘事件
document.addEventListener('keydown', function(event) {
switch(event.keyCode) {
case 37:
if (snakeDirection !== 'right') {
snakeDirection = 'left';
}
break;
case 38:
if (snakeDirection !== 'down') {
snakeDirection = 'up';
}
break;
case 39:
if (snakeDirection !== 'left') {
snakeDirection = 'right';
}
break;
case 40:
if (snakeDirection !== 'up') {
snakeDirection = 'down';
}
break;
}
});
// 每隔一定时间移动蛇
setInterval(moveSnake, 100);
</script>
</body>
</html>
阅读全文