JavaScript面向?qū)ο髮?shí)現(xiàn)貪吃蛇游戲
本文實(shí)例為大家分享了面向?qū)ο蟮呢澇陨邔?shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
1 工具對(duì)象(Tools.js)
因?yàn)橐S機(jī)生成食物,所以先將生成min-max范圍內(nèi)隨機(jī)整數(shù)的方法提取出來(lái)。randomNum屬性就是生成的隨機(jī)數(shù)。
function Tools(min,max){
? ? min = Math.ceil(min);
? ? max = Math.floor(max);
? ? this.randomNum=Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}2 食物對(duì)象(Food.js)
//為避免命名沖突,使用自調(diào)用函數(shù)
(function(){
? ? var foodDivs=[];
? //1 定義食物對(duì)象
? function Food(options){
? ? ? options=options||{};//封裝屬性的對(duì)象
? ? ? this.backgroundColor=options.backgroundColor||'green';
? ? ? this.width=options.width||20;
? ? ? this.height=options.height||20;
? ? ? this.x=options.x||0;
? ? ? this.y=options.y||0;
? }
? //2 渲染到map上
? Food.prototype.render=function(map){
? ? ? removeFood();
? ? ? var div=document.createElement('div');
? ? ? this.x=new Tools(0,map.offsetWidth/this.width-1).randomNum;
? ? ? this.y=new Tools(0,map.offsetHeight/this.height-1).randomNum;
? ? ? div.style.width=this.width+'px';
? ? ? div.style.height=this.height+'px';
? ? ? div.style.backgroundColor=this.backgroundColor;
? ? ? div.style.left=this.x*this.width+'px';
? ? ? div.style.top=this.y*this.height+'px';
? ? ? div.style.position = 'absolute';
? ? ? map.appendChild(div);
? ? ? foodDivs.push(div);
? }
? function removeFood(){
? ? for(var i=foodDivs.length-1;i>=0;i--){
? ? ? ? foodDivs[i].parentNode.removeChild(foodDivs[i]);//先從頁(yè)面刪除
? ? ? ? foodDivs.splice(i,1);//刪除指定位置元素 ?//再?gòu)臄?shù)組實(shí)際刪除
? ? }
? }
? window.Food=Food;//暴露Food,外部才能訪問(wèn)
})();3 蛇對(duì)象(Snake.js)
(function(){
? ? var snakeDivs=[];
? ? //1 蛇構(gòu)造函數(shù)
? ? function Snake(options){
? ? ? ? options=options||{};
? ? ? ? this.body=[
? ? ? ? ? ? {x:3,y:2,bgc:'red'},
? ? ? ? ? ? {x:2,y:2,bgc:'blue'},
? ? ? ? ? ? {x:1,y:2,bgc:'blue'}
? ? ? ? ];
? ? ? ? this.width=options.width||20;
? ? ? ? this.height=options.height||20; ? ? ?
? ? ? ? this.direction=options.direction||'right'; ? ? ?
? ? }
? ? //2 渲染到map上
? ? Snake.prototype.render=function(map){
? ? ? ? removeSnake();
? ? ? ? for(var i=0;i<this.body.length;i++){
? ? ? ? ? ? var block=this.body[i];
? ? ? ? ? ? var div=document.createElement('div');
? ? ? ? ? ? div.style.width=this.width+'px';
? ? ? ? ? ? div.style.height=this.height+'px';
? ? ? ? ? ? div.style.backgroundColor=block.bgc;
? ? ? ? ? ? div.style.left=block.x*this.width+'px';
? ? ? ? ? ? div.style.top=block.y*this.height+'px';
? ? ? ? ? ? div.style.position = 'absolute';
? ? ? ? ? ? map.appendChild(div);
? ? ? ? ? ? snakeDivs.push(div);//添加蛇divs
? ? ? ? }
? ? }
? ? //3 蛇的移動(dòng)
? ? Snake.prototype.move=function(food,map){
? ? ? ? //邏輯上從后往前 先蛇身再蛇頭
? ? ? ? //蛇身每一節(jié)替換上次的位置
? ? ? ? for(var i=this.body.length-1;i>0;i--){
? ? ? ? ? ? this.body[i].x=this.body[i-1].x;
? ? ? ? ? ? this.body[i].y=this.body[i-1].y;
? ? ? ? }
? ? ? ? //蛇頭移動(dòng)
? ? ? ? var head=this.body[0];
? ? ? ? switch(this.direction){
? ? ? ? ? ? ? case 'left':
? ? ? ? ? ? ? head.x-=1;
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? case 'up':
? ? ? ? ? ? ? head.y-=1;
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? case 'right':
? ? ? ? ? ? ? head.x+=1;
? ? ? ? ? ? ? break;
? ? ? ? ? ? ? case 'down':
? ? ? ? ? ? ? head.y+=1;
? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if(head.x===food.x&&head.y===food.y){//蛇頭與食物重合
? ? ? ? ? ? var last=this.body[this.body.length-1];
? ? ? ? ? ? this.body.push({
? ? ? ? ? ? ? ? x:last.x,
? ? ? ? ? ? ? ? y:last.y,
? ? ? ? ? ? ? ? bgc:last.bgc
? ? ? ? ? ? }); ?
? ? ? ? ? ? food.render(map);//這里就包括移除前面的食物這一操作 ? ? ? ? ??
? ? ? ? } ? ? ? ?
? ? }
? ? function removeSnake(){
? ? ? ?for(var i=snakeDivs.length-1;i>=0;i--){
? ? ? ? ? ?snakeDivs[i].parentNode.removeChild(snakeDivs[i]);//先從頁(yè)面刪除
? ? ? ? ? ?snakeDivs.splice(i,1);//刪除指定位置元素 ?//再?gòu)臄?shù)組實(shí)際刪除
? ? ? ?}
? ? }
? ? window.Snake=Snake;
})();4 游戲?qū)ο?Game.js)
主要是控制游戲的開(kāi)始,以及按鍵對(duì)應(yīng)的行為在游戲中的含義,將蛇和食物這兩個(gè)對(duì)象組織在一起。
?(function(){
var that;
?function Game(){
? ? ?this.food=new Food();
? ? ?this.snake=new Snake();
? ? ?that=this;
?}
?Game.prototype.start=function(map){
? ? this.snake.render(map);
? ? this.food.render(map);
? ? var head=this.snake.body[0];
? ? var span=document.getElementById('tag');
? ? var timerId=setInterval(function(){
? ? ? ?that.snake.move(that.food,map);
? ? ? ?that.snake.render(map);
? ? ? ?if((head.x>map.offsetWidth/that.snake.width-2)||head.x<=0){
? ? ? ? ?clearInterval(timerId);
? ? ? ? ?span.style.display='block';//提示Game Over
? ? ? ?}
? ? ? ?if((head.y>map.offsetHeight/that.snake.height-2)||head.y<=0){
? ? ? ? clearInterval(timerId);
? ? ? ? span.style.display='block';//提示Game Over
? ? ? }
? ? },150);
??
? ? console.log(map.offsetWidth/this.snake.width-1);
? ? bindKey();
}
function bindKey(){
? ? document.addEventListener('keydown',function(e){
? ? ? ? switch(e.keyCode){//left 37 up 38 right 39 down 40
? ? ? ? ? ? case 37:
? ? ? ? ? ? that.snake.direction='left';
? ? ? ? ? ? break;
? ? ? ? ? ? case 38:
? ? ? ? ? ? that.snake.direction='up';
? ? ? ? ? ? break;
? ? ? ? ? ? case 39:
? ? ? ? ? ? that.snake.direction='right';
? ? ? ? ? ? break;
? ? ? ? ? ? case 40:
? ? ? ? ? ? that.snake.direction='down';
? ? ? ? ? ? break;
? ? ? ? }
? ? });
}
window.Game=Game;
})()
var Game=new Game();
var map=document.getElementById('map');
Game.start(map);5 index.html
顯示的html頁(yè)面
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="utf-8" />
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <title>Page Title</title>
? ? <meta name="viewport" content="width=device-width, initial-scale=1">
? ? <style>
? ? #map{
? ? ? ? background-color: lightgray;
? ? ? ? width: 800px;
? ? ? ? height: 500px;
? ? ? ? margin: 0 auto;
? ? ? ? position: relative;
? ? }
? ? #tag{
? ? width: 200px;
? ? height: 100px;
? ? background-color: gray;
? ? position: absolute;
? ? left:300px;
? ? top:200px;
? ? line-height: 100px;
? ? text-align: center;
? ? display: none;
? ? }
? ? </style>
</head>
<body>
? ? <div id='map'>
? ? ? ? <span id='tag'>Game Over</span>
? ? </div>
? ? <script src="js/Tools.js"></script>
? ? <script src="js/Food.js"></script>
? ? <script src="js/Snake.js"></script>
? ? <script src="js/Game.js"></script>
</body>
</html>6 運(yùn)行界面

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS冷知識(shí)之不起眼但有用的String.raw方法
String.raw是JavaScript中模板字符串的一個(gè)標(biāo)簽函數(shù),它的作用是將模板字符串不轉(zhuǎn)義的原始字符串內(nèi)容返回,接下來(lái)通過(guò)本文給大家介紹JS冷知識(shí)之不起眼但有用的String.raw方法,需要的朋友可以參考下2022-06-06
學(xué)習(xí)drag and drop js實(shí)現(xiàn)代碼經(jīng)典之作
今天讀John Resig的Pro Javascript Techniques時(shí)候看到他書(shū)上給的一個(gè)關(guān)于drag and drop的例子, 合上書(shū)本自己寫(xiě)一個(gè)簡(jiǎn)化版本的。大約20分鐘完成, 沒(méi)有考慮兼容firefox。整個(gè)代碼封裝成一個(gè)對(duì)象 也是借鑒書(shū)中的風(fēng)格。我覺(jué)得很好。2009-04-04
uniapp使用百度地圖的保姆式教學(xué)(適合初學(xué)者!)
公司項(xiàng)目中有地圖展示和定位功能,所以下面這篇文章主要給大家介紹了關(guān)于uniapp使用百度地圖的保姆式教學(xué),文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
javascript removeChild 使用注意事項(xiàng)
removeChild的注意事項(xiàng)。大家可以參考下。2009-04-04
JS樹(shù)形菜單組件Bootstrap TreeView使用方法詳解
這篇文章主要為大家詳細(xì)介紹了js組件Bootstrap TreeView使用方法,本文一部分針對(duì)于bootstrap的treeview的實(shí)踐,另一部分是介紹自己寫(xiě)的樹(shù)形菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

