javascript实现简单的贪吃蛇游戏


Posted in Javascript onMarch 31, 2015

javascript实现简单的贪吃蛇游戏,功能很简单,代码也很实用,就不多废话了,小伙伴们参考注释吧。

<html>
<head>
  <meta http-equiv='content-type' content='text/html;charset=utf-8'>
<title>贪吃蛇</title>
<script type="text/javascript">
  var map;  //地图
  var snake; //蛇
  var food;  //食物
  var timer; //定时器
  var initSpeed=200; //初始定时器时间间隔(毫秒),间接代表蛇移动速度
  var nowSpeed=initSpeed; //游戏进行时蛇移动速度
  var grade=0;  //积分
  var flag=1;   //(可间接看做)关卡
  //地图类
  function Map(){
    this.width=800;
    this.height=400;
    this.position='absolute';
    this.color='#EEEEEE';
    this._map=null;
    //生成地图
    this.show=function(){
      this._map=document.createElement('div');
      this._map.style.width=this.width+'px';
      this._map.style.height=this.height+'px';
      this._map.style.position=this.position;
      this._map.style.backgroundColor=this.color;
      document.getElementsByTagName('body')[0].appendChild(this._map);
    }
  }
  //食物类
  function Food(){
    this.width=20;
    this.height=20;
    this.position='absolute';
    this.color='#00FF00';
    this.x=0;
    this.y=0;
    this._food;
    //生成食物
    this.show=function(){
      this._food=document.createElement('div');
      this._food.style.width=this.width+'px';
      this._food.style.height=this.height+'px';
      this._food.style.position=this.position;
      this._food.style.backgroundColor=this.color;
      this.x=Math.floor(Math.random()*map.width/this.width);
      this.y=Math.floor(Math.random()*map.height/this.width);
      this._food.style.left=this.x*this.width;
      this._food.style.top=this.y*this.height;
 
      map._map.appendChild(this._food);
    }
  }
  //蛇类
  function Snake(){
    this.width=20;
    this.height=20;
    this.position='absolute';
    this.direct=null;//移动方向
    //初始蛇身
    this.body=new Array(
        [3,2,'red',null],//蛇头
        [2,2,'blue',null],
        [1,2,'blue',null]
      );
    //生成蛇身
    this.show=function(){
      for(var i=0;i<this.body.length;i++){
        if(this.body[i][3]==null){
          this.body[i][3]=document.createElement('div');
          this.body[i][3].style.width=this.width;
          this.body[i][3].style.height=this.height;
          this.body[i][3].style.position=this.position;
          this.body[i][3].style.backgroundColor=this.body[i][2];
          map._map.appendChild(this.body[i][3]);
        }
        this.body[i][3].style.left=this.body[i][0]*this.width+'px';
        this.body[i][3].style.top=this.body[i][1]*this.height+'px';
      }
    }
    //控制蛇移动
    this.move=function(){
       
      var length=this.body.length-1;
      for(var i=length;i>0;i--){
        this.body[i][0]=this.body[i-1][0];
        this.body[i][1]=this.body[i-1][1];
      }
 
      switch(this.direct){
        case 'right':
          this.body[0][0]=this.body[0][0]+1;
          break;
        case 'left':
          this.body[0][0]=this.body[0][0]-1;
          break;
        case 'up':
          this.body[0][1]=this.body[0][1]-1;
          break;
        case 'down':
          this.body[0][1]=this.body[0][1]+1;
          break;
      }
       
      this.condition();
      this.show();
    }
    //定时器,开始游戏时,调用
    this.speed=function(){
      timer=setInterval('snake.move()',initSpeed);
    }
    //条件处理
    this.condition=function(){
      //吃食物
      if(this.body[0][0]==food.x&&this.body[0][1]==food.y){
        grade++;
        this.body[[this.body.length]]=[0,0,'blue',null];
        map._map.removeChild(food._food)
        food.show();
      }
      //判断是否撞墙
      if(this.body[0][0]<0||this.body[0][0]>=map.width/this.width||this.body[0][1]<0||this.body[0][1]>=map.height/this.height){
        alert('game over');
        clearInterval(timer);
        return ;
      }
      //判断是否撞到自身
      for(var i=1;i<this.body.length;i++){
        if(this.body[0][0]==this.body[i][0]&&this.body[0][1]==this.body[i][1]){
          alert('game over');
          clearInterval(timer);
          return ;
        }
      }
      //速度提升处理,积分每曾2分,速度提升一倍
      if(grade/2==flag){
        clearInterval(timer);
        flag++;
        nowSpeed=initSpeed/flag;
        timer=setInterval('snake.move()',nowSpeed);
      }
      document.title='贪吃蛇 积分'+grade+' 速度等级'+initSpeed/nowSpeed;
 
    }
  }
 
  document.onkeydown=function(event){
    //按下任意键,开始游戏
    if(snake.direct===null){
      snake.direct='right';
      snake.speed();
    }
    //控制方向,W S D A分别代表 上下右左
    switch(window.event?window.event.keyCode:event.keyCode){//浏览器兼容处理
      case 87 :
        snake.direct=snake.body[0][0]==snake.body[1][0]?snake.direct:'up';//避免反向移动,触发死亡bug
        break;
      case 83 :
        snake.direct=snake.body[0][0]==snake.body[1][0]?snake.direct:'down';
        break;
      case 68 :
        snake.direct=snake.body[0][1]==snake.body[1][1]?snake.direct:'right';
        break;
      case 65 :
        snake.direct=snake.body[0][1]==snake.body[1][1]?snake.direct:'left';
        break;
    }
  }
  //自动加载游戏
  window.onload=function(){
    map=new Map();
    map.show();
    food=new Food();
    food.show();
    snake=new Snake();
    snake.show();
     
  }
</script>
</head>
<body>
</body>
</html>

以上所述就是本文的全部内容了,希望大家能够喜欢。

Javascript 相关文章推荐
23个Javascript弹出窗口特效整理
Feb 25 Javascript
JQuery+JS实现仿百度搜索结果中关键字变色效果
Aug 02 Javascript
Javascript的时间戳和php的时间戳转换注意事项
Apr 12 Javascript
js实现的切换面板实例代码
Jun 17 Javascript
改变checkbox默认选中状态及取值的实现代码
May 26 Javascript
JavaScript必知必会(七)js对象继承
Jun 08 Javascript
ES6扩展运算符的用途实例详解
Aug 20 Javascript
angular动态表单制作
Feb 23 Javascript
vue实现重置表单信息为空的方法
Sep 29 Javascript
微信小程序swiper左右扩展各显示一半代码实例
Dec 05 Javascript
javascript设计模式 ? 职责链模式原理与用法实例分析
Apr 16 Javascript
在Vue中获取自定义属性方法:data-id的实例
Sep 09 Javascript
javascript制作2048游戏
Mar 30 #Javascript
JavaScript模拟实现继承的方法
Mar 30 #Javascript
jQuery制作可自定义大小的拼图游戏
Mar 30 #Javascript
JS实现向表格中动态添加行的方法
Mar 30 #Javascript
JS实现向表格行添加新单元格的方法
Mar 30 #Javascript
JS实现控制表格行文本对齐的方法
Mar 30 #Javascript
JS实现控制表格行内容垂直对齐的方法
Mar 30 #Javascript
You might like
浅析PHP的ASCII码转换类
2013/07/05 PHP
PHP命名空间(namespace)的动态访问及使用技巧
2014/08/18 PHP
WordPress开发中用于标题显示的相关函数使用解析
2016/01/07 PHP
Laravel中服务提供者和门面模式的入门介绍
2017/11/06 PHP
php常用字符串长度函数strlen()与mb_strlen()用法实例分析
2019/06/25 PHP
Java 正则表达式学习总结和一些小例子
2012/09/13 Javascript
JS获取月的最后一天与JS得到一个月份最大天数的实例代码
2013/12/16 Javascript
js,jquery滚动/跳转页面到指定位置的实现思路
2014/06/03 Javascript
jQuery实现鼠标滑过点击事件音效试听
2015/08/31 Javascript
jQuery获取当前点击的对象元素(实现代码)
2016/05/19 Javascript
概述javascript在Google IE中的调试技巧
2016/11/24 Javascript
js中小数向上取整数,向下取整数,四舍五入取整数的实现(必看篇)
2017/02/13 Javascript
Vue路由跳转问题记录详解
2017/06/15 Javascript
Vue自定义事件(详解)
2017/08/19 Javascript
JS+HTML5实现获取手机验证码倒计时按钮
2018/08/08 Javascript
Vue事件修饰符native、self示例详解
2019/07/09 Javascript
js实现数据导出为EXCEL(支持大量数据导出)
2020/03/31 Javascript
Python实现把数字转换成中文
2015/06/29 Python
django表单实现下拉框的示例讲解
2018/05/29 Python
python中单例常用的几种实现方法总结
2018/10/13 Python
python看某个模块的版本方法
2018/10/16 Python
Python依赖包整体迁移方法详解
2019/08/15 Python
PyCharm2018 安装及破解方法实现步骤
2019/09/09 Python
Tensorflow轻松实现XOR运算的方式
2020/02/03 Python
深入浅析Python代码规范性检测
2020/07/31 Python
python 如何实现遗传算法
2020/09/22 Python
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
Clos19英国:高档香槟、葡萄酒和烈酒在线购物平台
2020/07/10 全球购物
简述synchronized和java.util.concurrent.locks.Lock的异同
2014/12/08 面试题
日语专业个人的求职信
2013/12/03 职场文书
党章培训心得体会
2014/09/04 职场文书
安全生产标语大全
2014/10/06 职场文书
2014年科室工作总结范文
2014/12/19 职场文书
教师学期述职自我鉴定
2019/08/16 职场文书
创业计划书之干洗店
2019/09/10 职场文书
Java 将PPT幻灯片转为HTML文件的实现思路
2021/06/11 Java/Android