可以帮我写一个贪吃蛇的html小游戏么
时间: 2023-04-09 17:01:22 浏览: 104
当然可以,我可以为您提供一个基本的贪吃蛇游戏的HTML代码,您可以根据自己的需求进行修改和完善。以下是代码:
<!DOCTYPE html>
<html>
<head>
<title>贪吃蛇小游戏</title>
<style>
body {
background-color: #f5f5f5;
}
#game-board {
width: 400px;
height: 400px;
margin: 0 auto;
border: 1px solid #ccc;
position: relative;
}
.snake {
width: 10px;
height: 10px;
background-color: #333;
position: absolute;
top: 0;
left: 0;
}
.food {
width: 10px;
height: 10px;
background-color: red;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="game-board"></div>
<script>
// 初始化游戏区域
var gameBoard = document.getElementById('game-board');
var boardWidth = gameBoard.offsetWidth;
var boardHeight = gameBoard.offsetHeight;
var cellWidth = 10;
var cellHeight = 10;
var rows = boardHeight / cellHeight;
var cols = boardWidth / cellWidth;
var cells = [];
for (var i = 0; i < rows; i++) {
var row = [];
for (var j = 0; j < cols; j++) {
var cell = document.createElement('div');
cell.className = 'cell';
cell.style.width = cellWidth + 'px';
cell.style.height = cellHeight + 'px';
cell.style.top = i * cellHeight + 'px';
cell.style.left = j * cellWidth + 'px';
gameBoard.appendChild(cell);
row.push(cell);
}
cells.push(row);
}
// 初始化蛇和食物
var snake = [{x: 0, y: 0}];
var food = {x: 0, y: 0};
var direction = 'right';
var score = 0;
var scoreBoard = document.createElement('div');
scoreBoard.innerHTML = '得分:' + score;
gameBoard.appendChild(scoreBoard);
function initSnake() {
for (var i = 0; i < snake.length; i++) {
var cell = cells[snake[i].y][snake[i].x];
cell.className = 'snake';
}
}
function initFood() {
food.x = Math.floor(Math.random() * cols);
food.y = Math.floor(Math.random() * rows);
var cell = cells[food.y][food.x];
cell.className = 'food';
}
initSnake();
initFood();
// 监听键盘事件
document.addEventListener('keydown', function(event) {
switch (event.keyCode) {
case 37: // 左
if (direction !== 'right') {
direction = 'left';
}
break;
case 38: // 上
if (direction !== 'down') {
direction = 'up';
}
break;
case 39: // 右
if (direction !== 'left') {
direction = 'right';
}
break;
case 40: // 下
if (direction !== 'up') {
direction = 'down';
}
break;
}
});
// 移动蛇
function moveSnake() {
var head = snake[0];
switch (direction) {
case 'left':
snake.unshift({x: head.x - 1, y: head.y});
break;
case 'up':
snake.unshift({x: head.x, y: head.y - 1});
break;
case 'right':
snake.unshift({x: head.x + 1, y: head.y});
break;
case 'down':
snake.unshift({x: head.x, y: head.y + 1});
break;
}
var tail = snake.pop();
var headCell = cells[snake[0].y][snake[0].x];
if (snake[0].x < 0 || snake[0].x >= cols || snake[0].y < 0 || snake[0].y >= rows) {
alert('游戏结束,得分:' + score);
location.reload();
} else if (headCell.className === 'snake') {
alert('游戏结束,得分:' + score);
location.reload();
} else if (headCell.className === 'food') {
score++;
scoreBoard.innerHTML = '得分:' + score;
snake.push(tail);
initFood();
} else {
headCell.className = 'snake';
var tailCell = cells[tail.y][tail.x];
tailCell.className = '';
}
}
setInterval(moveSnake, 100);
</script>
</body>
</html>
阅读全文