相思资源网 Design By www.200059.com
本文实例讲述了原生js实现的贪吃蛇网页版游戏。分享给大家供大家参考。具体实现方法如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>原生js写的贪吃蛇网页版游戏</title> </head> <body> </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> </html>
希望本文所述对大家的javascript程序设计有所帮助。
标签:
js,贪吃蛇,游戏
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无原生js实现的贪吃蛇网页版游戏完整实例的评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。