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 相关文章推荐
jquery必须知道的一些常用特效方法及使用示例(整理)
Jun 24 Javascript
javascript使用正则控制input输入框允许输入的值方法大全
Jun 19 Javascript
简单的jquery左侧导航栏和页面选中效果
Aug 21 Javascript
利用jquery操作Radio方法小结
Oct 20 Javascript
浅谈javascript回调函数
Dec 07 Javascript
使用Jquery实现每日签到功能
Apr 03 Javascript
JavaScript数据结构与算法之栈与队列
Jan 29 Javascript
Bootstrap开发实战之第一次接触Bootstrap
Jun 02 Javascript
jQuery动态生成Bootstrap表格
Nov 01 Javascript
select自定义小三角样式代码(实用总结)
Aug 18 Javascript
jQuery选择器之属性过滤选择器详解
Sep 28 jQuery
layui插件表单验证提交触发提交的例子
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 array_multisort() 函数的深入解析
2013/06/20 PHP
php数组删除元素示例
2014/03/21 PHP
jQuery toggle()设置CSS样式
2009/11/05 Javascript
用Jquery实现多级下拉框无刷新的联动
2010/12/22 Javascript
javascript一些实用技巧小结
2011/03/18 Javascript
jQuery中的.bind()、.live()和.delegate()之间区别分析
2011/06/08 Javascript
Extjs TimeField 显示正常时间格式的代码
2011/06/28 Javascript
jsonp原理及使用
2013/10/28 Javascript
JavaScript伸缩的菜单简单示例
2013/12/03 Javascript
在页面上用action传递参数到后台出现乱码的解决方法
2013/12/31 Javascript
JQuery中层次选择器用法实例详解
2015/05/18 Javascript
JavaScript中字面量与函数的基本使用知识
2015/10/20 Javascript
IE8利用自带的setCapture和releaseCapture解决iframe的拖拽事件方法
2016/10/25 Javascript
js对字符串进行编码的方法总结(推荐)
2016/11/10 Javascript
微信小程序 使用canvas制作K线实例详解
2017/01/12 Javascript
nodejs+websocket实时聊天系统改进版
2017/05/18 NodeJs
在Js页面通过POST传递参数跳转到新页面详解
2017/08/25 Javascript
深入理解vue-router之keep-alive
2017/08/31 Javascript
微信小程序实现下拉刷新和轮播图效果
2017/11/21 Javascript
原生javascript的ajax请求及后台PHP响应操作示例
2020/02/24 Javascript
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
Python类的继承、多态及获取对象信息操作详解
2019/02/28 Python
Python实现的ftp服务器功能详解【附源码下载】
2019/06/26 Python
python opencv 图像拼接的实现方法
2019/06/27 Python
python写入文件自动换行问题的方法
2019/07/05 Python
迪斯尼商品官方网站:ShopDisney
2016/08/01 全球购物
美国优质马术服装购买网站:Breeches.com
2019/12/16 全球购物
日本化妆品植村秀俄罗斯官方网站:Shu Uemura俄罗斯
2020/02/01 全球购物
工商企业管理应届生求职信
2013/11/03 职场文书
建筑公司文秘岗位职责
2013/11/29 职场文书
转预备党员政审材料
2014/02/06 职场文书
2014年招生工作总结
2014/11/26 职场文书
2014年药剂科工作总结
2014/11/26 职场文书
2015年机关党建工作总结
2015/05/22 职场文书
2016教师廉洁教育心得体会
2016/01/13 职场文书
解析高可用Redis服务架构分析与搭建方案
2021/06/20 Redis