原生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 相关文章推荐
JS实现打开本地文件或文件夹
Mar 09 Javascript
js根据手机客户端浏览器类型,判断跳转官网/手机网站多个实例代码
Apr 30 Javascript
使用PBFunc在Powerbuilder中支付宝当面付款功能
Oct 01 Javascript
使用vue与jquery实时监听用户输入状态的操作代码
Sep 19 jQuery
React Native日期时间选择组件的示例代码
Apr 27 Javascript
JS引用传递与值传递的区别与用法分析
Jun 01 Javascript
微信小程序HTTP接口请求封装的实现
Feb 21 Javascript
小程序识别身份证,银行卡,营业执照,驾照的实现
Nov 05 Javascript
浅析Vue下的components模板使用及应用
Nov 27 Javascript
jQuery实现小火箭返回顶部特效
Feb 03 jQuery
Node.js 在本地生成日志文件的方法
Feb 07 Javascript
vue项目配置使用flow类型检查的步骤
Mar 18 Javascript
微信小程序 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 XML备份Mysql数据库
2009/05/27 PHP
destoon实现调用图文新闻的方法
2014/08/21 PHP
PHP 中TP5 Request 请求对象的实例详解
2017/07/31 PHP
php连接sftp的作用以及实例代码
2019/09/23 PHP
checkbox设置复选框的只读效果不让用户勾选
2013/08/12 Javascript
JavaScript获取table中某一列的值的方法
2014/05/06 Javascript
对Js OOP编程 创建对象的一些全面理解
2016/07/26 Javascript
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2016/10/27 Javascript
iscroll动态加载数据完美解决方法
2017/07/18 Javascript
webpack+vue2构建vue项目骨架的方法
2018/01/09 Javascript
如何以Angular的姿势打开Font-Awesome详解
2018/04/22 Javascript
浅谈微信小程序之官方UI框架we-ui使用教程
2018/08/20 Javascript
详解在vue-cli项目下简单使用mockjs模拟数据
2018/10/19 Javascript
jQuery实现的鼠标拖动浮层功能示例【拖动div等任何标签】
2018/12/29 jQuery
微信小程序map组件结合高德地图API实现wx.chooseLocation功能示例
2019/01/23 Javascript
python list 合并连接字符串的方法
2013/03/09 Python
在Linux下调试Python代码的各种方法
2015/04/17 Python
深入学习python的yield和generator
2016/03/10 Python
wxpython中Textctrl回车事件无效的解决方法
2016/07/21 Python
Python代码解决RenderView窗口not found问题
2016/08/28 Python
浅谈python装饰器探究与参数的领取
2017/12/01 Python
Python构建网页爬虫原理分析
2017/12/19 Python
python实现求最长回文子串长度
2018/01/22 Python
使用python获取(宜宾市地震信息)地震信息
2019/06/20 Python
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
2019/07/22 Python
python抓取多种类型的页面方法实例
2019/11/20 Python
Python实现不规则图形填充的思路
2020/02/02 Python
利用canvas实现图片压缩的示例代码
2018/07/17 HTML / CSS
科尔士百货公司官网:Kohl’s
2016/07/11 全球购物
销售代表求职自荐信
2013/10/01 职场文书
优秀高中生事迹材料
2014/02/11 职场文书
搞笑的获奖感言
2014/08/16 职场文书
2015感人爱情寄语
2015/02/26 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书
用基于python的appium爬取b站直播消费记录
2021/04/17 Python
mybatis使用oracle进行添加数据的方法
2021/04/27 Oracle