贪吃蛇的javascript实现

最近在学Javascript,花了半个月的时间终于把《Javascript权威指南》(推荐此书,入门级) 扫完。

在学习的过程中发现使用JS实现动态效果挺有趣的。

 

在习作的过程中尝试着贪吃蛇游戏用JS实现了。竟然成功了。

思路:使用10px*10px的div层担当“像素”,然后使用40*40矩阵160个“像素”构成了游戏的界面。

下面是代码:

 

  1. // JavaScript Document
  2. alert("键盘的方向键控制方向,空格键暂停。/nLIFE制作/nhttp://blog.csdn.net/anhulife");
  3. // 添加基本的图形块,即160个10 * 10的层组成的二维矩阵
  4. var rowindex = new Array(40);
  5. var colindex;
  6. var cell;
  7. // 图像单元的定义
  8. var backcolor = "black";
  9. for(var i = 0; i < 40; i ++ )
  10. {
  11.    colindex = new Array(40);
  12.    for(var j = 0; j < 40; j ++ )
  13.    {
  14.       // 设置每个单元的属性
  15.       cell = document.createElement("div");
  16.       cell.style.backgroundColor = backcolor;
  17.       cell.style.width = "10px";
  18.       cell.style.height = "10px";
  19.       cell.style.position = "absolute";
  20.       cell.style.left = "" + (j * 10 + 100) + "px";
  21.       cell.style.top = "" + (i * 10 + 100) + "px";
  22.       cell.style.overflow = "hidden";
  23.       // 添加单元
  24.       document.body.appendChild(cell);
  25.       // 填充列组
  26.       colindex[j] = cell;
  27.    }
  28.    // 填充行组
  29.    rowindex[i] = colindex;
  30. }
  31. // 贪吃蛇类的定义,基于基本的图像单元
  32. function snake()
  33. {
  34.    // 定义蛇的身体,并初始化
  35.    this.bodycolor = "white";
  36.    this.bodys = new Array();
  37.    for(var i = 20; i < 25; i ++ )
  38.    {
  39.       rowindex[20][i].style.backgroundColor = this.bodycolor;
  40.       // rowindex的第一个坐标是行标,第二是列标
  41.       this.bodys.push(rowindex[20][i]);
  42.    }
  43.    // 定义蛇的头部坐标,第一个是行标, 第二个是列标
  44.    this.head = [20, 20];
  45.    // 定义蛇的前进方向,0代表左、1代表下、2代表右、3代表上
  46.    this.direct = 0;
  47. }
  48. // 移动模块
  49. function move()
  50. {
  51.    // 根据前进方向计算头部的坐标
  52.    switch(this.direct)
  53.    {
  54.       case 0 :
  55.          this.head[1] -= 1;
  56.          break;
  57.       case 1 :
  58.          this.head[0] += 1;
  59.          break;
  60.       case 2 :
  61.          this.head[1] += 1;
  62.          break;
  63.       case 3 :
  64.          this.head[0] -= 1;
  65.          break;
  66.    }
  67.    // 判断是否越界
  68.    if(this.head[0] < 0 || this.head[0] > 39 || this.head[1] < 0 || this.head[1] > 39)
  69.    {
  70.       // 如果越界则返回false
  71.       return false;
  72.    }
  73.    else
  74.    // 如果没有越界则检查下一个元素的性质,如果是食物则吃掉,并再生成食物。如果是其自身则返回false
  75.    if(this.head[0] == food[0] && this.head[1] == food[1])
  76.    {
  77.       // 如果是食物
  78.       rowindex[this.head[0]][this.head[1]].style.backgroundColor = this.bodycolor;
  79.       this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);
  80.       score++;
  81.       makefood();
  82.       return true;
  83.    }
  84.    else
  85.    // 如果是它自身
  86.    if(rowindex[this.head[0]][this.head[1]].style.backgroundColor == this.bodycolor)
  87.    {
  88.       if(rowindex[this.head[0]][this.head[1]] == this.bodys.pop())// 如果是它的尾部
  89.       {
  90.          this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);
  91.          return true;
  92.       }
  93.       // 如果不是尾部
  94.       return false;
  95.    }
  96.    // 以上情况都不是
  97.    this.bodys.pop().style.backgroundColor = backcolor;
  98.    rowindex[this.head[0]][this.head[1]].style.backgroundColor = this.bodycolor;
  99.    this.bodys.unshift(rowindex[this.head[0]][this.head[1]]);
  100.    return true;
  101. }
  102. snake.prototype.move = move;
  103. // 生成食物模块
  104. var foodcolor = "blue";
  105. var food = [20, 17];
  106. rowindex[food[0]][food[1]].style.backgroundColor = foodcolor;
  107. function makefood()
  108. {
  109.    var tempfood;
  110.    var tempelement;
  111.    out :
  112.    while(true)
  113.    {
  114.       tempfood = [Math.round(Math.random() * 39), Math.round(Math.random() * 39)];
  115.       tempelement = rowindex[tempfood[0]][tempfood[1]];
  116.       for(var i in s.bodys)
  117.       {
  118.          if(s.bodys[i] == tempelement)
  119.          {
  120.             // 如果随机生成的食物在蛇的身体上,则跳出继续
  121.             continue out;
  122.          }
  123.          // 生成食物成功
  124.          break out;
  125.          
  126.       }
  127.    }
  128.    food = tempfood;
  129.    rowindex[food[0]][food[1]].style.backgroundColor = foodcolor;
  130. }
  131. // 转向模块和暂停模块
  132. document.onkeydown = turnorstop;
  133. function turnorstop(event)
  134. {
  135.    if(window.event != undefined)
  136.    {
  137.       if(parseInt(window.event.keyCode)==32)
  138.       {
  139.       alert("休息一下");
  140.       }
  141.       else
  142.       {
  143.          switch(parseInt(window.event.keyCode))
  144.          {
  145.             case 37 :
  146.             if(s.direct!=2)
  147.             s.direct = 0;
  148.             break;
  149.             case 38 :
  150.             if(s.direct!=1)
  151.             s.direct = 3;
  152.             break;
  153.             case 39 :
  154.             if(s.direct!=0)
  155.             s.direct = 2;
  156.             break;
  157.             case 40 :
  158.             if(s.direct!=3)
  159.             s.direct = 1;
  160.             break;
  161.          }
  162.       }
  163.    }
  164.    else
  165.    {
  166.       if(parseInt(event.which)==32)
  167.       {
  168.       alert("休息一下");
  169.       }
  170.       else
  171.       {
  172.          switch(parseInt(event.which))
  173.          {
  174.             case 37 :
  175.             if(s.direct!=2)
  176.             s.direct = 0;
  177.             break;
  178.             case 38 :
  179.             if(s.direct!=1)
  180.             s.direct = 3;
  181.             break;
  182.             case 39 :
  183.             if(s.direct!=0)
  184.             s.direct = 2;
  185.             break;
  186.             case 40 :
  187.             if(s.direct!=3)
  188.             s.direct = 1;
  189.             break;
  190.          }
  191.       }
  192.    }
  193. }
  194. // 启动游戏模块
  195. var s = new snake();
  196. var time = 60;//蛇的速度指数
  197. function startmove()
  198. {
  199.    if(s.move())
  200.    {
  201.       setTimeout(startmove, time);
  202.    }
  203.    else
  204.    {
  205.       alert("GAME OVER/n您的分数是:"+score+"分");
  206.    }
  207. }
  208. //分数设置
  209. var score = -1;
  210. //运行游戏
  211. startmove();

在网页中连接该JS文件即可。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值