网页贪吃蛇代码,分简单模式和困难模式
时间: 2023-10-26 15:05:00 浏览: 35
以下是一个简单模式的网页贪吃蛇代码:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>简单模式贪吃蛇</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript:
```javascript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var blockSize = 10;
var width = canvas.width / blockSize;
var height = canvas.height / blockSize;
var snake = {
body: [[6,4], [5,4], [4,4], [3,4], [2,4]],
direction: "right"
};
var apple = {
position: [Math.floor(Math.random() * width), Math.floor(Math.random() * height)]
};
var score = 0;
function drawBlock(ctx, position) {
var x = position[0] * blockSize;
var y = position[1] * blockSize;
ctx.fillRect(x, y, blockSize, blockSize);
}
function drawSnake(ctx, snake) {
ctx.fillStyle = "green";
snake.body.forEach(function (block) {
drawBlock(ctx, block);
})
}
function drawApple(ctx, apple) {
ctx.fillStyle = "red";
drawBlock(ctx, apple.position);
}
function moveSnake(snake) {
var head = snake.body[0];
if (snake.direction === "right") {
var newHead = [head[0] + 1, head[1]];
} else if (snake.direction === "left") {
var newHead = [head[0] - 1, head[1]];
} else if (snake.direction === "up") {
var newHead = [head[0], head[1] - 1];
} else if (snake.direction === "down") {
var newHead = [head[0], head[1] + 1];
}
snake.body.unshift(newHead);
if (newHead[0] === apple.position[0] && newHead[1] === apple.position[1]) {
apple.position = [Math.floor(Math.random() * width), Math.floor(Math.random() * height)];
score++;
} else {
snake.body.pop();
}
}
function collision(snake) {
var head = snake.body[0];
if (head[0] < 0 || head[0] >= width || head[1] < 0 || head[1] >= height) {
return true;
}
for (var i = 1; i < snake.body.length; i++) {
if (snake.body[i][0] === head[0] && snake.body[i][1] === head[1]) {
return true;
}
}
return false;
}
function updateScore(score) {
document.getElementById("score").innerHTML = "Score: " + score;
}
document.addEventListener("keydown", function (event) {
if (event.keyCode === 37 && snake.direction !== "right") {
snake.direction = "left";
} else if (event.keyCode === 38 && snake.direction !== "down") {
snake.direction = "up";
} else if (event.keyCode === 39 && snake.direction !== "left") {
snake.direction = "right";
} else if (event.keyCode === 40 && snake.direction !== "up") {
snake.direction = "down";
}
});
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnake(ctx, snake);
drawApple(ctx, apple);
moveSnake(snake);
updateScore(score);
if (collision(snake)) {
clearInterval(interval);
alert("Game over!");
}
}
var interval = setInterval(gameLoop, 100);
```
以下是一个困难模式的网页贪吃蛇代码:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>困难模式贪吃蛇</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="snake.js"></script>
</body>
</html>
```
JavaScript:
```javascript
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var blockSize = 10;
var width = canvas.width / blockSize;
var height = canvas.height / blockSize;
var snake = {
body: [[6,4], [5,4], [4,4], [3,4], [2,4]],
direction: "right"
};
var apple = {
position: [Math.floor(Math.random() * width), Math.floor(Math.random() * height)]
};
var score = 0;
var obstacle = {
position: [[9,9], [10,9], [11,9], [12,9], [13,9], [14,9], [15,9], [16,9], [17,9], [18,9], [19,9], [20,9], [21,9], [22,9], [23,9], [24,9],
[9,10], [10,10], [11,10], [12,10], [13,10], [14,10], [15,10], [16,10], [17,10], [18,10], [19,10], [20,10], [21,10], [22,10], [23,10], [24,10],
[9,11], [10,11], [11,11], [12,11], [13,11], [14,11], [15,11], [16,11], [17,11], [18,11], [19,11], [20,11], [21,11], [22,11], [23,11], [24,11],
[9,12], [10,12], [11,12], [12,12], [13,12], [14,12], [15,12], [16,12], [17,12], [18,12], [19,12], [20,12], [21,12], [22,12], [23,12], [24,12],
[9,13], [10,13], [11,13], [12,13], [13,13], [14,13], [15,13], [16,13], [17,13], [18,13], [19,13], [20,13], [21,13], [22,13], [23,13], [24,13],
[9,14], [10,14], [11,14], [12,14], [13,14], [14,14], [15,14], [16,14], [17,14], [18,14], [19,14], [20,14], [21,14], [22,14], [23,14], [24,14]]
};
function drawBlock(ctx, position) {
var x = position[0] * blockSize;
var y = position[1] * blockSize;
ctx.fillRect(x, y, blockSize, blockSize);
}
function drawSnake(ctx, snake) {
ctx.fillStyle = "green";
snake.body.forEach(function (block) {
drawBlock(ctx, block);
})
}
function drawApple(ctx, apple) {
ctx.fillStyle = "red";
drawBlock(ctx, apple.position);
}
function drawObstacle(ctx, obstacle) {
ctx.fillStyle = "gray";
obstacle.position.forEach(function (block) {
drawBlock(ctx, block);
})
}
function moveSnake(snake) {
var head = snake.body[0];
if (snake.direction === "right") {
var newHead = [head[0] + 1, head[1]];
} else if (snake.direction === "left") {
var newHead = [head[0] - 1, head[1]];
} else if (snake.direction === "up") {
var newHead = [head[0], head[1] - 1];
} else if (snake.direction === "down") {
var newHead = [head[0], head[1] + 1];
}
snake.body.unshift(newHead);
if (newHead[0] === apple.position[0] && newHead[1] === apple.position[1]) {
apple.position = [Math.floor(Math.random() * width), Math.floor(Math.random() * height)];
score++;
} else {
snake.body.pop();
}
}
function collision(snake, obstacle) {
var head = snake.body[0];
if (head[0] < 0 || head[0] >= width || head[1] < 0 || head[1] >= height) {
return true;
}
for (var i = 1; i < snake.body.length; i++) {
if (snake.body[i][0] === head[0] && snake.body[i][1] === head[1]) {
return true;
}
}
for (var i = 0; i < obstacle.position.length; i++) {
if (head[0] === obstacle.position[i][0] && head[1] === obstacle.position[i][1]) {
return true;
}
}
return false;
}
function updateScore(score) {
document.getElementById("score").innerHTML = "Score: " + score;
}
document.addEventListener("keydown", function (event) {
if (event.keyCode === 37 && snake.direction !== "right") {
snake.direction = "left";
} else if (event.keyCode === 38 && snake.direction !== "down") {
snake.direction = "up";
} else if (event.keyCode === 39 && snake.direction !== "left") {
snake.direction = "right";
} else if (event.keyCode === 40 && snake.direction !== "up") {
snake.direction = "down";
}
});
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawObstacle(ctx, obstacle);
drawSnake(ctx, snake);
drawApple(ctx, apple);
moveSnake(snake);
updateScore(score);
if (collision(snake, obstacle)) {
clearInterval(interval);
alert("Game over!");
}
}
var interval = setInterval(gameLoop, 100);
```