一、游戏截图

在这里插入图片描述

二、源码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>离谱贪吃蛇</title><style>body {margin: 0;padding: 20px;font-family: 'Arial', sans-serif;background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);min-height: 100vh;display: flex;align-items: center;justify-content: center;}.game-container {background: rgba(255, 255, 255, 0.1);backdrop-filter: blur(10px);border-radius: 20px;padding: 25px;box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);border: 1px solid rgba(255, 255, 255, 0.2);text-align: center;max-width: 900px;width: 100%;}.game-title {color: white;font-size: 2.8em;margin-bottom: 15px;text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1);-webkit-background-clip: text;-webkit-text-fill-color: transparent;background-clip: text;}.game-header {display: flex;justify-content: space-between;align-items: center;margin-bottom: 20px;flex-wrap: wrap;gap: 15px;}.game-info {display: flex;gap: 20px;background: rgba(0, 0, 0, 0.2);padding: 12px 20px;border-radius: 15px;color: white;font-weight: bold;font-size: 1.1em;}.info-item {display: flex;align-items: center;gap: 5px;}.grid-container {display: flex;justify-content: center;margin: 20px 0;}.grid {display: grid;grid-template-columns: repeat(20, 25px);grid-template-rows: repeat(15, 25px);gap: 1px;background-color: rgba(0, 0, 0, 0.3);border-radius: 10px;padding: 15px;box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);}.cell {width: 25px;height: 25px;display: flex;align-items: center;justify-content: center;font-size: 14px;border-radius: 4px;background-color: rgba(0, 0, 0, 0.2);transition: all 0.1s ease;}.snake-head {background: linear-gradient(135deg, #4ade80, #22c55e);color: white;font-weight: bold;box-shadow: 0 0 10px rgba(74, 222, 128, 0.5);}.snake-body {background: linear-gradient(135deg, #22c55e, #16a34a);color: white;}.food {background: linear-gradient(135deg, #ef4444, #dc2626);color: white;animation: pulse 1s infinite;}.power-up {background: linear-gradient(135deg, #f59e0b, #d97706);color: white;animation: spin 2s infinite linear;}.special-food {background: linear-gradient(135deg, #fbbf24, #f59e0b);color: white;animation: bounce 1s infinite;}@keyframes pulse {0%, 100% { transform: scale(1); }50% { transform: scale(1.1); }}@keyframes spin {from { transform: rotate(0deg); }to { transform: rotate(360deg); }}@keyframes bounce {0%, 100% { transform: translateY(0); }50% { transform: translateY(-5px); }}.controls {margin: 25px 0;display: flex;justify-content: center;gap: 15px;flex-wrap: wrap;}button {padding: 12px 25px;border: none;border-radius: 25px;background: rgba(255, 255, 255, 0.2);color: white;font-weight: bold;cursor: pointer;transition: all 0.3s;backdrop-filter: blur(5px);box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);font-size: 1em;}button:hover {background: rgba(255, 255, 255, 0.3);transform: translateY(-2px);}.instructions {color: white;margin: 20px 0;font-size: 0.95em;line-height: 1.6;background: rgba(0, 0, 0, 0.2);padding: 20px;border-radius: 15px;}.rules-section {margin: 15px 0;}.rules-section h3 {margin-top: 0;margin-bottom: 10px;color: #4ecdc4;}.power-ups-list, .special-foods-list {display: flex;justify-content: center;gap: 15px;margin: 10px 0;flex-wrap: wrap;}.item {display: flex;align-items: center;gap: 5px;background: rgba(255, 255, 255, 0.1);padding: 8px 15px;border-radius: 20px;font-size: 0.85em;}.game-over {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background: rgba(0, 0, 0, 0.9);color: white;padding: 40px;border-radius: 20px;text-align: center;z-index: 1000;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);max-width: 400px;width: 90%;}.pause-screen {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background: rgba(0, 0, 0, 0.8);color: white;padding: 40px;border-radius: 20px;text-align: center;z-index: 1000;}.combo-display {background: rgba(255, 215, 0, 0.3);color: gold;padding: 8px 15px;border-radius: 20px;font-weight: bold;margin: 5px 0;animation: glow 0.5s infinite alternate;}@keyframes glow {from { box-shadow: 0 0 5px gold; }to { box-shadow: 0 0 20px gold; }}.active-power {animation: pulse 0.5s infinite;}@media (max-width: 768px) {.game-container {padding: 15px;}.game-title {font-size: 2em;}.game-info {flex-direction: column;gap: 10px;}.grid {grid-template-columns: repeat(20, 20px);grid-template-rows: repeat(15, 20px);padding: 10px;}.cell {width: 20px;height: 20px;font-size: 12px;}.controls {flex-direction: column;align-items: center;}button {width: 100%;max-width: 200px;}}</style>
</head>
<body><div class="game-container"><h1 class="game-title">🎮 离谱贪吃蛇 🎮</h1><div class="game-header"><div class="game-info"><div class="info-item"><span>🏆</span><span>分数: <span id="score">0</span></span></div><div class="info-item"><span>❤️</span><span>生命: <span id="lives">3</span></span></div><div class="info-item"><span></span><span>等级: <span id="level">1</span></span></div><div class="info-item"><span>🔥</span><span>连击: <span id="combo" class="combo-display">0</span></span></div></div></div><div class="grid-container"><div id="game-grid" class="grid"></div></div><div class="controls"><button id="start-btn">▶️ 开始游戏</button><button id="pause-btn">⏸️ 暂停</button><button id="reset-btn">🔄 重置游戏</button></div><div class="instructions"><div class="rules-section"><h3>🎮 游戏规则:</h3><p>🎯 目标:吃到食物获得分数,避开自身碰撞</p><p>⚡ 特殊道具:⚡❄️🌱🔻🌀✨ - 每个都有独特效果</p><p>🌟 特殊食物:⭐❤️💎☁️ - 获得额外奖励</p><p>💥 连击系统:连续吃到5个食物获得额外50分奖励!</p></div><div class="rules-section"><h3>⚡ 道具效果:</h3><div class="power-ups-list"><div class="item">⚡ 速度加速 (5秒) - 快速移动</div><div class="item">❄️ 减速道具 - 慢速移动</div><div class="item">🌱 增长道具 - 蛇身增长</div><div class="item">🔻 缩小道具 - 蛇身缩短</div><div class="item">🌀 传送道具 - 随机传送</div><div class="item">✨ 无敌道具 - 5秒无敌</div></div></div><div class="rules-section"><h3>🌟 特殊食物效果:</h3><div class="special-foods-list"><div class="item">⭐ 超级星星 (+50分) - 大量分数</div><div class="item">❤️ 爱心生命 (+1生命) - 生命恢复</div><div class="item">💎 钻石宝石 (+50分) - 高分奖励</div><div class="item">☁️ 云朵祝福 (+50分) - 额外分数</div></div></div><div class="rules-section"><h3>🎮 操作说明:</h3><p>🎮 方向键控制方向 | ⚡ 空格键暂停 | 🔄 R键重置</p></div></div></div><script>// 游戏变量let snake = [{x: 10, y: 10}];let direction = {x: 0, y: -1};let food = {x: 15, y: 15};let powerUps = [];let specialFoods = [];let gameRunning = false;let gameOver = false;let score = 0;let level = 1;let lives = 3;let activePowerUps = [];let gameSpeed = 150;let gameLoop;let combo = 0;let isPaused = false;let invincible = false;// DOM元素const gameGrid = document.getElementById('game-grid');const scoreElement = document.getElementById('score');const livesElement = document.getElementById('lives');const levelElement = document.getElementById('level');const comboElement = document.getElementById('combo');const startBtn = document.getElementById('start-btn');const pauseBtn = document.getElementById('pause-btn');const resetBtn = document.getElementById('reset-btn');// 生成随机位置function generateRandomPosition() {return {x: Math.floor(Math.random() * 20),y: Math.floor(Math.random() * 15)};}// 检查碰撞function checkCollision(head, snakeBody) {if (invincible) return false;return snakeBody.slice(1).some(segment => segment.x === head.x && segment.y === head.y);}// 生成食物function generateFood() {let newFood;let valid = false;while (!valid) {newFood = generateRandomPosition();valid = !snake.some(segment => segment.x === newFood.x && segment.y === newFood.y) &&!powerUps.some(power => power.x === newFood.x && power.y === newFood.y) &&!specialFoods.some(food => food.x === newFood.x && food.y === newFood.y);}return newFood;}// 生成道具function generatePowerUp() {const types = ['speed', 'slow', 'grow', 'shrink', 'teleport', 'invincible'];const type = types[Math.floor(Math.random() * types.length)];return {...generateRandomPosition(),type: type,id: Date.now() + Math.random()};}// 生成特殊食物function generateSpecialFood() {const types = ['star', 'heart', 'diamond', 'cloud'];const type = types[Math.floor(Math.random() * types.length)];return {...generateRandomPosition(),type: type,id: Date.now() + Math.random()};}// 渲染游戏网格function renderGrid() {gameGrid.innerHTML = '';for (let y = 0; y < 15; y++) {for (let x = 0; x < 20; x++) {const cell = document.createElement('div');cell.className = 'cell';const isSnakeHead = snake[0].x === x && snake[0].y === y;const isSnakeBody = snake.slice(1).some(segment => segment.x === x && segment.y === y);const isFood = food.x === x && food.y === y;const isPowerUp = powerUps.some(power => power.x === x && power.y === y);const isSpecialFood = specialFoods.some(food => food.x === x && food.y === y);if (isSnakeHead) {cell.classList.add('snake-head');cell.textContent = '🐍';} else if (isSnakeBody) {cell.classList.add('snake-body');cell.textContent = '🟢';} else if (isFood) {cell.classList.add('food');cell.textContent = '🍎';} else if (isPowerUp) {cell.classList.add('power-up');const icons = {'speed': '⚡', 'slow': '❄️', 'grow': '🌱', 'shrink': '🔻', 'teleport': '🌀', 'invincible': '✨'};cell.textContent = icons[powerUps.find(p => p.x === x && p.y === y).type];} else if (isSpecialFood) {cell.classList.add('special-food');const icons = {'star': '⭐', 'heart': '❤️', 'diamond': '💎', 'cloud': '☁️'};cell.textContent = icons[specialFoods.find(f => f.x === x && f.y === y).type];}gameGrid.appendChild(cell);}}}// 更新游戏状态function updateGame() {if (!gameRunning || gameOver || isPaused) return;// 移动蛇const newSnake = [...snake];const head = {...newSnake[0]};head.x += direction.x;head.y += direction.y;// 边界处理(可穿越)if (head.x < 0) head.x = 19;if (head.x > 19) head.x = 0;if (head.y < 0) head.y = 14;if (head.y > 14) head.y = 0;// 检查碰撞if (checkCollision(head, newSnake)) {lives--;livesElement.textContent = lives;if (lives <= 0) {gameOver = true;gameRunning = false;showGameOver();return;}// 重置蛇的位置snake = [{x: 10, y: 10}];direction = {x: 0, y: -1};invincible = false;return;}newSnake.unshift(head);// 检查是否吃到食物if (head.x === food.x && head.y === food.y) {score += 10;combo++;comboElement.textContent = combo;scoreElement.textContent = score;if (combo >= 5) {score += 50;scoreElement.textContent = score;combo = 0;comboElement.textContent = combo;comboElement.style.animation = 'glow 0.5s infinite alternate';setTimeout(() => comboElement.style.animation = '', 2000);}food = generateFood();} else {newSnake.pop();}// 检查是否吃到道具powerUps = powerUps.filter(power => {if (power.x === head.x && power.y === head.y) {// 应用道具效果switch (power.type) {case 'speed':gameSpeed = 100;setTimeout(() => gameSpeed = 150, 5000);break;case 'grow':newSnake.push(...Array(2).fill().map(() => ({...newSnake[newSnake.length - 1]})));break;case 'shrink':if (newSnake.length > 3) {newSnake.splice(-2, 2);}break;case 'teleport':const teleportPos = generateRandomPosition();head.x = teleportPos.x;head.y = teleportPos.y;break;case 'invincible':invincible = true;setTimeout(() => invincible = false, 3000);break;}return false; // 移除道具}return true;});// 检查是否吃到特殊食物specialFoods = specialFoods.filter(foodItem => {if (foodItem.x === head.x && foodItem.y === head.y) {switch (foodItem.type) {case 'star':score += 50;scoreElement.textContent = score;break;case 'heart':lives++;livesElement.textContent = lives;break;case 'diamond':score += 50;scoreElement.textContent = score;break;case 'cloud':score += 50;scoreElement.textContent = score;break;}return false; // 移除食物}return true;});snake = newSnake;// 随机生成道具和特殊食物if (Math.random() < 0.02) {powerUps.push(generatePowerUp());}if (Math.random() < 0.01) {specialFoods.push(generateSpecialFood());}renderGrid();}// 显示游戏结束function showGameOver() {const gameOverDiv = document.createElement('div');gameOverDiv.className = 'game-over';gameOverDiv.innerHTML = `<h2>🎮 游戏结束! 🎮</h2><p>最终得分: ${score}</p><p>最高连击: ${Math.max(combo, 5)}</p><button onclick="this.parentElement.remove(); resetGame()" style="margin-top: 15px; padding: 12px 25px; background: #ff6b6b; border: none; border-radius: 25px; color: white; font-weight: bold; cursor: pointer;">再玩一次!</button>`;document.body.appendChild(gameOverDiv);}// 显示暂停界面function showPauseScreen() {const pauseDiv = document.createElement('div');pauseDiv.className = 'pause-screen';pauseDiv.innerHTML = `<h2>⏸️ 暂停中 ⏸️</h2><p>按空格键继续游戏</p>`;document.body.appendChild(pauseDiv);return pauseDiv;}// 开始游戏function startGame() {if (gameRunning) return;gameRunning = true;gameOver = false;isPaused = false;score = 0;lives = 3;level = 1;combo = 0;snake = [{x: 10, y: 10}];direction = {x: 0, y: -1};food = generateFood();powerUps = [];specialFoods = [];gameSpeed = 150;invincible = false;scoreElement.textContent = score;livesElement.textContent = lives;levelElement.textContent = level;comboElement.textContent = combo;renderGrid();if (gameLoop) clearInterval(gameLoop);gameLoop = setInterval(updateGame, gameSpeed);}// 暂停游戏function togglePause() {if (!gameRunning || gameOver) return;isPaused = !isPaused;pauseBtn.textContent = isPaused ? '▶️ 继续' : '⏸️ 暂停';if (isPaused) {showPauseScreen();} else {const pauseDiv = document.querySelector('.pause-screen');if (pauseDiv) pauseDiv.remove();}}// 重置游戏function resetGame() {if (gameLoop) clearInterval(gameLoop);gameRunning = false;gameOver = false;isPaused = false;const pauseDiv = document.querySelector('.pause-screen');if (pauseDiv) pauseDiv.remove();startGame();}// 键盘控制document.addEventListener('keydown', (e) => {if (!gameRunning || gameOver) return;switch (e.key) {case 'ArrowUp':if (direction.y === 0) direction = {x: 0, y: -1};break;case 'ArrowDown':if (direction.y === 0) direction = {x: 0, y: 1};break;case 'ArrowLeft':if (direction.x === 0) direction = {x: -1, y: 0};break;case 'ArrowRight':if (direction.x === 0) direction = {x: 1, y: 0};break;case ' ':togglePause();break;}});// 按钮事件startBtn.addEventListener('click', startGame);pauseBtn.addEventListener('click', togglePause);resetBtn.addEventListener('click', resetGame);// 初始化游戏renderGrid();</script>
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/94662.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/94662.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/94662.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

InnoDB详解2

InnoDB详解2一.行结构1.结构图2.InnoDB支持的数据行格式1&#xff09;查看当前数据库或表的行格式2&#xff09;指定行格式3&#xff09;DYNAMIC 格式的组成3.数据区存储真实数据方式4.行的额外(管理)信息区5.头信息区域1&#xff09;删除一行记录时在InnoDB内部执行的操作6.Nu…

Rust系统编程实战:驾驭内存安全、无畏并发与WASM跨平台开发

简介本文深入探讨Rust在系统编程领域的核心实战应用&#xff0c;通过代码示例解析其所有权机制如何保障内存安全&#xff0c;如何利用 fearless concurrency 构建高性能并发应用&#xff0c;并实践如何将Rust代码编译为WebAssembly&#xff08;WASM&#xff09;以突破性能瓶颈。…

JavaScript 基础入门:从概念解析到流程控制

文章目录1. JavaScript 核心认知1.1 浏览器与 JavaScript 的关系1.2 JavaScript 的三大核心组成1.3 JavaScript 引入1.3.1 内联脚本&#xff08;事件属性绑定&#xff09;1.3.2 内部脚本&#xff08;<script> 标签嵌入&#xff09;1.3.3 外部脚本&#xff08;独立 .js 文…

WebSocket简单了解

WebSocket 是一种计算机网络通信协议&#xff0c;它在客户端和服务器之间建立一个持久的、双向的通信通道。与传统的 HTTP 请求-响应模型不同&#xff0c;WebSocket 允许数据在客户端和服务器之间实时双向传输&#xff0c;因此非常适合需要即时交互的应用&#xff0c;如实时聊天…

【实时Linux实战系列】基于实时Linux的生物识别系统

在当今数字化时代&#xff0c;生物识别技术因其高安全性和便捷性而被广泛应用。生物识别系统通过识别个人的生物特征&#xff08;如面部、指纹等&#xff09;来验证身份&#xff0c;广泛应用于安全门禁、移动支付、智能设备解锁等领域。这些系统不仅提高了安全性&#xff0c;还…

汇智焕彩,聚势创新 - openKylin 2.0 SP2正式发布!

OpenAtom openKylin&#xff08;简称 “openKylin”&#xff09; 2.0 SP2版本正式发布&#xff01;本次版本更新在底层核心能力上&#xff0c;持续维护 6.6 稳定版内核&#xff0c;深度适配海光、飞腾、兆芯、龙芯等国产主流芯片&#xff0c;并积极推动 RISC-V 开放指令集架构生…

怎么评估高精度组合惯导的惯性导航价格?

内容概要高精度组合惯导系统的价格评估是一个需要综合考量多个关键因素的复杂过程。理解其成本构成&#xff0c;对于制定合理的采购预算和优化决策至关重要。评估的核心首先聚焦于IMU传感器价格&#xff0c;这是整个系统成本中最主要的组成部分之一。同时&#xff0c;选择可靠且…

深度学习开篇

首先我们要知道深度学习和机器学习的关系——深度学习(DL, Deep Learning)是机器学习(ML, Machine Learning)领域中一个新的研究方向。 深度学习简介 我理解的深度学习就通过多层感知器&#xff0c;对数据进行训练&#xff0c;可以达到非线性变换&#xff0c;如何可以提取非线性…

Typescript入门-interface讲解

对象成员语法形式1&#xff09;对象属性2&#xff09;对象的属性索引3&#xff09;对象的方法4&#xff09;函数5&#xff09;构造函数interface 的继承interface 继承 interfaceinterface 继承 typeinterface 继承 class接口合并interface 与 type 的异同interface 是对象的模…

数据结构青铜到王者第五话---LinkedList与链表(2)

目录 一、常见的链表题目练习&#xff08;续&#xff09; 1、链表的回文结构。 2、输入两个链表&#xff0c;找出它们的第一个公共结点。 3、给定一个链表&#xff0c;判断链表中是否有环。 4、给定一个链表&#xff0c;返回链表开始入环的第一个节点。 如果链表无环&#…

Kafa面试经典题--Kafka为什么吞吐量大,速度快

这是一个非常核心的面试题和技术问题。Kafka 的高吞吐量和速度并非来自某一项“银弹”技术,而是其架构设计中一系列精巧决策共同作用的结果。 一、核心思想:最大化利用底层硬件资源 Kafka 速度快的根本原因是,它的设计哲学是 “尽可能地避免不必要的开销,并将硬件(尤其是…

Stream API 新玩法:从 teeing()到 mapMulti()

1. 背景&#xff1a;Stream API 的演进 自 Java 8 引入 Stream API 以来&#xff0c;Java 的集合处理方式发生了质变。开发者可以用声明式风格实现复杂的数据转换与聚合。然而&#xff0c;随着应用场景多样化&#xff0c;社区逐渐发现一些“尴尬空缺”&#xff1a; 聚合时&…

STM32G4 SVPWM VF开环强拖电机

目录一、STM32G4 SVPWM VF开环强拖电机1 SVPWM1.1 SVPWM技术简介1.2 基于零序分量注入的SVPWM算法的实现2. VF开环强拖电机3. VF启动电机实验现象附学习参考网址欢迎大家有问题评论交流 (* ^ ω ^)一、STM32G4 SVPWM VF开环强拖电机 1 SVPWM 1.1 SVPWM技术简介 SVPWM控制策略…

产品运营必备职场通用能力及提升攻略,一文说明白

在互联网行业蓬勃发展的当下&#xff0c;产品运营岗位成为了连接产品、用户与商业目标的关键纽带。从用户增长到活动策划&#xff0c;从数据分析到跨部门协作&#xff0c;产品运营人员需具备多元化技能&#xff0c;才能在激烈竞争中崭露头角。随着企业对精细化运营与数据驱动决…

面试 总结(1)

面试总结 一、spring相关 1. Spring Security角色管理实现 在智慧种植虫害识别系统中&#xff0c;我实现了农户端和企业端的双角色权限控制&#xff0c;这一部分是这样实现的&#xff1a; MySQL 表时设计区分农户和企业的角色表与权限表。登录时&#xff0c;JWT 令牌包含用户 I…

串与数组:从字符处理到多维存储的数据结构详解

串&#xff08;字符串&#xff09;和数组是数据结构中的两个重要分支&#xff0c;它们在程序设计中承担着不同但互补的角色。串专门处理字符数据&#xff0c;而数组则提供了多维数据的存储和访问机制。本文将深入探讨这两种数据结构的理论基础、实现方法和核心算法。 文章目录1…

面试之JVM

类的生命周期 加载、链接、初始化&#xff08;是类的初始化&#xff09;、使用&#xff08;对象的初始化&#xff09;、卸载&#xff08;GC&#xff09; 链接&#xff1a;验证、准备、解析 类加载 JDK9的升级点&#xff1a;扩展类加载器改成了平台类加载器。 java中很多的包分…

webpack开发模式与生产模式(webpack --mode=development/production“, )

webpack开发模式与生产模式的区别webpack的development&#xff08;开发模式&#xff09;和production&#xff08;生产模式&#xff09;是两种常见的构建环境配置&#xff0c;主要区别体现在构建速度、代码优化和调试支持等方面。开发模式 (development)目标&#xff1a;注重开…

当自然语言遇上数据库:Text2Sql.Net的MCP革命如何重新定义开发者与数据的交互方式

想象一下&#xff0c;在IDE中对AI助手说"帮我找出本月销售额最高的前10个产品"&#xff0c;然后它不仅能理解你的意图&#xff0c;还能直接生成并执行SQL查询&#xff0c;返回准确结果——这不是科幻&#xff0c;而是Text2Sql.Net的MCP集成带来的现实。 &#x1f3af…

2025流程图模板和工具深度评测:AI如何提升绘图效率80%?

引言&#xff1a;流程图模板的价值革命 在数字化办公的浪潮中&#xff0c;流程图已从单纯的"业务说明工具"进化为跨部门协作的"视觉语言"。据智研咨询2025年报告显示&#xff0c;规范使用流程图模板可使团队沟通效率提升40%&#xff0c;错误率降低58%。无…