|
|
html
Copy Code
<!DOCTYPE html>
<html>
<head>
<title>彩色贪吃蛇</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background-color: #222;
color: white;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #444;
margin-top: 20px;
}
.score {
font-size: 24px;
margin: 10px 0;
}
.game-over {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
padding: 20px;
border-radius: 10px;
text-align: center;
display: none;
}
button {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>彩色贪吃蛇游戏</h1>
<div class="score">得分: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="game-over">
<h2>游戏结束!</h2>
<p>最终得分: <span id="final-score">0</span></p>
<button id="restart-btn">重新开始</button>
<button id="quit-btn">退出</button>
</div>
<script src="game.js"></script>
</body>
</html>
JavaScript部分 (game.js)
javascript
Copy Code
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.querySelector('.score');
const gameOverDisplay = document.querySelector('.game-over');
const finalScoreDisplay = document.getElementById('final-score');
const restartBtn = document.getElementById('restart-btn');
const quitBtn = document.getElementById('quit-btn');
// 游戏参数
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let speed = 7;
// 蛇和食物
let snake = [
{x: 10, y: 10}
];
let food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
let xVelocity = 0;
let yVelocity = 0;
let score = 0;
// 蛇身颜色数组
const snakeColors = [
'#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33F3'
];
// 游戏状态
let gameRunning = true;
// 控制方向
document.addEventListener('keydown', changeDirection);
function changeDirection(e) {
// 防止180度转向
if (e.key === 'ArrowUp' && yVelocity !== 1) {
xVelocity = 0;
yVelocity = -1;
} else if (e.key === 'ArrowDown' && yVelocity !== -1) {
xVelocity = 0;
yVelocity = 1;
} else if (e.key === 'ArrowLeft' && xVelocity !== 1) {
xVelocity = -1;
yVelocity = 0;
} else if (e.key === 'ArrowRight' && xVelocity !== -1) {
xVelocity = 1;
yVelocity = 0;
}
}
// 游戏主循环
function gameLoop() {
if (gameRunning) {
setTimeout(() => {
clearCanvas();
moveSnake();
drawFood();
drawSnake();
checkCollision();
gameLoop();
}, 1000 / speed);
}
}
function clearCanvas() {
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawSnake() {
snake.forEach((segment, index) => {
ctx.fillStyle = snakeColors[index % snakeColors.length];
HTML部分 (index.html)
<!DOCTYPE html> <html> <head> <title>彩色贪吃蛇</title> <style> body { display: flex; flex-direction: column; align-items: center; background-color: #222; color: white; font-family: Arial, sans-serif; } canvas { border: 2px solid #444; margin-top: 20px; } .score { font-size: 24px; margin: 10px 0; } .game-over { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(0, 0, 0, 0.8); padding: 20px; border-radius: 10px; text-align: center; display: none; } button { padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; background-color: #4CAF50; color: white; border: none; border-radius: 5px; } </style> </head> <body> <h1>彩色贪吃蛇游戏</h1> <div class="score">得分: 0</div> <canvas id="gameCanvas" width="400" height="400"></canvas> <div class="game-over"> <h2>游戏结束!</h2> <p>最终得分: <span id="final-score">0</span></p> <button id="restart-btn">重新开始</button> <button id="quit-btn">退出</button> </div>
text
Copy Code
<script src="game.js"></script>
</body> </html>
JavaScript部分 (game.js)
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.querySelector('.score');
const gameOverDisplay = document.querySelector('.game-over');
const finalScoreDisplay = document.getElementById('final-score');
const restartBtn = document.getElementById('restart-btn');
const quitBtn = document.getElementById('quit-btn');
// 游戏参数
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let speed = 7;
// 蛇和食物
let snake = [
{x: 10, y: 10}
];
let food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
let xVelocity = 0;
let yVelocity = 0;
let score = 0;
// 蛇身颜色数组
const snakeColors = [
'#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33F3'
];
// 游戏状态
let gameRunning = true;
// 控制方向
document.addEventListener('keydown', changeDirection);
function changeDirection(e) {
// 防止180度转向
if (e.key === 'ArrowUp' && yVelocity !== 1) {
xVelocity = 0;
yVelocity = -1;
} else if (e.key === 'ArrowDown' && yVelocity !== -1) {
xVelocity = 0;
yVelocity = 1;
} else if (e.key === 'ArrowLeft' && xVelocity !== 1) {
xVelocity = -1;
yVelocity = 0;
} else if (e.key === 'ArrowRight' && xVelocity !== -1) {
xVelocity = 1;
yVelocity = 0;
}
}
// 游戏主循环
function gameLoop() {
if (gameRunning) {
setTimeout(() => {
clearCanvas();
moveSnake();
drawFood();
drawSnake();
checkCollision();
gameLoop();
}, 1000 / speed);
}
}
function clearCanvas() {
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawSnake() {
snake.forEach((segment, index) => {
ctx.fillStyle = snakeColors[index % snakeColors.length];
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize-1, gridSize-1);
});
}
function drawFood() {
ctx.fillStyle = '#4CAF50';
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize-1, gridSize-1);
}
function moveSnake() {
const head = {
x: snake[0].x + xVelocity,
y: snake[0].y + yVelocity
};
text
Copy Code
snake.unshift(head);
// 检查是否吃到食物
if (head.x === food.x && head.y === food.y) {
score++;
scoreDisplay.textContent = `得分: ${score}`;
generateFood();
} else {
snake.pop();
}
}
function generateFood() {
food = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
text
Copy Code
// 确保食物不会生成在蛇身上
for (let segment of snake) {
if (segment.x === food.x && segment.y === food.y) {
generateFood();
return;
}
}
}
function checkCollision() {
const head = snake[0];
text
Copy Code
// 检查墙壁碰撞
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver();
}
// 检查自身碰撞
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
function gameOver() {
gameRunning = false;
finalScoreDisplay.textContent = score;
gameOverDisplay.style.display = 'block';
}
// 按钮事件
restartBtn.addEventListener('click', () => {
// 重置游戏状态
snake = [{x: 10, y: 10}];
xVelocity = 0;
yVelocity = 0;
score = 0;
scoreDisplay.textContent = 得分: ${score};
generateFood();
gameOverDisplay.style.display = 'none';
gameRunning = true;
gameLoop();
});
quitBtn.addEventListener('click', () => {
gameOverDisplay.style.display = 'none';
});
// 开始游戏
gameLoop();
|
|