原生JS实现贪吃蛇小游戏


Posted in Javascript onMarch 09, 2020

本文实例为大家分享了JS实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下

思路:在页面上添加一个地图,以提供边界,在地图中随机出现食物,给蛇身设置一个初始长度,用键盘方向键控制蛇的走向,当蛇触碰到食物时(既坐标重复时),增加蛇身长度,碰到墙壁或自身时,程序停止,游戏结束。

HTML结构:

<body>
 <div id="map"></div>
</body>

CSS样式:

<style>
 #map{
  width: 600px;
  height: 300px;
  background: #ccc;
  border: 5px solid blacks;
  margin: 0 auto;
  position: relative;}
</style>

js实现功能:

<script>
 class Map{ //地图
  constructor(){
   this.mapEle = document.getElementById("map");
   this.w = this.mapEle.offsetWidth;
   this.h = this.mapEle.offsetHeight;
  }
 }
 class Food{ //食物
  constructor(){
   this.w = 20;
   this.h = 20;
   this.x = 0;
   this.y = 0;
   this.c = "orange";
   this.createEle();
  } 
  createEle(){
   this.foodEle = document.createElement("div");
   this.foodEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};position:absolute;left:${this.x * this.w}px;top:${this.y * this.h}px;border-radius: 40%;`;
   m.mapEle.appendChild(this.foodEle);
  }
  randomPos(){
   this.x = random(0,(m.w-this.w) / this.w);
   this.y = random(0,(m.h-this.h) / this.h);
   this.foodEle.style.left = this.x * this.w + "px";
   this.foodEle.style.top = this.y * this.h + "px";
  }
 }
 class Snake{ //身体
  constructor(){
   this.w = 20;
   this.h = 20;
   this.body = [{
     ele:null,x:4,y:3,c:randomColor()
   },{
    ele:null,x:3,y:3,c:randomColor()
   },{
    ele:null,x:2,y:3,c:randomColor()
   }];
   this.d = "right"; //设置默认行走方向
   this.createEle();
  }
  createEle(){
   for(var i=0;i<this.body.length;i++){
    if(!this.body[i].ele){
     this.body[i].ele = document.createElement("div");
     m.mapEle.appendChild(this.body[i].ele);
    }
    this.body[i].ele.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.body[i].c};position:absolute;left:${this.body[i].x * this.w}px;top:${this.body[i].y * this.h}px;color:#fff;border-radius: 40%;`;
   }
   this.body[0].ele.innerHTML = "00"
   setTimeout(()=>{this.move()},300);
  }
  move(){
   for(var i=this.body.length-1;i>0;i--){
    this.body[i].x = this.body[i-1].x;
    this.body[i].y = this.body[i-1].y;
   }
   switch(this.d){
    case "left":this.body[0].x -= 1;break;
    case "right":this.body[0].x += 1;break;
    case "top":this.body[0].y -= 1;break;
    case "bottom":this.body[0].y += 1;break;
   }
   if(this.body[0].x < 0 || this.body[0].y < 0 || this.body[0].x > ((m.w-this.w) / this.w) || this.body[0].y > ((m.h-this.h) / this.h)){
    alert("撞墙了");
    return;
   }
   if(this.body[0].x === f.x && this.body[0].y === f.y){
    this.body.push({ele:null,x:this.body[this.body.length-1].x,y:this.body[this.body.length-1].y,c:randomColor()});
    f.randomPos();
   }
   for(var i=1;i<this.body.length;i++){
    if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
     alert("撞到自己了");
     return;
    }
   }
   this.createEle();
  }
  direct(type){
   switch(type){
    case 37:
     if(this.d === "right") break;
     this.d = "left";break;
    case 38:
     if(this.d === "bottom") break;
     this.d = "top";break;
    case 39:
     if(this.d === "left") break;
     this.d = "right";break;
    case 40:
     if(this.d === "top") break;
     this.d = "bottom";break;
   }
  }
 }
 function random(a,b){
  return Math.round(Math.random()*(a-b)+b);
 }
 function randomColor(){
  return `rgb(${random(0,255)},${random(0,255)},${random(0,255)})`;
 }

 var m = new Map();

 var f = new Food();
 f.randomPos();

 var s = new Snake();
 document.onkeydown = function(eve){
  var e = eve || window.event;
  var code = e.keyCode || e.which;
  s.direct(code);
 }
</script>

小编还为大家准备了精彩的专题:javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript中的new的使用方法与注意事项
May 16 Javascript
jQuery判断iframe中元素是否存在的方法
May 11 Javascript
For循环中分号隔开的3部分的执行顺序探讨
May 27 Javascript
JavaScript组件开发完整示例
Dec 15 Javascript
理解javascript中的with关键字
Feb 15 Javascript
EasyUI实现下拉框多选功能
Nov 07 Javascript
关于angularJs清除浏览器缓存的方法
Nov 28 Javascript
React Native基础入门之调试React Native应用的一小步
Jul 02 Javascript
微信小程序踩坑记录之解决tabBar.list[3].selectedIconPath大小超过40kb
Jul 04 Javascript
Vue如何使用混合Mixins和插件开发详解
Feb 05 Javascript
javascript 函数的暂停和恢复实例详解
Apr 25 Javascript
Vue提供的三种调试方式你知道吗
Jan 18 Vue.js
微信小程序 wx.getUserInfo引导用户授权问题实例分析
Mar 09 #Javascript
在Vue中实现随hash改变响应菜单高亮
Mar 09 #Javascript
Node.js+Vue脚手架环境搭建的方法步骤
Mar 08 #Javascript
JS中的const命令你真懂它吗
Mar 08 #Javascript
Vue2.4+新增属性.sync、$attrs、$listeners的具体使用
Mar 08 #Javascript
Vue vm.$attrs使用场景详解
Mar 08 #Javascript
浅谈Vue2.4.0 $attrs与inheritAttrs的具体使用
Mar 08 #Javascript
You might like
php中选择什么接口(mysql、mysqli)访问mysql
2013/02/06 PHP
php设置静态内容缓存时间的方法
2014/12/01 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
2018/04/21 PHP
PHP执行linux命令6个函数代码实例
2020/11/24 PHP
JavaScript的类型转换(字符转数字 数字转字符)
2010/08/30 Javascript
js或者jquery判断图片是否加载完成实现代码
2013/03/20 Javascript
jquery Tab效果和动态加载的简单实例
2013/12/11 Javascript
jquery防止重复执行动画避免页面混乱
2014/04/22 Javascript
jQuery实现感应鼠标动画效果自动伸长的输入框实例
2015/02/24 Javascript
微信小程序之仿微信漂流瓶实例
2016/12/09 Javascript
JavaScript限制在客户区可见范围的拖拽(解决scrollLeft和scrollTop的问题)(2)
2017/05/17 Javascript
纯js实现的积木(div层)拖动功能示例
2017/07/19 Javascript
vue axios用法教程详解
2017/07/23 Javascript
vue2.0在table中实现全选和反选的示例代码
2017/11/04 Javascript
vue 2.0 购物车小球抛物线的示例代码
2018/02/01 Javascript
node实现登录图片验证码的示例代码
2018/04/20 Javascript
vuex与组件联合使用的方法
2018/05/10 Javascript
Vue.js 中 axios 跨域访问错误问题及解决方法
2018/11/21 Javascript
使用vue-cli webpack 快速搭建项目的代码
2018/11/21 Javascript
深入理解vue-class-component源码阅读
2019/02/18 Javascript
vue2.0+SVG实现音乐播放圆形进度条组件
2019/09/21 Javascript
Vue实现将数据库中带html标签的内容输出(原始HTML(Raw HTML))
2019/10/28 Javascript
[48:41]VP vs VG Supermajor小组赛 B组胜者组决赛 BO3 第二场 6.2
2018/06/03 DOTA
Python 命令行非阻塞输入的小例子
2013/09/27 Python
python unittest实现api自动化测试
2018/04/04 Python
在python中使用xlrd获取合并单元格的方法
2018/12/26 Python
python调用c++传递数组的实例
2019/02/13 Python
Python多叉树的构造及取出节点数据(treelib)的方法
2019/08/09 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
2020/04/15 Python
离线状态下在jupyter notebook中使用plotly实例
2020/04/24 Python
python collections模块的使用
2020/10/16 Python
python实现xml转json文件的示例代码
2020/12/30 Python
瑞士香水购物网站:Parfumcity.ch
2017/01/14 全球购物
2014年电话销售工作总结
2014/12/01 职场文书
远程教育学习心得体会
2016/01/23 职场文书
html+css 实现简易导航栏功能
2021/04/07 HTML / CSS