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

开发 前端 游戏开发
第四回主要讲熊移动碰到边界时的反弹处理:一、写一个碰撞处理函数、二、在游戏循环GameLoop()尾部中加入检测边界函数,如下:

第四回主要讲熊移动碰到边界时的反弹处理

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

一、写一个碰撞处理函数

  1. function HasAnimalHitEdge()      
  2. {      
  3.     //熊碰到右边边界      
  4.     if(animal.x>screenWidth - animal.image.width)      
  5.     {      
  6.         if(horizontalSpeed > 0)//假如向右移动      
  7.             horizontalSpeed =-horizontalSpeed;//改变水平速度方向      
  8.     }      
  9.     //熊碰到左边边界      
  10.     if(animal.x<-10)      
  11.     {      
  12.         if(horizontalSpeed < 0)//假如向左移动      
  13.             horizontalSpeed = -horizontalSpeed;//改变水平速度方向      
  14.     }      
  15.     //熊碰到下面边界      
  16.     if(animal.y>screenHeight - animal.image.height)      
  17.     {      
  18.         //2秒钟后从新开始      
  19.         setTimeout(function(){      
  20.             horizontalSpeed = speed;      
  21.             verticalSpeed = -speed;      
  22.             animal.x = parseInt(screenWidth/2);      
  23.             animal.y = parseInt(screenHeight/2);      
  24.             gameLoop();      
  25.         }, 2000);      
  26.     }      
  27.     //熊碰到上边边界      
  28.     if(animal.y<0)      
  29.     {      
  30.         verticalSpeed = -verticalSpeed;      
  31.     }      
  32. }    

二、在游戏循环GameLoop()尾部中加入检测边界函数,如下

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

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

  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.     //加载图片         
  67.     function LoadImages()         
  68.     {         
  69.         mushroomImg.src = "images/mushroom.png";//蘑菇         
  70.         backgroundForestImg.src = "images/forest1.jpg";//森林背景图        
  71.         bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的      
  72.               
  73.         mushroom.image = mushroomImg;         
  74.         animal.image = bearEyesClosedImg;      
  75.     }       
  76.     function HasAnimalHitEdge()      
  77.     {      
  78.         //熊碰到右边边界      
  79.         if(animal.x>screenWidth - animal.image.width)      
  80.         {      
  81.             if(horizontalSpeed > 0)//假如向右移动      
  82.                 horizontalSpeed =-horizontalSpeed;//改变水平速度方向      
  83.         }      
  84.         //熊碰到左边边界      
  85.         if(animal.x<-10)      
  86.         {      
  87.             if(horizontalSpeed < 0)//假如向左移动      
  88.                 horizontalSpeed = -horizontalSpeed;//改变水平速度方向      
  89.         }      
  90.         //熊碰到下面边界      
  91.         if(animal.y>screenHeight - animal.image.height)      
  92.         {      
  93.             //2秒钟后从新开始      
  94.             setTimeout(function(){      
  95.                 horizontalSpeed = speed;      
  96.                 verticalSpeed = -speed;      
  97.                 animal.x = parseInt(screenWidth/2);      
  98.                 animal.y = parseInt(screenHeight/2);      
  99.                 gameLoop();      
  100.             }, 2000);      
  101.         }      
  102.         //熊碰到上边边界      
  103.         if(animal.y<0)      
  104.         {      
  105.             verticalSpeed = -verticalSpeed;      
  106.         }      
  107.     }      
  108.     //事件处理         
  109.     function AddEventHandlers()         
  110.     {         
  111.         //鼠标移动则蘑菇跟着移动         
  112.         $("#container").mousemove(function(e){         
  113.             mushroom.x = e.pageX - (mushroom.image.width/2);         
  114.         });          
  115.                  
  116.     }       
  117.     //初始化         
  118.     $(window).ready(function(){          
  119.         AddEventHandlers();//添加事件        
  120.         LoadImages();                 
  121.         ctx = document.getElementById('canvas').getContext('2d'); //获取2d画布            
  122.         screenWidth = parseInt($("#canvas").attr("width")); //画布宽度       
  123.         screenHeight = parseInt($("#canvas").attr("height"));         
  124.         //初始化蘑菇      
  125.         mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐标        
  126.         mushroom.y = screenHeight - 40;//蘑菇Y坐标         
  127.         //初始化熊      
  128.         animal.x = parseInt(screenWidth/2);      
  129.         animal.y = parseInt(screenHeight/2);       
  130.         setInterval(GameLoop, 10);         
  131.     });         
  132.        
  133.         
  134. </script>        
  135. </head>        
  136.         
  137. <body>        
  138.     <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">        
  139.         <canvas id="canvas" width="480" height="320" >       
  140.         浏览器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">请下载</a>支持html5的浏览器来观看       
  141.         </canvas>        
  142.     </div>        
  143.        </body>        
  144. </html>       

第四回就讲到这了,第五回讲熊碰撞蘑菇的事件

【编辑推荐】

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

2012-05-21 13:18:12

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 13:25:49

HTML5

2012-05-21 10:45:30

HTML5

2021-03-13 13:59:49

Python编码回测

2020-07-09 10:18:00

人工智能

2020-12-07 16:20:53

Python 开发编程语言

2012-11-05 10:48:14

敏捷测试软件测试

2020-03-31 19:22:04

微信实名制小游戏

2018-02-24 15:48:53

2015-11-17 11:15:23

2017-04-11 10:27:10

2013-10-31 11:03:02

2013年度IT博客大熊子川

2011-05-31 13:18:14

51CTO 熊平 新

2012-09-07 10:20:53

2020-11-25 08:24:13

人脸识别

2012-09-11 11:38:20

小游戏云计算
点赞
收藏

51CTO技术栈公众号