北极熊的窝

————————————我的百度HI:烂柿饼

用户操作
[即时聊天] [发私信] [加为好友]
北极熊ID:anhulife
1197次访问,排名2万外,好友1人,关注者2人。
对新技术有一种莫名的“冲动”。正在学习中,希望能在未来的web应用领域有所发展。更希望能够推进技术改革 嘿嘿
anhulife的文章
原创 11 篇
翻译 0 篇
转载 0 篇
评论 5 篇
最近评论
ninjajie:很强大
你观察形状变形的条件很透彻!
也就是矩阵中-1的设定。
想问一下:shapeClass.prototype.move=_move;
中prototype是什么属性?
ninjajie:不错啊
真厉害!!
fejay:不错。收藏了
PrideRock:不错啊
biao:强!涡阳人就是强!
文章分类
收藏
    相册
    基于Flex Webservice 的天气预报
    杂物
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 贪吃蛇的javascript实现收藏

    新一篇: Javascript版俄罗斯方块(含Bug) | 旧一篇: 分享软件实习成果

    最近在学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文件即可。

    发表于 @ 2008年09月26日 16:54:00|评论(loading...)|收藏

    新一篇: Javascript版俄罗斯方块(含Bug) | 旧一篇: 分享软件实习成果

    评论

    #fejay 发表于2008-09-27 14:01:15  IP: 58.48.137.*
    不错。收藏了
    #ninjajie 发表于2008-10-16 22:56:34  IP: 218.69.185.*
    不错啊
    真厉害!!
    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 北极熊