js实现经典贪吃蛇小游戏


Posted in Javascript onMarch 19, 2020

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

<script>
  
  class Map{
    constructor(){
      this.w = 800;
      this.h = 400;
      this.c = "#ccc";
      this.createEle();
    }
    createEle(){
      this.mapEle = document.createElement("div");
      this.mapEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};margin:0 auto;position:relative;border:solid 10px black;`;
      document.body.appendChild(this.mapEle);
    }
  }
  class Food{
    constructor(){
      this.w = 20;
      this.h = 20;
      this.c = "red";
      this.x = 0;
      this.y = 0;
      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;`;
      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;`;
      }
      this.body[0].ele.innerHTML = "0";
 
      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>

js实现经典贪吃蛇小游戏

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

Javascript 相关文章推荐
javascript 中对象的继承〔转贴〕
Jan 22 Javascript
javascript 一段左右两边随屏滚动的代码
Jun 18 Javascript
JQuery 文本框使用小结
May 22 Javascript
JavaScript中for..in循环陷阱介绍
Nov 12 Javascript
JavaScript1.6数组新特性介绍以及JQuery的几个工具方法
Dec 06 Javascript
当达到输入长度时表单自动切换焦点
Apr 06 Javascript
Jquery动态添加及删除页面节点元素示例代码
Jun 16 Javascript
js中取得变量绝对值的方法
Jan 03 Javascript
jquery ajaxfileupload异步上传插件使用详解
Feb 08 Javascript
Vue单页式应用(Hash模式下)实现微信分享的实例
Jul 21 Javascript
javascriptvoid(0)含义以及与&quot;#&quot;的区别讲解
Jan 19 Javascript
vue父组件触发事件改变子组件的值的方法实例详解
May 07 Javascript
javascrpt密码强度校验函数详解
Mar 18 #Javascript
Node.js Domain 模块实例详解
Mar 18 #Javascript
js判断密码强度的方法
Mar 18 #Javascript
vue项目配置使用flow类型检查的步骤
Mar 18 #Javascript
Vue项目中使用flow做类型检测的方法
Mar 18 #Javascript
JavaScript正则表达式验证登录实例
Mar 18 #Javascript
JS正则表达式验证密码强度
Mar 18 #Javascript
You might like
深入apache配置文件httpd.conf的部分参数说明
2013/06/28 PHP
thinkPHP简单遍历数组方法分析
2016/05/16 PHP
Laravel框架实现多个视图共享相同数据的方法详解
2019/07/09 PHP
jquery设置元素的readonly和disabled的写法
2013/09/22 Javascript
代码获取历史上的今天发生的事
2014/04/11 Javascript
jQuery 复合选择器应用的几个例子
2014/09/11 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
jQuery实现MSN中文网滑动Tab菜单效果代码
2015/09/09 Javascript
今天抽时间给大家整理jquery和ajax的相关知识
2015/11/17 Javascript
JavaScript实现图片滑动切换的代码示例分享
2016/03/06 Javascript
深入解析JavaScript中函数的Currying柯里化
2016/03/19 Javascript
探索Javascript中this的奥秘
2016/12/11 Javascript
JQuery 进入页面默认给已赋值的复选框打钩
2017/03/23 jQuery
详解angular2实现ng2-router 路由和嵌套路由
2017/03/24 Javascript
vue组件中watch props根据v-if动态判断并挂载DOM的问题
2019/05/12 Javascript
React传值 组件传值 之间的关系详解
2019/08/26 Javascript
webpack 处理CSS资源的实现
2019/09/27 Javascript
Vue路由对象属性 .meta $route.matched详解
2019/11/04 Javascript
JS实现4位随机验证码
2020/10/19 Javascript
Vue实现多页签组件
2021/01/14 Vue.js
CentOS 7下安装Python 3.5并与Python2.7兼容并存详解
2017/07/07 Python
django定期执行任务(实例讲解)
2017/11/03 Python
python实现用户管理系统
2018/01/10 Python
python指定写入文件时的编码格式方法
2018/06/07 Python
Python上下文管理器类和上下文管理器装饰器contextmanager用法实例分析
2019/11/07 Python
Python 根据数据模板创建shapefile的实现
2019/11/26 Python
python opencv图片编码为h264文件的实例
2019/12/12 Python
如何配置关联Python 解释器 Anaconda的教程(图解)
2020/04/30 Python
python爬虫scrapy框架之增量式爬虫的示例代码
2021/02/26 Python
学生生病请假条范文
2014/02/16 职场文书
2014年教研员工作总结
2014/12/23 职场文书
运动会班级口号霸气押韵
2015/12/24 职场文书
zabbix监控mysql的实例方法
2021/06/02 MySQL
MySQL基础快速入门知识总结(附思维导图)
2021/09/25 MySQL
MySQL 逻辑备份 into outfile
2022/05/15 MySQL
python如何利用cv2.rectangle()绘制矩形框
2022/12/24 Python