使用Vue框架开发的贪吃蛇小游戏

article/2025/8/26 14:46:30

注意:尽量不要用谷歌浏览器运行,因为谷歌浏览器会把GameValue翻译成中文,使游戏掉帧卡顿

解决办法:1.使用Edge浏览器,2.把谷歌浏览器的自动中文翻译关闭即可

游戏截图:

源码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8" content="width=device-width,initial-scale=1.0,user-scalable=0" name="viewport"><title>贪吃蛇</title><link href="https://llh317.oss-cn-guangzhou.aliyuncs.com/bootstrap-4.6.1-dist/bootstrap-4.6.1-dist/css/bootstrap.min.css"rel="stylesheet"><script src="https://llh317.oss-cn-guangzhou.aliyuncs.com/js-package/js/vue.js"></script><style>* {user-select: none;}.mapParent {height: 500px;}.map {border: 1px red dashed;height: 100%;width: 100%;}.titleAlert {font-weight: bold;}.system {color: #909399;font-size: 12px;}.titleValue {font-size: 12px;color: #909399;margin: -10px 0;}.foot {text-align: center;}.butWASD button {font-size: 50px;width: 150px;height: 150px;}.hereto {color: #909399;margin: 0 5px;text-decoration: underline;}.description {font-size: 16px;color: #909399;}.footNote {font-size: 12px;color: #909399;}.sbody {background-color: skyblue;}.shead {background-color: red;}.food {background-color: blueviolet;}</style>
</head><body>
<div class="llh"><br><br><div @keydown.65="aKey"@keydown.68="dKey"@keydown.75="accelerate(true)"@keydown.83="sKey"@keydown.87="wKey"@keydown.down="sKey"@keydown.left="aKey"@keydown.right="dKey"@keydown.up="wKey"@keyup.74="gameState(2)"@keyup.75="accelerate(false)"class="container" id="v1"><div class="row"><div class="col-sm-3" style="text-align: center;"><button :class="{btn:true,'btn-info':buttonColorNum===0,'btn-success':buttonColorNum===1,'btn-danger':buttonColorNum===2,'btn-warning':buttonColorNum===3}" @blur="gameState(1)" @click="gameState(0)">{{buttonPrompt}}</button><br><br><h3 style="color: #303133;">得分:{{score}}</h3><h6 class="titleAlert" id="open" style="color: blue;display: none;">[ 已启动加速 ]</h6><h6 class="titleAlert" id="close" style="color: red;">[ 已关闭加速 ]</h6></div><div class="col-sm-6 mapParent"><table align="center" class="map" v-html="gameMapData"></table></div><div class="col-sm-3 system"><label class="titleValue">游戏参数调试板:</label><br><label class="titleValue">score:~~~</label>{{score}}<br><label class="titleValue">snakeLength:~~~</label>{{snakeLength}}<br><label class="titleValue">xSnake:~~~</label>{{xSnake}}<br><label class="titleValue">ySnake:~~~</label>{{ySnake}}<br><label class="titleValue">xTimerLog:~~~</label>{{xTimerLog}}<br><label class="titleValue">yTimerLog:~~~</label>{{yTimerLog}}<br><label class="titleValue">playerDirection:~~~</label>{{playerDirection}}<br><label class="titleValue">X-Y:</label>{{walkList}}</div></div><br><div><div class="foot"><div class="butWASD" v-if="butWASD"><button @click="wKey" class="btn btn-danger">上</button><br><button @click="aKey" class="btn btn-danger">左</button><button class="btn"></button><button @click="dKey" class="btn btn-danger">右</button><br><button @click="sKey" class="btn btn-danger">下</button></div><hr><div class="description">操作:( W-A-S-D:方向 )-(暂停/开始游戏:j )-( 加速:k )-(如果游戏出现停止运行,重新点击按钮即可)</div><hr><div class="footNote">Creation time 2021-10-01<a class="hereto" href="https://blog.csdn.net/L0317">©廖利辉</a><a class="hereto"href="https://blog.csdn.net/L0317/article/details/122098926?spm=1001.2014.3001.5501">®TCS</a><a class="hereto" href="https://cn.vuejs.org/index.html">™VUE.JS-version:2.0</a></div></div></div><br></div>
</div>
</body>
<script>new Vue({el: "#v1",data: {/* 按钮样式 */buttonColorNum: -1, //按钮颜色buttonPrompt: "", //按钮提示/* 绘制地图 */gameMapData: "", //地图的html数据dituSize: 100, //地图的大小/* 游戏参数(管远缘量身定做) */speed: 70, //速度speedMirroring: 0, //速度镜像score: 0, //得分snakeLength: 10, //蛇身长度/* 游戏日志 */walkList: [], //行走记录xSnake: 0, //横向ySnake: 0, //纵向xFood: 0, //食物横向yFood: 0, //食物纵向playerDirection: 0, //保存方向/* 计时器 */xTimerLog: null, //横向计时器yTimerLog: null, //纵向计时器/* 手机端按钮 */butWASD: false},methods: {accelerate(bool) {//加速this.speed = bool ? 20 : this.speedMirroringthis.getllh("open").style.display = bool ? "block" : "none"this.getllh("close").style.display = bool ? "none" : "block"},gameState(state) {//0运行 1游戏意外停止(游戏指针转移) 2暂停//处理重复手动点击if (state === 0 && this.buttonColorNum === 1) {state = 2}//处理暂停状态时按jif (state === 2 && this.buttonColorNum === 3) {state = 0}//处理停止状态时按jif (state === 2 && this.buttonColorNum === 2) {state = 0}if (state === 0) {/* 游戏开始 */this.buttonPrompt = "游戏正在运行";this.buttonColorNum = 1/* 初始化 */if (this.xSnake === 0 && this.xFood === 0) {//保持从中间出来this.ySnake = this.xSnake = Math.floor(this.dituSize / 2);this.setFood(); //随机产生豆子} else {//启动后,重新启动保持方向if (this.xTimerLog != null && this.playerDirection > 0) {this.xTimerLog = null; //设置为空,方向改变器this.dKey();} else if (this.xTimerLog != null && this.playerDirection < 0) {this.xTimerLog = null; //设置为空,方向改变器this.aKey();} else if (this.yTimerLog != null && this.playerDirection > 0) {this.yTimerLog = null; //设置为空,方向改变器this.sKey();} else if (this.yTimerLog != null && this.playerDirection < 0) {this.yTimerLog = null; //设置为空,方向改变器this.wKey();}}} else if (state === 1) {// PC端需要聚焦到开始按钮,手机端不需要if (!this.butWASD) {/* 游戏停止 */this.buttonPrompt = "游戏停止运行(继续运行)";this.buttonColorNum = 2;// 清空计时器this.xTimerLog != null ? clearInterval(this.xTimerLog) : ""this.yTimerLog != null ? clearInterval(this.yTimerLog) : ""}} else if (state === 2) {/* 游戏暂停 */this.buttonPrompt = "游戏暂停运行(开始游戏)";this.buttonColorNum = 3;//清空计时器this.xTimerLog != null ? clearInterval(this.xTimerLog) : ""this.yTimerLog != null ? clearInterval(this.yTimerLog) : ""}},wKey() {this.direction("y", -1); //y轴减少},aKey() {this.direction("x", -1); //x轴减少},sKey() {this.direction("y", 1); //y轴增多},dKey() {this.direction("x", 1); //x轴增多},direction(axis, num) {/* 方向改变器 *///游戏必须处于运行状态if (this.buttonColorNum === 1) {if (axis === "x") {/* x轴运动器 *///x轴计时器必须为空if (this.xTimerLog == null) {if (this.yTimerLog != null) this.initializeTimer("y") //初始化ythis.playerDirection = num; //保存玩家的方向this.xTimerLog = setInterval(() => {this.xSnake += num //x加1this.addWalkList(this.xSnake, this.ySnake); //保存记录this.walkStart(); //行走}, this.speed)}} else if (axis === "y") {/* y轴运动器 */if (this.yTimerLog == null) {if (this.xTimerLog) this.initializeTimer("x") //初始化xthis.playerDirection = num; //保存玩家的方向this.yTimerLog = setInterval(() => {this.ySnake += num //y加1this.addWalkList(this.xSnake, this.ySnake); //保存记录this.walkStart(); //行走}, this.speed)}}}},addWalkList(x, y) {/* 记录保存器 */function Log(xLog, yLog) { //日志对象this.xLog = xLog;this.yLog = yLog;}this.walkList[this.walkList.length] = new Log(x, y); //当前记录添加进去/* 性能优化(数组长度根据蛇的长度+1去定制) +1是为了获取被清空的那个一格*/let performance = [];if (this.walkList.length > this.snakeLength) {/* 截取蛇身长度的数值优化 */for (let i = this.walkList.length - 1 - this.snakeLength; i <= this.walkList.length -1; i++) {performance[performance.length] = this.walkList[i];}this.walkList = performance;}},walkStart() {/* 行走器 */let headObj = this.walkList[this.walkList.length - 1]; //读取玩家头记录this.sStyle(headObj.xLog, headObj.yLog, true, "shead"); //设置头let bodyObj = this.walkList[this.walkList.length - 2]; //读取玩家身体记录if (bodyObj != null) this.sStyle(bodyObj.xLog, bodyObj.yLog, true, "sbody") //设置身体let clearObj = this.walkList[this.walkList.length - 1 - this.snakeLength]; //读取清空的记录if (clearObj != null) this.sStyle(clearObj.xLog, clearObj.yLog, false) //清空身体if (headObj.xLog === this.xFood && headObj.yLog === this.yFood) { //判断头部,是否触碰了食物this.score += 1; //加一分if (this.score % 2 === 0) this.snakeLength += 1 //2个豆子一格身子this.setFood(); //随机产生食物}for (let i = 0; i <= this.walkList.length - 2; i++) { //遍历身体,判断头部是否触碰到身体if (this.walkList[i].xLog === headObj.xLog && this.walkList[i].yLog === headObj.yLog) {location.reload()alert("游戏结束")}}},setFood() {/* 食物放置器 */this.xFood = Math.floor(Math.random() * this.dituSize); //食物的x轴this.yFood = Math.floor(Math.random() * this.dituSize); //食物的y轴this.sStyle(this.xFood, this.yFood, true, "food"); //给食物添加样式},initializeTimer(axis) {/* 计时初始化器 */if (axis === "x") {if (this.xTimerLog != null) {clearInterval(this.xTimerLog)this.xTimerLog = null;}} else if (axis === "y") {if (this.yTimerLog != null) {clearInterval(this.yTimerLog)this.yTimerLog = null;}}},sStyle(x, y, bool, className) {/* 样式改变器*/try {if (bool) {/* 设置样式 */this.getllh("llh-" + x + "-" + y).className = className;} else {/* 清空样式 */this.getllh("llh-" + x + "-" + y).className = "";}} catch (exception) {location.reload()alert("游戏结束")}},getllh(name) {/* 元素获取器 */return document.getElementById(name);},isPC() {// 是否为PC端let sUserAgent = navigator.userAgent.toLowerCase();return !/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(sUserAgent);}},created() {/* 初始化地图 */console.log(`%c 贪吃蛇 %c 作者:廖利辉 %c `,'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px;  color: #fff','background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0;  color: #fff','background:transparent')this.buttonPrompt = "地图渲染中"; // 改变按钮文字let size = this.dituSize; // 获取地图的参数/* 循环y轴 */for (let y = 0; y < size; y++) {this.gameMapData += "<tr>";/* 循环x轴 */for (let x = 0; x < size; x++) {this.gameMapData += "<td id='llh-" + x + "-" + y + "'></td>";}this.gameMapData += "</tr>";}this.buttonPrompt = "地图渲染完成(开始游戏)"; // 改变按钮文字this.buttonColorNum = 0; // 改变按钮颜色this.speedMirroring = this.speed; // 将速度镜像赋值/* 为了展示手机端的上下左右按钮,PC不展示 */this.butWASD = !this.isPC() // 赋值是否为PC}})</script>
</html>


http://chatgpt.dhexx.cn/article/XrB93XHE.shtml

相关文章

QT大作业——自制小游戏

The RPGgame with QT 一、 系统架构 该项目有4个ui组成&#xff0c;其中mainwindow作为项目程序入口&#xff0c;窗口跳转流程为&#xff1a;mainwindow->gamegraph->area_prairrie->playerstastus。 主要游戏功能装载在gamegraph和area_prairrie两个ui中&#xff0…

webpack + typescript 开发微信小游戏实践

源码地址 微信小游戏版本技术选型使用typescript开发 但是微信小游戏原生不支持 typescript 开发&#xff0c;于是探索一下使用ts开发微信小游戏 1. 创建小游戏 使用测试号&#xff0c;创建一个使用官方示例的小游戏 会生成一个可以直接运行的打飞机小游戏 2. 准备工作 …

小游戏开发引擎CocosCreator

小游戏 六彩跳棋 已经通过审核并且发布了好几天了&#xff0c;对跳棋感兴趣的朋友可以去看看&#xff0c;在微信游戏里搜索 六彩跳棋 &#xff0c;点击 立即玩 吧&#xff01;进去游戏后&#xff0c;需要点击 获取头像昵称 才能得到玩家数据&#xff0c;然后 划动屏幕 选择游戏…

unity开发微信小游戏1

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、准备工作二、官方文档三、WX开发者工具四、获得Appid五、获得AppidError: app.json: app.json六、资源下载失败404总结 前言 最开使用unity3d开发微信小游…

【微信小游戏】微信小游戏开发设置竖屏

微信小游戏开发环境默认横屏&#xff0c;对竖屏游戏非常不方便。 设置竖屏的入口十分隐蔽&#xff0c;以至于一度令我以为不能设置竖屏。 网上也根本搜不到解决方法&#xff0c;经过我的不懈努力&#xff0c;最终还是找到了设置方法。 原本是横屏的 按下面路径设置竖屏 开发…

使用pygame开发一个小游戏

学习了pygame&#xff0c;身为一个IKUN所以&#xff0c;做了一个简单的小游戏。游戏规则是&#xff0c;使用键盘的方向键控制坤坤&#xff0c;当坤坤触碰到篮球&#xff0c;就会爆发出音乐”只因你太美“。代码如下&#xff1a; import random import sys import pygame pygam…

微信小游戏开发教程

微信小游戏开发教程-前言 自18年年初对开发者开放小游戏接口以后小游戏越来越火热&#xff0c;本文就是对小游戏开发的入门教程&#xff0c;希望这篇文章能够帮到想要入门开发游戏的你。 哈哈哈&#xff0c;看到有人说我故意分成好几篇刷PV&#xff0c;改了一下删了两篇&…

支持小游戏开发的“引擎四剑客”

2017年12月28日&#xff0c;微信发布了一款叫“跳一跳”的微信小程序的一个小游戏&#xff0c;无需下载安装&#xff0c;即点即玩&#xff0c;只需要在微信客户端的小程序界面搜索“跳一跳”&#xff0c;点击即可加载进入该游戏玩。游戏小而精&#xff0c;借助于微信巨大的社交…

小程序开发小游戏注意事项

今天研究小游戏开发&#xff0c;总结了一些自己遇到的问题 : 一. 注册appId 用小程序开发的小游戏跟用小程序开发其他项目不是公用的一个appId 如果你现在的小程序账号已经选了别的类目&#xff08;非游戏&#xff09;&#xff0c;那你就需要另外注册一个账号来单独申请小游戏…

【小程序】快来开发你的第一个微信小游戏(详细流程)

&#x1f973; 作者&#xff1a;伯子南 &#x1f60e; 坚信&#xff1a; 好记性不如乱笔头&#xff0c;独乐乐不如众乐乐 &#x1f4aa; 个人主页&#xff1a;https://blog.csdn.net/qq_34577234?spm1010.2135.3001.5421 &#x1f46c;&#x1f3fb; 觉得博主文章不错的话&…

小游戏开发

小游戏开发 1、游戏发展历史 广义&#xff1a;一种有组织的玩耍&#xff0c;一般是以娱乐为目的&#xff0c;有时也有教育目的。在英语中&#xff0c;体育比赛(Game)也是游戏&#xff0c;只要其活动本质带有目的、规则、挑战和互动&#xff0c;我们都可以把其归为游戏。 狭义…

小游戏开发指南及过程中的难点问题

如果仅仅针对个人开发者来讲&#xff0c;要独立开发一款大型游戏几乎无可能&#xff0c;更大成功的可能还是开发一款类似《羊了个羊》这样洗脑的小程序游戏。 所以这里主要论述小游戏开发的情况&#xff0c;也就是小程序游戏&#xff0c;首先从小游戏的开发流程来看&#xff1…

oracle数据库中的注释

oracle数据库中的注释 单行注释-- --这是oracle中的单行注释 SELECT SYSDATE FROM dual; 多行注释/**/ /*这是oracle中的多行注释*/ SELECT 6 6 AS "计算结果" FROM dual;

MYSQL数据库如何写注释

方式一 注意&#xff1a;在写注释时&#xff0c;–与注释之间必须要一个空格 select * from stu -- 注释方式二 注意&#xff1a;这种方式比较随意&#xff0c;加不加空格不影响 select * from t_info #注释select * from t_info#注释select * from t_info # 注释方式三 s…

Idea连接数据库,显示表注释

idea设置显示数据库表名注释 操作步骤&#xff1a; View Apperarance Details in Tree View选上

达梦数据库中的注释的使用

在管理规模较大的数据库时&#xff0c;我们往往需要面对大量的表与视图&#xff0c;与此同时在表与视图中可能会存在着许多的字段&#xff0c;让人难以迅速分辨&#xff0c;不利于对于数据库对象的管理。除了在命名时&#xff0c;对于有意义的表、视图及列&#xff0c;应尽量赋…

Jpa 自动建表的时候在数据库中添加注释。

github地址 本项目可以让 Jpa 自动建表的时候在数据库中添加注释。 为什么做这件事 过去想要让 Jpa 在建表的时候自动添加注释一般需要使用 Column#columnDefinition 属性。示例如下&#xff1a; Column(columnDefinition "INT COMMENT ...") private int unitI…

达梦数据库中迁移过来的数据,在Mysql中批量添加注释,修改注释

一、原因 数据是从达梦数据库中迁移过来的&#xff0c;迁移完成注释丢失 二、方法 利用 information_schema.COLUMNS 这个表 三、执行步骤 1.这是达梦数据库导出的注释2.达梦的数数据库数据导出时是和创建表和索引都在一起&#xff0c;因此需要单独把注释这一块的给提取出…

2021.3.17丨致病菌毒力因子(VFDB)数据库注释

摘要 接到一个常规细菌的组装注释项目&#xff0c;不过客户提出想要获取关于组装结果与病毒之间的联系/按之前的操作&#xff0c;dfast没有病毒相关的数据库&#xff0c;无法满足客户需求。一番查阅&#xff0c;发现大家用这个VFDB数据库进行常规的病毒注释&#xff0c;下面将介…

【TP5】获取数据库注释信息

author&#xff1a;咔咔 wechat&#xff1a;fangkangfk table为表名 $data Db::query(SHOW FULL COLUMNS FROM .$table);