蘑菇与熊游戏开发第五回(熊碰撞蘑菇处理)

开发 前端 游戏开发
一、由于碰撞的地方比较多,所以定义一个通用的判断2个物体是否碰撞的函数。二、熊碰撞蘑菇发生的事件以及处理。三、在游戏循环GameLoop()尾部中加入熊碰撞蘑菇函数。

第五回主要讲熊碰到蘑菇之后向上反弹的效果

预期达到的效果:http://www.html5china.com/html5games/mogu/index4.html

一、由于碰撞的地方比较多,所以定义一个通用的判断2个物体是否碰撞的函数

  1. //方法用途:检测2个物体是否碰撞      
  2. //参数object1:物体1      
  3. //参数object1:物体2      
  4. //参数overlap:可重叠的区域值      
  5. //返回布尔值:碰撞返回true,不碰撞返回false      
  6. function CheckIntersect(object1, object2, overlap)      
  7. {      
  8.     //    x-轴                      x-轴      
  9.     //  A1------>B1 C1              A2------>B2 C2      
  10.     //  +--------+   ^              +--------+   ^      
  11.     //  | object1|   | y-轴         | object2|   | y-轴      
  12.     //  |        |   |              |        |   |      
  13.     //  +--------+  D1              +--------+  D2      
  14.     //  看图可知两物体各4个点的位置      
  15.     A1 = object1.x + overlap;      
  16.     B1 = object1.x + object1.image.width - overlap;      
  17.     C1 = object1.y + overlap;      
  18.     D1 = object1.y + object1.image.height - overlap;      
  19.        
  20.     A2 = object2.x + overlap;      
  21.     B2 = object2.x + object2.image.width - overlap;      
  22.     C2 = object2.y + overlap;      
  23.     D2 = object2.y + object2.image.width - overlap;      
  24.        
  25.     //假如他们在x-轴重叠      
  26.     if(A1 > A2 && A1 < B2      
  27.        || B1 > A2 && B1 < B2)      
  28.     {      
  29.         //判断y-轴重叠      
  30.         if(C1 > C2 && C1 < D1      
  31.        || D1 > C2 && D1 < D2)      
  32.         {      
  33.             //碰撞      
  34.             return true;      
  35.         }      
  36.     }      
  37.     return false;      
  38. }    

二、熊碰撞蘑菇发生的事件以及处理

  1. //动物碰撞蘑菇      
  2. function HasAnimalHitMushroom()      
  3. {      
  4.     //假如碰撞      
  5.     if(CheckIntersect(animal, mushroom, 5))      
  6.     {      
  7.         //假如碰撞的位置属于蘑菇的左下位置      
  8.         if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))      
  9.         {      
  10.             horizontalSpeed = -speed;//反弹      
  11.         }      
  12.         //假如碰撞的位置属于蘑菇的左上位置      
  13.         else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))      
  14.         {      
  15.             //反弹速度减半      
  16.             horizontalSpeed = -speed/2;      
  17.         }      
  18.         //假如碰撞的位置属于蘑菇的右上位置      
  19.         else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))      
  20.         {      
  21.             horizontalSpeed = speed/2;      
  22.         }      
  23.         else     
  24.         {      
  25.             horizontalSpeed = speed;      
  26.         }      
  27.         verticalSpeed = -speed;//改变垂直速度。也即动物向上移动      
  28.        
  29.     }      
  30. }    

三、在游戏循环GameLoop()尾部中加入熊碰撞蘑菇函数,如下

  1. //游戏功能循环      
  2.    function GameLoop()         
  3.    {         
  4.        //清除屏幕         
  5.        ctx.clearRect(0, 0, screenWidth, screenHeight);         
  6.        ctx.save();         
  7.        //绘制背景         
  8.        ctx.drawImage(backgroundForestImg, 0, 0);         
  9.        //绘制蘑菇         
  10.        ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  11.     //绘制熊      
  12.     //改变移动动物X和Y位置      
  13.     animal.x += horizontalSpeed;      
  14.     animal.y += verticalSpeed;      
  15.     //改变翻滚角度      
  16.     animal.angle += bearAngle;      
  17.     //以当前熊的中心位置为基准      
  18.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  19.     //根据当前熊的角度轮换      
  20.     ctx.rotate(animal.angle * Math.PI/180);      
  21.     //描绘熊      
  22.     ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  23.        ctx.restore();      
  24.     //检测是否碰到边界      
  25.     HasAnimalHitEdge();      
  26.     //检测熊碰撞蘑菇      
  27.     HasAnimalHitMushroom();      
  28.        }       

到此第五回的完整代码如下:

  1. <!DOCTYPE>        
  2. <html>        
  3. <head>        
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        
  5. <title>蘑菇动起来-html5中文网</title>        
  6. <!-- 要记得引用jquery-1.4.2.js -->     
  7. <script type="text/javascript" src="./js/jquery-1.4.2.js"></script>        
  8. <script type="text/javascript" >        
  9.     //全局变量         
  10.     var backgroundForestImg = new Image();//森林背景图         
  11.     var mushroomImg = new Image();//蘑菇       
  12.     var bearEyesClosedImg = new Image();//闭着眼睛的熊熊       
  13.     var ctx;//2d画布         
  14.     var screenWidth;//画布宽度         
  15.     var screenHeight;//画布高度       
  16.     var speed = 2;//不变常量,从新开始的速度        
  17.     var horizontalSpeed = speed;//水平速度,随着熊的碰撞会发生改变      
  18.     var verticalSpeed = -speed; //垂直速度,开始肯定是要向上飘,所以要负数,随着熊的碰撞会发生改变      
  19.     var bearAngle = 2;//熊旋转的速度      
  20.     //公用 定义一个游戏物体戏对象         
  21.     function GameObject()         
  22.     {         
  23.         this.x = 0;         
  24.         this.y = 0;         
  25.         this.image = null;         
  26.     }         
  27.              
  28.     //定义蘑菇Mushroom 继承游戏对象GameObject         
  29.     function Mushroom() {};         
  30.     Mushroom.prototype = new GameObject();//游戏对象GameObject         
  31.     //蘑菇实例         
  32.     var mushroom = new Mushroom();        //循环描绘物体        
  33.            
  34.     //定义动物熊 Animal 继承 游戏对象GameObject      
  35.     function Animal() {};      
  36.     Animal.prototype = new GameObject();//游戏对象GameObject      
  37.     Animal.prototype.angle = 0;//动物的角度,目前中(即作为动物它在屏幕上旋转退回)      
  38.     //定义熊实例       
  39.     var animal = new Animal();      
  40.     //游戏功能循环      
  41.     function GameLoop()         
  42.     {         
  43.         //清除屏幕         
  44.         ctx.clearRect(0, 0, screenWidth, screenHeight);         
  45.         ctx.save();         
  46.         //绘制背景         
  47.         ctx.drawImage(backgroundForestImg, 0, 0);         
  48.         //绘制蘑菇         
  49.         ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  50.         //绘制熊      
  51.         //改变移动动物X和Y位置      
  52.         animal.x += horizontalSpeed;      
  53.         animal.y += verticalSpeed;      
  54.         //改变翻滚角度      
  55.         animal.angle += bearAngle;      
  56.         //以当前熊的中心位置为基准      
  57.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  58.         //根据当前熊的角度轮换      
  59.         ctx.rotate(animal.angle * Math.PI/180);      
  60.         //描绘熊      
  61.         ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  62.         ctx.restore();      
  63.         //检测是否碰到边界      
  64.         HasAnimalHitEdge();      
  65.         //检测熊碰撞蘑菇      
  66.         HasAnimalHitMushroom();      
  67.         }         
  68.     //加载图片         
  69.     function LoadImages()         
  70.     {         
  71.         mushroomImg.src = "images/mushroom.png";//蘑菇         
  72.         backgroundForestImg.src = "images/forest1.jpg";//森林背景图        
  73.         bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的      
  74.               
  75.         mushroom.image = mushroomImg;         
  76.         animal.image = bearEyesClosedImg;      
  77.     }       
  78.     //熊碰撞边界      
  79.     function HasAnimalHitEdge()      
  80.     {      
  81.         //熊碰到右边边界      
  82.         if(animal.x>screenWidth - animal.image.width)      
  83.         {      
  84.             if(horizontalSpeed > 0)//假如向右移动      
  85.                 horizontalSpeed =-horizontalSpeed;//改变水平速度方向      
  86.         }      
  87.         //熊碰到左边边界      
  88.         if(animal.x<-10)      
  89.         {      
  90.             if(horizontalSpeed < 0)//假如向左移动      
  91.                 horizontalSpeed = -horizontalSpeed;//改变水平速度方向      
  92.         }      
  93.         //熊碰到下面边界      
  94.         if(animal.y>screenHeight - animal.image.height)      
  95.         {      
  96.             //2秒钟后从新开始      
  97.             setTimeout(function(){      
  98.                 horizontalSpeed = speed;      
  99.                 verticalSpeed = -speed;      
  100.                 animal.x = parseInt(screenWidth/2);      
  101.                 animal.y = parseInt(screenHeight/2);      
  102.                 gameLoop();      
  103.             }, 2000);      
  104.         }      
  105.         //熊碰到上边边界      
  106.         if(animal.y<0)      
  107.         {      
  108.             verticalSpeed = -verticalSpeed;      
  109.         }      
  110.     }      
  111.     //事件处理         
  112.     function AddEventHandlers()         
  113.     {         
  114.         //鼠标移动则蘑菇跟着移动         
  115.         $("#container").mousemove(function(e){         
  116.             mushroom.x = e.pageX - (mushroom.image.width/2);         
  117.         });          
  118.                  
  119.     }       
  120.     //方法用途:检测2个物体是否碰撞      
  121.     //参数object1:物体1      
  122.     //参数object1:物体2      
  123.     //参数overlap:可重叠的区域值      
  124.     //返回布尔值:碰撞返回true,不碰撞返回false      
  125.     function CheckIntersect(object1, object2, overlap)      
  126.     {      
  127.         //    x-轴                      x-轴      
  128.         //  A1------>B1 C1              A2------>B2 C2      
  129.         //  +--------+   ^              +--------+   ^      
  130.         //  | object1|   | y-轴         | object2|   | y-轴      
  131.         //  |        |   |              |        |   |      
  132.         //  +--------+  D1              +--------+  D2      
  133.         //  看图可知两物体各4个点的位置      
  134.         A1 = object1.x + overlap;      
  135.         B1 = object1.x + object1.image.width - overlap;      
  136.         C1 = object1.y + overlap;      
  137.         D1 = object1.y + object1.image.height - overlap;      
  138.            
  139.         A2 = object2.x + overlap;      
  140.         B2 = object2.x + object2.image.width - overlap;      
  141.         C2 = object2.y + overlap;      
  142.         D2 = object2.y + object2.image.width - overlap;      
  143.            
  144.         //假如他们在x-轴重叠      
  145.         if(A1 > A2 && A1 < B2     
  146.            || B1 > A2 && B1 < B2)      
  147.         {      
  148.             //判断y-轴重叠      
  149.             if(C1 > C2 && C1 < D1     
  150.            || D1 > C2 && D1 < D2)      
  151.             {      
  152.                 //碰撞      
  153.                 return true;      
  154.             }      
  155.         }      
  156.         return false;      
  157.     }      
  158.     //动物碰撞蘑菇      
  159.     function HasAnimalHitMushroom()      
  160.     {      
  161.         //假如碰撞      
  162.         if(CheckIntersect(animal, mushroom, 5))      
  163.         {      
  164.             //假如碰撞的位置属于蘑菇的左下位置      
  165.             if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))      
  166.             {      
  167.                 horizontalSpeed = -speed;//反弹      
  168.             }      
  169.             //假如碰撞的位置属于蘑菇的左上位置      
  170.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))      
  171.             {      
  172.                 //反弹速度减半      
  173.                 horizontalSpeed = -speed/2;      
  174.             }      
  175.             //假如碰撞的位置属于蘑菇的右上位置      
  176.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))      
  177.             {      
  178.                 horizontalSpeed = speed/2;      
  179.             }      
  180.             else      
  181.             {      
  182.                 horizontalSpeed = speed;      
  183.             }      
  184.             verticalSpeed = -speed;//改变垂直速度。也即动物向上移动      
  185.            
  186.         }      
  187.     }      
  188.     //初始化         
  189.     $(window).ready(function(){          
  190.         AddEventHandlers();//添加事件        
  191.         LoadImages();                 
  192.         ctx = document.getElementById('canvas').getContext('2d'); //获取2d画布            
  193.         screenWidth = parseInt($("#canvas").attr("width")); //画布宽度       
  194.         screenHeight = parseInt($("#canvas").attr("height"));         
  195.         //初始化蘑菇      
  196.         mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐标        
  197.         mushroom.y = screenHeight - 40;//蘑菇Y坐标         
  198.         //初始化熊      
  199.         animal.x = parseInt(screenWidth/2);      
  200.         animal.y = parseInt(screenHeight/2);       
  201.         setInterval(GameLoop, 10);         
  202.     });         
  203.        
  204.         
  205. </script>        
  206. </head>        
  207.         
  208. <body>        
  209.     <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">        
  210.         <canvas id="canvas" width="480" height="320" >       
  211.         浏览器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">请下载</a>支持html5的浏览器来观看       
  212.         </canvas>        
  213.     </div>        
  214.        </body>        
  215. </html>       

第五回就讲到这了,第六回讲描绘奖品

【编辑推荐】

  1. 蘑菇与熊游戏开发第一回(游戏分析)
  2. 蘑菇与熊游戏开发第二回(让蘑菇动起来)
  3. 蘑菇与熊游戏开发第三回(让熊动起来)
  4. 蘑菇与熊游戏开发第四回(熊碰撞边界处理)
  5. 蘑菇与熊游戏开发第六回(绘制奖品)
  6. 蘑菇与熊游戏开发第七回(熊碰到奖品处理)
  7. 蘑菇与熊游戏开发第八回(完善游戏)
责任编辑:张伟 来源: HTML5China
相关推荐

2012-05-21 13:11:51

HTML5

2012-05-21 13:32:45

HTML5

2012-05-21 14:08:21

HTML5

2012-05-21 10:53:30

HTML5

2012-05-21 10:40:13

HTML5

2012-05-21 10:45:30

HTML5

2012-05-21 13:25:49

HTML5

2010-10-09 16:51:47

2017-10-13 17:35:30

深度学习移动端机器学习

2021-03-13 13:59:49

Python编码回测

2020-07-09 10:18:00

人工智能

2023-03-08 08:54:18

CPU内存语言

2020-12-07 16:20:53

Python 开发编程语言

2012-11-05 10:48:14

敏捷测试软件测试

2020-03-31 19:22:04

微信实名制小游戏

2023-03-20 13:21:49

计算机

2012-10-29 09:47:24

蘑菇街

2017-05-24 11:29:10

蘑菇街搜索推荐

2015-11-05 10:20:21

蘑菇街Docker私有云

2018-02-24 15:48:53

点赞
收藏

51CTO技术栈公众号