当前位置: 首页 > news >正文

做一个贪吃蛇小游戏happy一下

直接Vue上代码

<template><div><div>贪吃蛇</div><canvas id="canvas" width="400" height="400"></canvas></div>
</template><script>
export default {data() {return {ctx: null,interval: null,snakeData: [],count: 0,//步数pointList: [{ x: 36, y: 18 }],//默认点direction: "d", // 默认右,w:上,s:下,a:左,d:右controll: null,//监听controll2: null,//监听speedCount: 0, // 渲染倍数speed: 8 // 渲染倍数分子};},mounted() {// 贪吃蛇,canvasconst canvas = document.getElementById("canvas");this.ctx = canvas.getContext("2d"); // 获取绘制上下文const list = [];for (let i = 0; i < 2; i++) {list.push({x: 0 + i * 6,y: 0,type: "right"});}this.snakeData = list;console.log("snakeData", this.snakeData);this.setView(list);this.controll = new AbortController();this.controll2 = new AbortController();window.addEventListener("keydown",val => {if (val.key === "w" ||val.key === "a" ||val.key === "s" ||val.key === "d") {// 方向不能相反switch (val.key) {case "w":if (this.direction !== "s") {this.direction = "w";this.speed = 4;}break;case "a":if (this.direction !== "d") {this.direction = "a";this.speed = 4;}break;case "s":if (this.direction !== "w") {this.direction = "s";this.speed = 4;}break;case "d":if (this.direction !== "a") {this.direction = "d";this.speed = 4;}break;default:console.log("方向不合法或无效按键");break;}}},{ signal: this.controll.signal });window.addEventListener("keyup",val => {if (val.key === "w" ||val.key === "a" ||val.key === "s" ||val.key === "d") {this.speed = 8;}},{ signal: this.controll2.signal });this.count = 0;this.intervalFun();},beforeDestroy() {this.count = 0;this.interval && cancelAnimationFrame(this.interval);this.controll.abort();this.controll2.abort();},methods: {intervalFun() {this.count++;this.speedCount++;// 默认向右移动if (this.count > 5000) {// clearInterval(this.interval);cancelAnimationFrame(this.interval);} else {if (this.speedCount % this.speed === 0) {// 速度缩减10倍,执行10次才渲染1次const w = canvas.clientWidth;const h = canvas.clientHeight;this.ctx.clearRect(0, 0, w, h);this.goForword(this.snakeData);}}this.interval = requestAnimationFrame(this.intervalFun);},goForword(list) {// console.log("list1", list);const option = { ...list[list.length - 1] };if (this.direction === "d") {option.x = Number(option.x) + 6;} else if (this.direction === "a") {option.x = Number(option.x) - 6;} else if (this.direction === "w") {option.y = Number(option.y) - 6;} else if (this.direction === "s") {option.y = Number(option.y) + 6;}list.push(option);let flag = false;// console.log("option", option);this.pointList = this.pointList.filter(item => {if (option.x === item.x && option.y === item.y) {flag = true;}return item.x !== option.x || item.y !== option.y;});if (!flag) {list.shift();} else {// 重新生成点const x = Math.floor(Math.random() * Math.round(400 / 6)) * 6;const y = Math.floor(Math.random() * Math.round(400 / 6)) * 6;this.pointList.push({x,y});}this.snakeData = list;// console.log("snakeData", this.snakeData);this.setView(this.snakeData); // 画蛇this.setView(this.pointList); // 画点},setView(list) {// this.ctx.clearRact(0, 0, 400, 400);this.ctx.beginPath();for (let i = 0; i < list.length; i++) {this.ctx.rect(list[i].x, list[i].y, 6, 6); // 绘制矩形}this.ctx.fill(); // 描边一个矩形轮廓}}
}
</script>
<style lang="scss" scoped>#canvas {border: 1px solid #dee2ed;
}
</style>

直接拿着用即可

http://www.lryc.cn/news/178254.html

相关文章:

  • opencv形态学-膨胀
  • 玄子Share 设计模式 GOF 全23种 + 七大设计原则
  • 单链表操作 C实现
  • WordPress主题网站首页添加好看的四格小工具教程
  • unittest自动化测试框架讲解以及实战
  • 数学建模之Matlab基础操作
  • 【Nuxt】04 Nuxt2-SEO: sitemap.xml、seo优化、robots.txt
  • VMware VSAN 入门
  • 【设计模式】备忘录模式
  • vue3+elementUiPlus表格导出功能
  • 专题五:优先级队列
  • 游戏设计模式专栏(一):工厂方法模式
  • element中使用el-steps 进度条效果demo(整理)
  • 038:mapboxGL 旋转地图(rotateTo)
  • java遇到的问题
  • LLM(二)| LIMA:在1k高质量数据上微调LLaMA1-65B,性能超越ChatGPT
  • Android AMS——创建Application(七)
  • html 边缘融合加载
  • ElasticSearch - 在 微服务项目 中基于 RabbitMQ 实现 ES 和 MySQL 数据异步同步(考点)
  • Springboot+vue的企业人事管理系统(有报告),Javaee项目,springboot vue前后端分离项目。
  • 初识Java 11-1 函数式编程
  • 【Ambari】银河麒麟V10 ARM64架构_安装Ambari2.7.6HDP3.3.1问题总结
  • 李宏毅机器学习第一课(结尾附作业模型详细分析)
  • 对日项目工作总结
  • 设计模式探索:从理论到实践的编码示例 (软件设计师笔记)
  • 【内网穿透】在Ubuntu搭建Web小游戏网站,并将其发布到公网访问
  • 在cesuim上展示二维模型
  • c/c++中如何输入pi
  • python爬虫:JavaScript 混淆、逆向技术
  • Vue error:0308010C:digital envelope routines::unsupported