Javascript基础示例:用JS写简易版贪吃蛇(面向对象)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Javascript基础示例:用JS写简易版贪吃蛇(面向对象),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4547字,纯文字阅读大概需要7分钟。
内容图文
废话不多说,代码如下:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>贪吃蛇</title> 6 <script> 7var map; //地图类对象 8var snake; //蛇类对象 9var food; //食物类对象 10var timer; //定时器对象 11var sum=0; //分数 12 13//地图类 14function Map() 15 { 16this.width=800; //地图宽度 17this.height=400; //地图高度 18this.position=‘absolute‘; //定位方式 19this.color=‘#cccccc‘; //地图颜色 20this._map=null; //保存地图dom元素 21 22this.show=function() 23 { 24//用于显示地图 25//创建地图div元素 26this._map = document.createElement(‘div‘); 27//设置地图样式 28this._map.style.width = this.width + ‘px‘; 29this._map.style.height = this.height + ‘px‘; 30this._map.style.position = this.position ; 31this._map.style.backgroundColor = this.color ; 32 33//将地图div元素追加到body标签之间 34 document.getElementsByTagName(‘body‘)[0].appendChild(this._map); 35 }; 36 } 37 38//食物类 39function Food() 40 { 41this.width=20; //宽度 42this.height=20; //高度 43this.position=‘absolute‘; //定位方式 44this.color=‘#00ff00‘; //食物颜色 45this._food=null; //用于保存食物dom元素 46this.x=0; //横向第几个格 47this.y=0; //纵向第几个格 48 49this.show=function() 50 { 51//用于显示食物 52if(this._food==null) 53 { 54this._food=document.createElement(‘div‘); 55 56//设置食物样式 57this._food.style.width = this.width + ‘px‘; 58this._food.style.height = this.height + ‘px‘; 59this._food.style.position = this.position ; 60this._food.style.backgroundColor = this.color ; 61 62 map._map.appendChild(this._food); 63 } 64//如果之前创建过,只需要重新设置坐标 65this.x=Math.floor(Math.random()*40); 66this.y=Math.floor(Math.random()*20); 67this._food.style.left = this.x*this.width+‘px‘; 68this._food.style.top = this.y*this.height+‘px‘; 69 }; 70 } 71 72//蛇类 73function Snake() 74 { 75this.width=20; //蛇节宽度 76this.height=20; //蛇节高度 77this.position=‘absolute‘; //蛇节定位方式 78this.direct=‘‘; //蛇的移动方向 79//所有蛇节全部信息 80this.body=[[3,2,‘red‘,null],[2,2,‘blue‘,null],[1,2,‘blue‘,null]]; 81 82this.setDirect = function(code) 83 { 84switch(code) 85 { 86case 37: 87this.direct=‘left‘; 88break; 89case 38: 90this.direct=‘up‘; 91break; 92case 39: 93this.direct=‘right‘; 94break; 95case 40: 96this.direct=‘down‘; 97break; 98 } 99 } 100101this.show=function() 102 { 103//用于显示蛇104for(var i=0;i<this.body.length;i++) 105 { 106if(this.body[i][3]==null) 107 { 108this.body[i][3] = document.createElement(‘div‘); 109this.body[i][3].style.width = this.width +‘px‘; 110this.body[i][3].style.height = this.height +‘px‘; 111this.body[i][3].style.position = this.position; 112this.body[i][3].style.backgroundColor = this.body[i][2]; 113 map._map.appendChild(this.body[i][3]); 114 } 115//设置蛇节的横纵坐标116this.body[i][3].style.left=this.body[i][0]*this.width+‘px‘; 117this.body[i][3].style.top=this.body[i][1]*this.height+‘px‘; 118 } 119 } 120121this.move = function() 122 { 123//移动蛇身124var length = this.body.length-1; 125for(var i=length;i>0;i--) 126 { 127//让后面的蛇节的坐标等于前面蛇节的坐标128this.body[i][0]=this.body[i-1][0]; //横坐标129this.body[i][1]=this.body[i-1][1]; //纵坐标130131 } 132switch(this.direct) 133 { 134case ‘right‘: 135this.body[0][0]=this.body[0][0]+1; 136break; 137case ‘down‘: 138this.body[0][1]=this.body[0][1]+1; 139break; 140case ‘left‘: 141this.body[0][0]=this.body[0][0]-1; 142break; 143case ‘up‘: 144this.body[0][1]=this.body[0][1]-1; 145break; 146default: 147return; 148 } 149150//判断蛇吃到食物151if(this.body[0][0]==food.x&&this.body[0][1]==food.y) 152 { 153var x=this.body[length][0]; 154var y=this.body[length][1]; 155 sum++; 156 document.title=‘分数:‘+sum+‘分‘; 157this.body.push([x,y,‘blue‘,null]); 158 food.show(); 159 } 160161//判断撞墙死162if(this.body[0][0]<0 || this.body[0][0]>39 ||this.body[0][1]<0 ||this.body[0][1]>19) 163 { 164 alert(‘撞墙死‘); 165 clearTimeout(timer); 166return; 167 } 168169//吃到自己死170for(var i=1;i<this.body.length;i++) 171 { 172if(this.body[0][0]==this.body[i][0]&&this.body[0][1]==this.body[i][1]) 173 { 174 alert(‘吃到自己死‘); 175 clearTimeout(timer); 176return; 177 } 178 } 179180this.show(); 181 } 182 } 183184 window.onload = function() 185 { 186 map = new Map(); //实例化地图类对象187 map.show(); //显示地图188189190 food=new Food(); //实例化食物类对象191 food.show(); //显示食物192193 snake = new Snake(); //实例化蛇类对象194 snake.show(); 195 timer = setInterval(‘snake.move()‘,100); 196197 document.onkeydown = function() 198 { 199var code; 200if(window.event) 201 { 202 code=window.event.keyCode; 203 }else204 { 205 code = event.keyCode; 206 } 207 snake.setDirect(code); 208 }; 209210211 } 212 </script> 213 </head> 214 <body> 215216 </body> 217 </html>
运行截图:
原文:http://www.cnblogs.com/guaidaodark/p/4471565.html
内容总结
以上是互联网集市为您收集整理的Javascript基础示例:用JS写简易版贪吃蛇(面向对象)全部内容,希望文章能够帮你解决Javascript基础示例:用JS写简易版贪吃蛇(面向对象)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。