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 解析url的search方法
Feb 09 Javascript
某页码显示的helper 少量调整,另附js版
Sep 12 Javascript
基于JQuery实现异步刷新的代码(转载)
Mar 29 Javascript
读JavaScript DOM编程艺术笔记
Nov 15 Javascript
js实现图片轮换效果代码
Apr 16 Javascript
莱鸟介绍window.print()方法
Jan 06 Javascript
深入浅析JavaScript中的constructor
Apr 19 Javascript
为什么JavaScript没有块级作用域
May 22 Javascript
Jquery针对tr td的一些实用操作方法(必看篇)
Oct 05 Javascript
javascript判断firebug是否开启的方法
Nov 23 Javascript
AngularJS 购物车全选/取消全选功能的实现方法
Aug 14 Javascript
vue中导出Excel表格的实现代码
Oct 18 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
如何使用动态共享对象的模式来安装PHP
2006/10/09 PHP
Codeigniter中mkdir创建目录遇到权限问题和解决方法
2014/07/25 PHP
PHP中如何判断exec函数执行成功?
2016/08/04 PHP
Javascript 类型转换方法
2010/10/24 Javascript
JavaScript格式化数字的函数代码
2010/11/30 Javascript
关于Javascript作用域链的八点总结
2013/12/06 Javascript
jquery获取元素索引值index()示例
2014/02/13 Javascript
使用javascript插入样式
2016/03/14 Javascript
jQuery实现点击按钮文字变成input框点击保存变成文字
2016/05/09 Javascript
IScroll5 中文API参数说明和调用方法
2016/05/21 Javascript
AngularJS基础 ng-disabled 指令详解及简单示例
2016/08/01 Javascript
Vuejs 页面的区域化与组件封装的实现
2017/09/11 Javascript
JavaScript实现音乐自动切换和轮播
2017/11/05 Javascript
JSON的parse()方法介绍
2019/01/31 Javascript
js验证身份证号码记录的方法
2019/04/26 Javascript
Vue scrollBehavior 滚动行为实现后退页面显示在上次浏览的位置
2019/05/27 Javascript
ES10的13个新特性示例(小结)
2019/09/23 Javascript
vue简单封装axios插件和接口的统一管理操作示例
2020/02/02 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
Vue 中如何将函数作为 props 传递给组件的实现代码
2020/05/12 Javascript
给Python IDLE加上自动补全和历史功能
2014/11/30 Python
Python使用CMD模块更优雅的运行脚本
2015/05/11 Python
Python编程之属性和方法实例详解
2015/05/19 Python
利用python3 的pygame模块实现塔防游戏
2019/12/30 Python
Python文件夹批处理操作代码实例
2020/07/21 Python
详解matplotlib绘图样式(style)初探
2021/02/03 Python
css3实现一个div设置多张背景图片及background-image属性实例演示
2017/08/10 HTML / CSS
如何给HTML标签中的文本设置修饰线
2019/11/18 HTML / CSS
素食餐饮项目创业计划书
2014/02/02 职场文书
新闻学专业个人求职信写作
2014/02/04 职场文书
给老师的感谢信
2015/01/20 职场文书
药店收银员岗位职责
2015/04/07 职场文书
大学生团日活动总结
2015/05/06 职场文书
狂人日记读书笔记
2015/06/30 职场文书
2016七一建党节慰问信
2015/11/30 职场文书
【2·13】一图读懂中国无线电发展
2022/02/18 无线电