js实现简单的贪吃蛇


预览图:

1、食物的代码

 1 //食物的代码
 2   (function(){
 3     var element=[];
 4     function Food(width,height,color){
 5       this.width= width || 20;
 6       this.height=height || 20;
 7       this.color=color ||'red';
 8       this.x=0;
 9       this.y=0;
10     }
11     Food.prototype.init=function(map){
12       remove();
13       var div=document.createElement('div')
14       div.style.position='absolute'
15       div.style.width=this.width+'px'
16       div.style.height=this.height+'px'
17         div.style.backgroundColor =this.color
18       this.x=(Math.floor(Math.random()*map.offsetWidth/this.width)*this.width);
19        this.y=(Math.floor(Math.random()*map.offsetHeight/this.height)*this.height);
20        div.style.left= this.x+'px'; 
21        div.style.top= this.y+'px';
22       map.appendChild(div)
23       element.push(div)
24     }
25      function remove(){
26       element.forEach(function(item,i){
27         item.parentNode.removeChild(item);
28       })
29       element.length=0;
30     }
31     
32       window.Food=Food;
33 
34   }());
Food.js

2、蛇的代码

 1  //贪吃蛇的代码
 2  (function(){
 3    var element=[];
 4   function Snake(width,height,direction){
 5     this.width=width||20;
 6     this.height=height || 20;
 7     this.direction = direction ||'right';
 8     this.obj=[
 9     {x:3,y:2,color:'#2DF60C'},
10     {x:2,y:2,color:'#F06868'},
11     {x:1,y:2,color:'#F06868'}
12     ];
13   }
14   Snake.prototype.init=function(map){
15     remove();
16     for (var i = 0; i < this.obj.length; i++) {
17       var div=document.createElement('div')
18        map.appendChild(div)
19       div.style.position='absolute'
20       div.style.width=this.width+'px'
21       div.style.height=this.height+'px'
22 
23       div.style.backgroundColor =this.obj[i].color;
24       div.style.left=this.obj[i].x*this.width+'px';
25       div.style.top=this.obj[i].y*this.height+'px';
26      
27      
28       element.push(div)
29 
30     }
31     
32   }
33   Snake.prototype.move=function(food,map){
34   
35     for (var i = this.obj.length-1; i > 0;i--) {
36       this.obj[i].x=this.obj[i-1].x;
37       this.obj[i].y=this.obj[i-1].y;
38     }
39     switch(this.direction){
40       case 'right': this.obj[0].x+=1; break;
41       case 'left': this.obj[0].x-=1;  break;
42       case 'top': this.obj[0].y-=1;  break;
43       case 'bottom': this.obj[0].y+=1;  break;
44     }
45 
46       var headX=this.obj[0].x*this.width;  
47 
48       var headY=this.obj[0].y*this.height;
49     if(headX==food.x && headY == food.y){
50 
51       var last=this.obj[this.obj.length-1];
52       this.obj.push({
53         x:last.x,
54         y:last.y,
55         color:last.color
56         })
57       food.init(map);
58     }
59 
60   }
61 
62      function remove(){
63       for (var i = element.length-1; i>=0; i--) {
64 
65         element[i].parentNode.removeChild(element[i]);
66       }
67       
68       element.length=0;
69       }
70       window.Snake=Snake;
71  }());
Snake.js

3、游戏对象的代码

 1    //启动贪吃蛇的游戏对象
 2   (function(){
 3   var that=null;
 4   var timeId;
 5   function Game(map){
 6     this.food=new Food();
 7     this.snake=new Snake();
 8     this.map=map;
 9     that=this;
10   }
11   Game.prototype.init=function(){
12     this.food.init(this.map)
13     this.snake.init(this.map)
14     this.runSnake(this.food,this.map)
15     this.bindKey();
16   }
17   Game.prototype.runSnake=function(food,map){
18 
19    timeId= setInterval(function(){
20       this.snake.move(food,map);
21       var flag=isGameOver();
22       if(flag){
23         return;
24       }
25       this.snake.init(map);
26     }.bind(that),150)
27   }
28   Game.prototype.bindKey=function(){
29 
30     document.addEventListener('keydown',function(e){
31    
32       switch(e.keyCode){
33         case 37:
34           if(this.snake.direction==='right') return; 
35           this.snake.direction='left';  
36           break;
37         case 38:
38           if(this.snake.direction==='bottom') return; 
39           this.snake.direction='top'
40            break;
41         case 39:
42           if(this.snake.direction==='left') return;
43           this.snake.direction='right'
44            break;
45         case 40:
46           if(this.snake.direction==='top') return;
47           this.snake.direction='bottom';
48            break;
49       }
50     }.bind(that),false)
51 
52     }
53   function isGameOver(){
54     var maxX=that.map.offsetWidth / that.snake.width;
55     var maxY=that.map.offsetHeight / that.snake.height;
56     var head=that.snake.obj[0];
57     if(head.x<0 || head.x >= maxX || head.y< 0 || head.y >= maxY){
58       clearInterval(timeId)
59       alert('撞墙了');
60       return true;
61     }
62   }
63   window.Game=Game;
64  }());
Game.js

4、贪吃蛇执行文件的代码

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="UTF-8">
 5   <title>title</title>
 6   <style>
 7     .map {
 8       width: 800px;
 9       height: 600px;
10       background-color: #CCC;
11       position: relative;
12     }
13   </style>
14 </head>
15 <body>
16 <!--画出地图,设置样式-->
17 <div class="map"></div>
18 <script src="Food.js"></script>
19 <script src="Snake.js"></script>
20 <script src="Game.js"></script>
21 <script>
22 
23 
24   //初始化游戏对象
25   var gm = new Game(document.querySelector(".map"));
26 
27   //初始化游戏---开始游戏
28   gm.init();
29 
30   
31 
32 </script>
33 </body>
34 </html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM