原生js写的贪吃蛇网页版游戏特效

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<body><title>原生js写的贪吃蛇网页版游戏特效代码 demo by js.alixixi.com</title></body>
<script>
  Star = {
    init:function(){
      var bigDiv = this.appendEle(this.addStyle(this.creatEle(),
        {w:'900',h:'600',p:'absolute',t:10,l:500}));
      for(var i = 0; i<600/30;i++){
        Star.data.arrayAll[i] = [];
        for(var j = 0; j<900/30; j++){
          div = this.addStyle(this.creatEle(),{w:(!+[1,] ? 30 :28),h:(!+[1,] ? 30 :28),f:'left',border:'1px solid #666'});
          div.setAttribute('number', i*30+j)
          this.appendEle(div,bigDiv)
          Star.data.arrayAll[i][j] = div;
        }
      }
      bigDiv = this.appendEle(this.addStyle(this.creatEle(),
        {w:'900',h:'600',p:'absolute',t:10,l:500}));

      this.pushEleInSelect(Star.data.arrayAll[9][15],Star.data.arrayAll[9][14],Star.data.arrayAll[9][13])
      this.keyBoard.apply(this,arguments);
      this.appearPoint();
      this.leftGo();
    },
    appearPoint:function(){
      var arrayIn = [];
      var number;
      for(var i = 0; i<600; i++){
        if(!this.hasInArray(Star.data.arrayAll[parseInt(i/30)][i%30].getAttribute('number'),Star.data.arraySelect)){
          arrayIn.push(Star.data.arrayAll[i])
        }
      }
      Star.data.foodNumber = number = parseInt(Math.random()*arrayIn.length);
      this.giveColor(number)
    },
    giveColor:function(number){
      var div = Star.data.arrayAll[parseInt(number/30)][number%30];
      Star.timeInterval.timeB = setInterval(function(){
        if(div.className == 'shanshuo'){
          div.className = ''
          div.style.backgroundColor = '#fff'
        }
        else{
          div.className = 'shanshuo';
          div.style.backgroundColor = '#f00'
        }
      },500)
    },
    disappearColor:function(){
      clearInterval(Star.timeInterval.timeB);
      Star.data.arrayAll[parseInt(Star.data.foodNumber/30)][Star.data.foodNumber%30].style.backgroundColor = '#f00';
    },
    hasInArray:function(number,array){
      for(var i in array){
        if(array[i] instanceof Array){
          if(this.hasInArray(number,array[i])){
            return true;
          }
        }
        if(array[i].getAttribute && array[i].getAttribute('number') == number) return true;
      }
      return false;
    },
    keyBoard:function(){
      var self = this;
      document.onkeydown = function(e){
        e = e? e : window.event;
        switch(e.keyCode){
          case 37: if(Star.keycode == 37 || Star.keycode == 39){return;};  self.leftGo(); break;
          case 38: if(Star.keycode == 38 || Star.keycode == 40){return;};  self.upGo();break;
          case 39: if(Star.keycode == 37 || Star.keycode == 39){return;};  self.rightGo();break;
          case 40: if(Star.keycode == 38 || Star.keycode == 40){return;};  self.downGo();break;
        }
      }
    },
    leftGo:function(){
      var div, number , self = this;
      Star.keycode = 37;
      clearInterval(Star.timeInterval.timeA)
      Star.timeInterval.timeA = setInterval(function(){
        number = Star.data.arraySelect[0].getAttribute('number');
        if(number%30<=0 || self.hasInArray(number-1,Star.data.arraySelect)){
          self.guanle();
        }
        else{
          if(Star.data.foodNumber == number-1){
            self.pushEleInSelect(Star.data.arrayAll[parseInt(Star.data.foodNumber/30)][Star.data.foodNumber%30]);
            self.disappearColor();
            self.appearPoint();
          }
          else{
            div = Star.data.arraySelect.pop();
            div.style.background = '#fff';

            self.pushEleInSelect(Star.data.arrayAll[parseInt(number/30)][number%30-1]);
          }
        }
      },Star.timeInterval.speed)
    },
    upGo:function(){
      var div, number , self = this;
      Star.keycode = 38;
      clearInterval(Star.timeInterval.timeA)
      Star.timeInterval.timeA = setInterval(function(){
        number = parseInt(Star.data.arraySelect[0].getAttribute('number'));

        if(parseInt(number/30)<=0 || self.hasInArray(number-30,Star.data.arraySelect)){
          self.guanle();
        }
        else{
          if(Star.data.foodNumber == number-30){
            self.pushEleInSelect(Star.data.arrayAll[parseInt(Star.data.foodNumber/30)][Star.data.foodNumber%30]);
            self.disappearColor();
            self.appearPoint();
          }
          else{
            div = Star.data.arraySelect.pop();
            div.style.background = '#fff';
            self.pushEleInSelect(Star.data.arrayAll[parseInt(number/30)-1][number%30]);
          }
        }
      },Star.timeInterval.speed)
    },
    rightGo:function(){
      var div, number , self = this;
      Star.keycode = 39;
      clearInterval(Star.timeInterval.timeA)
      Star.timeInterval.timeA = setInterval(function(){
        number = parseInt(Star.data.arraySelect[0].getAttribute('number'));
        if(parseInt(number%30)>=29 || self.hasInArray(number+1,Star.data.arraySelect)){
          self.guanle();
        }
        else{
          if(Star.data.foodNumber == number+1){
            self.pushEleInSelect(Star.data.arrayAll[parseInt(Star.data.foodNumber/30)][Star.data.foodNumber%30]);
            self.disappearColor();
            self.appearPoint();
          }
          else{
            div = Star.data.arraySelect.pop();
            div.style.background = '#fff';
            self.pushEleInSelect(Star.data.arrayAll[parseInt(number/30)][number%30+1]);
          }
        }
      },Star.timeInterval.speed)
    },
    downGo:function(){
      var div, number , self = this;
      Star.keycode = 40;
      clearInterval(Star.timeInterval.timeA)
      Star.timeInterval.timeA = setInterval(function(){
        number = parseInt(Star.data.arraySelect[0].getAttribute('number'));
        if(parseInt(number/30)>=19 || self.hasInArray(number+30,Star.data.arraySelect)){
          self.guanle();
        }
        else{
          if(Star.data.foodNumber == number+30){
            self.pushEleInSelect(Star.data.arrayAll[parseInt(Star.data.foodNumber/30)][Star.data.foodNumber%30]);
            self.disappearColor();
            self.appearPoint();
          }
          else{
            div = Star.data.arraySelect.pop();
            div.style.background = '#fff';
            self.pushEleInSelect(Star.data.arrayAll[parseInt(number/30)+1][number%30]);
          }
        }
      },Star.timeInterval.speed)
    },
    guanle:function(){
      alert('撞墙了,总分:' + (Star.data.arraySelect.length-3) * parseInt(1000 / Star.timeInterval.speed));
      location.reload();
    },
    creatEle:function(tag){
      var tagName = tag || 'DIV'
      return document.createElement(tagName)
    },
    appendEle:function(ele,father){
      var father = father || document.body || document.documentElement
      father.appendChild(ele)
      return ele;
    },
    addStyle:function(ele,css){
      for(var i in css){
        switch(i){
          case 'b' : ele.style.background = css[i];       break;
          case 'l' : ele.style.left       = css[i]+'px';  break;
          case 'r' : ele.style.right      = css[i]+'px';  break;
          case 't' : ele.style.top        = css[i]+'px';  break;
          case 'd' : ele.style.down       = css[i]+'px';  break;
          case 'p' : ele.style.position   = css[i];       break;
          case 'w' : ele.style.width      = css[i]+'px';  break;
          case 'h' : ele.style.height     = css[i]+'px';  break;
          case 'f' : ele.style.cssFloat   = css[i];  ele.style.styleFloat   = css[i];     break;
          default  : ele.style[i]         = css[i];       break;
        }
      }
      return ele;
    },
    pushEleInSelect:function(){
      for(var i = 0; i<arguments.length; i++){
        Star.data.arraySelect = [arguments[i]].concat(Star.data.arraySelect)
        this.addStyle(arguments[i],{b:'#f00'})
      }
    }
  }
  Star.data={
    arrayAll : [],
    arraySelect:[],
    newPoint:null,
    foodNumber:0
  }
  Star.timeInterval={
    timeA:null,
    timeB:null
  }
  Star.keycode = 0;

  window.onload = function(){
    var select = Star.creatEle('select');
    var optionDefault = Star.creatEle('option');
    optionDefault.innerHTML = '请选择关卡'
    Star.appendEle(optionDefault,select)
    Star.addStyle(select,{w:200,h:30,p:'absolute',left:'40%',top:'40%'})
    for(var i = 0 ; i <10 ; i++){
      var option = Star.creatEle('option');
      option.innerHTML = '第' + (i+1) + '关'
      Star.appendEle(option,select);
    }
    Star.appendEle(select)
    select.onchange = function(){
      selectValue = select.options[select.selectedIndex].value || select.options[select.selectedIndex].innerHTML
      var number = selectValue.match(/\d+/)[0]
      Star.timeInterval.speed = parseInt(200/number);
      Star.addStyle(select,{display:'none'});
      Star.init();
    }
  }

</script>

效果:

时间: 2024-10-24 17:45:46

原生js写的贪吃蛇网页版游戏特效的相关文章

原生js实现的贪吃蛇网页版游戏完整实例

  本文实例讲述了原生js实现的贪吃蛇网页版游戏.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 7

原生js实现的贪吃蛇网页版游戏完整实例_javascript技巧

本文实例讲述了原生js实现的贪吃蛇网页版游戏.分享给大家供大家参考.具体实现方法如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>原生js写的贪吃蛇网页版游戏</title> </head> <body> </body> <sc

js贪吃蛇网页版游戏特效代码分享(挑战十关)_javascript技巧

js贪吃蛇网页版游戏特效,经测试图片切换过程非常酷,相信大家一定都玩过这个经典小游戏吧,但是它是怎么实现的呐,感兴趣的朋友快来学习学习吧 运行效果图:----------------------查看效果----------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. 为大家分享的js贪吃蛇网页版游戏特效代码如下 <body><title>js贪吃蛇网页版游戏特效</title></body> <script>

JS写的贪吃蛇游戏

JS贪吃蛇游戏,个人练习之用,放在这备份一下,   复制代码 代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS贪吃蛇-练习</t

JS写的贪吃蛇游戏(个人练习)_javascript技巧

JS贪吃蛇游戏,个人练习之用,放在这备份一下,   复制代码 代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS贪吃蛇-练习</t

Flash Actionscript制作贪吃蛇简陋版

全部代码如下: /************2005.5**************/ /*********贪吃蛇简陋版************/ /***********寒冰(胡乱编写)********/ var s_x = 0; var s_y = 0; var dir = [[-1, 0], [0, -1], [1, 0], [0, 1]]; //四个方向 var _dir; var c_x = (Stage.width-256)/2; var c_y = (Stage.height-256

java贪吃蛇极速版_java

本文为大家推荐了一款由java实现经典小游戏:贪吃蛇,相信大家都玩过,如何实现的呐? 效果图: 废话不多说,直接奉上代码: 1. public class GreedSnake { public static void main(String[] args) { SnakeModel model = new SnakeModel(20,30); SnakeControl control = new SnakeControl(model); SnakeView view = new SnakeVi

用c#写的贪吃蛇游戏,简简单单

这几天玩手机的贪吃蛇玩得入了迷,想写个在电脑上玩,也当作为编程练习. 贪吃蛇游戏需求:1.玩法:类似Nokia手机上的贪吃蛇那样,但是用键盘的方向键或是鼠标来控制蛇的运动.2.外观:为了降低难度,只需在一个窗体上写一个矩形表示贪吃蛇运动的场所,贪吃蛇就象Nokia手机上的那种,用几个方块组成就行.3.吃豆:蛇每吃一个豆,蛇的长度就增一,分数加5分.(好像是没有吃豆的蛇)4.死亡:当蛇碰撞到墙壁时或自身身体时就死亡. 运行环境和编程语言由于这段时间在学c#,自然的就想到用c#语言来写这个小游戏.,

贪吃蛇 Java版(基于GUI)

完全自己写的小程序,主要难点在控制蛇身的运动上,已经较好的解决,不完善的地方是没有进行画面双缓冲,但刷新频率较低,所以闪烁现象较不明显. 界面如下: import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.ArrayList; public class SnakeClient extends Frame { /** * 贪吃蛇主客户端 * 蛇头可动,鸡蛋随机出现 * 0.4蛇身跟随着运