|
|
好哒!我帮你把游戏变得更恐怖,增加更多陷阱和更多随机元素,让游戏更刺激惊险!
主要改进:
- 陷阱数量更多,而且每次吃完食物后陷阱会重新随机刷新。
- 增加迷雾效果,让视野变窄,看不到全局。
- 恐怖元素多个出现,速度波动更大更不稳定。
- 随机出现黑暗闪烁(画面瞬间变黑再恢复),增加惊悚感。
- 加一些恐怖文字提示。
你把下面完整代码复制到浏览器打开试试吧:
恐怖贪吃蛇加强版
body {
background-color: black;
color: white;
text-align: center;
font-family: '微软雅黑', sans-serif;
}
#gameCanvas {
background-color: #111;
display: block;
margin: 20px auto;
border: 3px solid darkred;
filter: brightness(0.6);
position: relative;
}
#status {
margin-top: 10px;
font-size: 18px;
color: red;
font-weight: bold;
height: 24px;
}
#fog {
position: absolute;
top: 0; left: 0;
width: 400px; height: 400px;
pointer-events: none;
}
恐怖贪吃蛇加强版????️????️
用方向键控制蛇移动。
红色陷阱碰了游戏结束。
绿色食物吃了变长。
紫色恐怖元素吃了让蛇速度更快或更慢,不可预测!
黑色迷雾让视野很有限,随机闪烁黑暗增加恐怖气氛!
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const fogCanvas = document.createElement('canvas');
fogCanvas.width = canvas.width;
fogCanvas.height = canvas.height;
const fogCtx = fogCanvas.getContext('2d');
document.getElementById('gameContainer').appendChild(fogCanvas);
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [];
let direction = {x:0, y:0};
let food = null;
let traps = [];
let horrors = [];
let speed = 200;
let gameInterval;
let score = 0;
let gameOver = false;
const statusDiv = document.getElementById('status');
// 生成随机坐标,不与蛇体、食物、陷阱和恐怖元素重叠
function randomPosition() {
let pos;
do {
pos = {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
} while(
snake.some(s => s.x === pos.x && s.y === pos.y) ||
(food && food.x === pos.x && food.y === pos.y) ||
traps.some(t => t.x === pos.x && t.y === pos.y) ||
horrors.some(h => h.x === pos.x && h.y === pos.y)
);
return pos;
}
function generateFood() {
food = randomPosition();
}
function generateTraps(count=10) {
traps = [];
for(let i=0;i<count;i++) {
traps.push(randomPosition());
}
}
function generateHorrors(count=3) {
horrors = [];
for(let i=0;i<count;i++) {
horrors.push(randomPosition());
}
}
// 黑暗闪烁特效
function flickerDarkness(){
let flickerTimes = 5;
let count = 0;
let flickerInterval = setInterval(() => {
if(count % 2 === 0){
ctx.fillStyle = 'rgba(0,0,0,0.8)';
ctx.fillRect(0,0,canvas.width,canvas.height);
} else {
drawGame();
}
count++;
if(count > flickerTimes*2){
clearInterval(flickerInterval);
}
},100);
}
// 迷雾效果(只显示蛇头附近区域)
function drawFog(){
fogCtx.clearRect(0,0,fogCanvas.width,fogCanvas.height);
fogCtx.fillStyle = 'rgba(0,0,0,0.95)';
fogCtx.fillRect(0,0,fogCanvas.width,fogCanvas.height);
if(snake.length === 0) return;
const head = snake[0];
const radius = 60;
let centerX = head.x * gridSize + gridSize/2;
let centerY = head.y * gridSize + gridSize/2;
let gradient = fogCtx.createRadialGradient(centerX, centerY, radius/4, centerX, centerY, radius);
gradient.addColorStop(0, 'rgba(0,0,0,0)');
gradient.addColorStop(1, 'rgba(0,0,0,0.95)');
fogCtx.globalCompositeOperation = 'destination-out';
fogCtx.fillStyle = gradient;
fogCtx.beginPath();
fogCtx.arc(centerX, centerY, radius, 0, Math.PI*2);
fogCtx.fill();
fogCtx.globalCompositeOperation = 'source-over';
}
function drawBlock(x,y,color) {
ctx.fillStyle = color;
ctx.fillRect(x*gridSize, y*gridSize, gridSize-1, gridSize-1);
}
function drawGame() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 画食物
if(food) drawBlock(food.x, food.y, 'limegreen');
// 画陷阱
for(let t of traps) drawBlock(t.x, t.y, 'red');
// 画恐怖元素
for(let h of horrors) drawBlock(h.x, h.y, 'purple');
// 画蛇身
for(let i=0; i< snake.length; i++) {
let col = i===0 ? 'white' : 'gray';
drawBlock(snake.x, snake.y, col);
}
drawFog();
}
function endGame() {
clearInterval(gameInterval);
gameOver = true;
statusDiv.innerText = `游戏结束!最终得分: ${score}. 带着恐惧离开吧...`;
}
function gameLoop() {
if(gameOver) return;
if(direction.x === 0 && direction.y === 0) return; // 游戏开始前不动
let head = snake[0];
let newX = head.x + direction.x;
let newY = head.y + direction.y;
// 撞墙
if(newX < 0 || newX >= tileCount || newY <0 || newY >= tileCount){
endGame();
return;
}
// 撞自己
if(snake.some(s => s.x === newX && s.y === newY)){
endGame();
return;
}
// 新蛇头加入
snake.unshift({x:newX, y:newY});
// 吃食物
if(food && newX === food.x && newY === food.y){
score++;
generateFood();
generateTraps(10 + Math.floor(score/2)); // 陷阱越来越多
generateHorrors(3 + Math.floor(score/3)); // 恐怖元素越来越多
statusDiv.innerText = `幸存者,你击败了一部分陷阱!当前分数:${score}`;
// 每次吃完食物,随机触发恐怖闪烁
if(Math.random() < 0.5) {
flickerDarkness();
}
} else {
// 没吃食物,尾巴缩短
snake.pop();
}
// 碰陷阱
if(traps.some(t => t.x === newX && t.y === newY)){
endGame();
return;
}
// 吃恐怖元素,速度可能变快或变慢,很随机,增添恐怖感
for(let i=0;i<horrors.length;i++){
let h = horrors;
if(h.x === newX && h.y === newY){
let oldSpeed = speed;
if(Math.random() < 0.5){
speed = Math.max(50, speed - 40); // 加速
statusDiv.innerText = `???你感到心跳加速!!!速度提高啦!`;
} else {
speed = Math.min(500, speed + 60); // 减速
statusDiv.innerText = `...脚步变得沉重,速度降低...`;
}
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
horrors.splice(i,1);
break;
}
}
drawGame();
}
window.addEventListener('keydown', e => {
if(e.key === 'ArrowUp' && direction.y !== 1) {
direction = {x:0, y:-1};
} else if(e.key === 'ArrowDown' && direction.y !== -1) {
direction = {x:0, y:1};
} else if(e.key === 'ArrowLeft' && direction.x !== 1) {
direction = {x:-1, y:0};
} else if(e.key === 'ArrowRight' && direction.x !== -1) {
direction = {x:1, y:0};
}
// 按R重置游戏
if(e.key.toLowerCase() === 'r' && gameOver){
initGame();
}
});
function initGame() {
snake = [{x:10, y:10}];
direction = {x:0, y:0};
score = 0;
speed = 200;
gameOver = false;
generateFood();
generateTraps(10);
generateHorrors(3);
statusDiv.innerText = '游戏开始!用方向键控制蛇,R键重置。';
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
}
initGame();
### 玩法提示:
- 红色陷阱是致命的,千万别撞上!
- 紫色恐怖元素吃了会让你速度忽快忽慢,小心脚步不稳!
- 屏幕中央有一圈模糊的迷雾,只能看到蛇头附近的区域。
- 随机黑暗闪烁会突然遮住整个屏幕,吓你一跳!
- 游戏结束后按“R”键重置,继续挑战恐怖深渊!
玩的开心!需要更酷的功能随时告诉我哦~ |
|