蘑菇与熊游戏开发第七回(熊碰到奖品处理)

开发 前端 游戏开发
给奖品加上一个存是否被撞过的属性hit,默认值为false。当奖品撞到的时候。改变hit的值为true。描绘奖品的时候判断hit值是否有没撞到,被撞到的话就不描绘。

第七回主要讲熊撞到奖品之后,奖品消失

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

简单说下原理:

给奖品加上一个存是否被撞过的属性hit,默认值为false。当奖品撞到的时候。改变hit的值为true。描绘奖品的时候判断hit值是否有没撞到,被撞到的话就不描绘

一、给奖品加hit属性

  1. //定义奖品数组Prizes和对象Prize,继承游戏对象GameObject      
  2. var prizes = new Array();      
  3. function Prize() {};      
  4. Prize.prototype = new GameObject();//继承游戏对象GameObject      
  5. Prize.prototype.row = 0;//奖品行位置      
  6. Prize.prototype.col = 0;//奖品列位置      
  7. Prize.prototype.hit = false;//是否被撞过    

二、熊撞到奖品事件

  1. //撞到奖品      
  2. function HasAnimalHitPrize()      
  3. {      
  4.     //取出所有奖品      
  5.     for(var x=0; x<prizes.length; x++)      
  6.     {      
  7.         var prize = prizes[x];      
  8.         //假如没有碰撞过      
  9.         if(!prize.hit)      
  10.         {      
  11.             //判断碰撞      
  12.             if(CheckIntersect(prize, animal, 0))      
  13.             {      
  14.                 prize.hit = true;      
  15.                 //熊反弹下沉      
  16.                 verticalSpeed = speed;      
  17.             }      
  18.         }      
  19.     }      
  20. }    

三、在描绘奖品函数中加如判断是否有被碰撞 if(!prize.hit) ,没被撞过,则描绘出来

  1. //撞到奖品      
  2. function HasAnimalHitPrize()      
  3. {      
  4.     //取出所有奖品      
  5.     for(var x=0; x<prizes.length; x++)      
  6.     {      
  7.         var prize = prizes[x];      
  8.         //假如没有碰撞过,则描绘在画布上      
  9.         if(!prize.hit)      
  10.         {      
  11.             //判断碰撞      
  12.             if(CheckIntersect(prize, animal, 0))      
  13.             {      
  14.                 prize.hit = true;      
  15.                 //熊反弹下沉      
  16.                 verticalSpeed = speed;      
  17.             }      
  18.         }      
  19.     }      
  20. }    

#p#

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

 

  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.     var flowerImg = new Image();//奖品鲜花      
  21.     var leafImg = new Image();//奖品叶子      
  22.     var acornImg = new Image();//奖品橡子      
  23.      
  24.     //公用 定义一个游戏物体戏对象         
  25.     function GameObject()         
  26.     {         
  27.         this.x = 0;         
  28.         this.y = 0;         
  29.         this.image = null;         
  30.     }         
  31.              
  32.     //定义蘑菇Mushroom 继承游戏对象GameObject         
  33.     function Mushroom() {};         
  34.     Mushroom.prototype = new GameObject();//游戏对象GameObject         
  35.     //蘑菇实例         
  36.     var mushroom = new Mushroom();        //循环描绘物体        
  37.            
  38.     //定义动物熊 Animal 继承 游戏对象GameObject      
  39.     function Animal() {};      
  40.     Animal.prototype = new GameObject();//游戏对象GameObject      
  41.     Animal.prototype.angle = 0;//动物的角度,目前中(即作为动物它在屏幕上旋转退回)      
  42.     //定义熊实例       
  43.     var animal = new Animal();      
  44.           
  45.     //定义奖品数组Prizes和对象Prize,继承游戏对象GameObject      
  46.     var prizes = new Array();      
  47.     function Prize() {};      
  48.     Prize.prototype = new GameObject();//继承游戏对象GameObject      
  49.     Prize.prototype.row = 0;//奖品行位置      
  50.     Prize.prototype.col = 0;//奖品列位置      
  51.     Prize.prototype.hit = false;//是否被撞过      
  52.           
  53.     function GameLoop()         
  54.     {         
  55.         //清除屏幕         
  56.         ctx.clearRect(0, 0, screenWidth, screenHeight);         
  57.         ctx.save();         
  58.         //绘制背景         
  59.         ctx.drawImage(backgroundForestImg, 0, 0);         
  60.         //绘制蘑菇         
  61.         ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  62.         //绘制奖品      
  63.         DrawPrizes();             
  64.         //绘制熊      
  65.         //改变移动动物X和Y位置      
  66.         animal.x += horizontalSpeed;      
  67.         animal.y += verticalSpeed;      
  68.         //改变翻滚角度      
  69.         animal.angle += bearAngle;      
  70.         //以当前熊的中心位置为基准      
  71.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  72.         //根据当前熊的角度轮换      
  73.         ctx.rotate(animal.angle * Math.PI/180);      
  74.         //描绘熊      
  75.         ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  76.      
  77.         ctx.restore();      
  78.         //检测是否碰到边界      
  79.         HasAnimalHitEdge();      
  80.         //检测熊碰撞蘑菇      
  81.         HasAnimalHitMushroom();      
  82.         //检测熊碰撞奖品      
  83.         HasAnimalHitPrize();      
  84.         }         
  85.     //加载图片         
  86.     function LoadImages()         
  87.     {         
  88.         mushroomImg.src = "images/mushroom.png";//蘑菇         
  89.         backgroundForestImg.src = "images/forest1.jpg";//森林背景图        
  90.         bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的      
  91.         flowerImg.src = "images/flower.png";//奖品花      
  92.         acornImg.src = "images/acorn.png";//奖品橡子      
  93.         leafImg.src = "images/leaf.png";//奖品叶子      
  94.               
  95.         mushroom.image = mushroomImg;         
  96.         animal.image = bearEyesClosedImg;      
  97.     }       
  98.     //熊碰撞边界      
  99.     function HasAnimalHitEdge()      
  100.     {      
  101.         //熊碰到右边边界      
  102.         if(animal.x>screenWidth - animal.image.width)      
  103.         {      
  104.             if(horizontalSpeed > 0)//假如向右移动      
  105.                 horizontalSpeed =-horizontalSpeed;//改变水平速度方向      
  106.         }      
  107.         //熊碰到左边边界      
  108.         if(animal.x<-10)      
  109.         {      
  110.             if(horizontalSpeed < 0)//假如向左移动      
  111.                 horizontalSpeed = -horizontalSpeed;//改变水平速度方向      
  112.         }      
  113.         //熊碰到下面边界      
  114.         if(animal.y>screenHeight - animal.image.height)      
  115.         {      
  116.             //2秒钟后从新开始      
  117.             setTimeout(function(){      
  118.                 horizontalSpeed = speed;      
  119.                 verticalSpeed = -speed;      
  120.                 animal.x = parseInt(screenWidth/2);      
  121.                 animal.y = parseInt(screenHeight/2);      
  122.                 GameLoop();      
  123.             }, 2000);      
  124.         }      
  125.         //熊碰到上边边界      
  126.         if(animal.y<0)      
  127.         {      
  128.             verticalSpeed = -verticalSpeed;      
  129.         }      
  130.     }      
  131.     //事件处理         
  132.     function AddEventHandlers()         
  133.     {         
  134.         //鼠标移动则蘑菇跟着移动         
  135.         $("#container").mousemove(function(e){         
  136.             mushroom.x = e.pageX - (mushroom.image.width/2);         
  137.         });          
  138.                  
  139.     }       
  140.     //检测2个物体是否碰撞      
  141.     function CheckIntersect(object1, object2, overlap)      
  142.     {      
  143.         //    x-轴                      x-轴      
  144.         //  A1------>B1 C1              A2------>B2 C2      
  145.         //  +--------+   ^              +--------+   ^      
  146.         //  | object1|   | y-轴         | object2|   | y-轴      
  147.         //  |        |   |              |        |   |      
  148.         //  +--------+  D1              +--------+  D2      
  149.         //      
  150.         //overlap是重叠的区域值      
  151.         A1 = object1.x + overlap;      
  152.         B1 = object1.x + object1.image.width - overlap;      
  153.         C1 = object1.y + overlap;      
  154.         D1 = object1.y + object1.image.height - overlap;      
  155.            
  156.         A2 = object2.x + overlap;      
  157.         B2 = object2.x + object2.image.width - overlap;      
  158.         C2 = object2.y + overlap;      
  159.         D2 = object2.y + object2.image.width - overlap;      
  160.            
  161.         //假如他们在x-轴重叠      
  162.         if(A1 > A2 && A1 < B2     
  163.            || B1 > A2 && B1 < B2)      
  164.         {      
  165.             //判断y-轴重叠      
  166.             if(C1 > C2 && C1 < D1     
  167.            || D1 > C2 && D1 < D2)      
  168.             {      
  169.                 //碰撞      
  170.                 return true;      
  171.             }      
  172.            
  173.         }      
  174.         return false;      
  175.     }      
  176.     //动物碰撞蘑菇      
  177.     function HasAnimalHitMushroom()      
  178.     {      
  179.         //假如碰撞      
  180.         if(CheckIntersect(animal, mushroom, 5))      
  181.         {      
  182.             //假如碰撞的位置属于蘑菇的左下位置      
  183.             if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))      
  184.             {      
  185.                 horizontalSpeed = -speed;//反弹      
  186.             }      
  187.             //假如碰撞的位置属于蘑菇的左上位置      
  188.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))      
  189.             {      
  190.                 //反弹速度减半      
  191.                 horizontalSpeed = -speed/2;      
  192.             }      
  193.             //假如碰撞的位置属于蘑菇的右上位置      
  194.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))      
  195.             {      
  196.                 horizontalSpeed = speed/2;      
  197.             }      
  198.             else      
  199.             {      
  200.                 horizontalSpeed = speed;      
  201.             }      
  202.             verticalSpeed = -speed;//改变垂直速度。也即动物向上移动      
  203.            
  204.         }      
  205.     }      
  206.     //创建奖品数组      
  207.     function InitPrizes()      
  208.     {      
  209.         var count=0;      
  210.         //一共3行      
  211.         for(var x=0; x<3; x++)      
  212.         {      
  213.             //一共23列      
  214.             for(var y=0; y<23; y++)      
  215.             {      
  216.                 prize = new Prize();      
  217.                 if(x==0)      
  218.                     prize.image = flowerImg;//鲜花放在***行      
  219.                 if(x==1)      
  220.                     prize.image = acornImg;//豫子刚在第2行      
  221.                 if(x==2)      
  222.                     prize.image = leafImg;//叶子放在第3行      
  223.                           
  224.                 prize.row = x;      
  225.                 prize.col = y;      
  226.                 prize.x = 20 * prize.col + 10;//x轴位置      
  227.                 prize.y = 30 * prize.row + 20;//y轴位置      
  228.                 //装入奖品数组,用来描绘      
  229.                 prizes[count] = prize;      
  230.                 count++;      
  231.             }      
  232.         }      
  233.     }      
  234.     //绘制奖品,把奖品显示在画布上      
  235.     function DrawPrizes()      
  236.     {      
  237.         for(var x=0; x<prizes.length; x++)      
  238.         {      
  239.             currentPrize = prizes[x];                 
  240.             //假如没有被撞击,则描绘      
  241.             if(!currentPrize.hit)      
  242.             {      
  243.                 ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);      
  244.             }      
  245.         }      
  246.     }      
  247.     //撞到奖品      
  248.     function HasAnimalHitPrize()      
  249.     {      
  250.         //取出所有奖品      
  251.         for(var x=0; x<prizes.length; x++)      
  252.         {      
  253.             var prize = prizes[x];      
  254.             //假如没有碰撞过,则描绘在画布上      
  255.             if(!prize.hit)      
  256.             {      
  257.                 //判断碰撞      
  258.                 if(CheckIntersect(prize, animal, 0))      
  259.                 {      
  260.                     prize.hit = true;      
  261.                     //熊反弹下沉      
  262.                     verticalSpeed = speed;      
  263.                 }      
  264.             }      
  265.         }      
  266.     }      
  267.     //初始化         
  268.     $(window).ready(function(){          
  269.         AddEventHandlers();//添加事件        
  270.         LoadImages();                 
  271.         ctx = document.getElementById('canvas').getContext('2d'); //获取2d画布            
  272.         screenWidth = parseInt($("#canvas").attr("width")); //画布宽度       
  273.         screenHeight = parseInt($("#canvas").attr("height"));         
  274.         //初始化蘑菇      
  275.         mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐标        
  276.         mushroom.y = screenHeight - 40;//蘑菇Y坐标         
  277.         //初始化熊      
  278.         animal.x = parseInt(screenWidth/2);      
  279.         animal.y = parseInt(screenHeight/2);       
  280.         //初始化奖品      
  281.         InitPrizes();      
  282.         setInterval(GameLoop, 10);         
  283.     });         
  284.        
  285.         
  286. </script>        
  287. </head>        
  288.         
  289. <body>        
  290.     <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">        
  291.         <canvas id="canvas" width="480" height="320" >       
  292.         浏览器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">请下载</a>支持html5的浏览器来观看       
  293.         </canvas>        
  294.     </div>        
  295.        </body>        
  296. </html>       

第七回讲完,整个游戏功能的大概框架已经出来了,成功就在眼前~

后面我们的任务就是去完善这个简陋的游戏,比如说加开始按钮、熊的生命数、显示得分、奖品被碰到后旋转再消失、蘑菇被碰到后颤抖几下、音乐等等

讲到这了,相信大家已经对整个游戏的功能、流程差不多了解了。后面的回合就不讲那么详细了,浪费大家的时间是吧~

第八回,开始完善游戏,加开始按钮、生命数、现实得分

原味链接:http://www.html5china.com/course/20110101_1499.html

【编辑推荐】

  1. 蘑菇与熊游戏开发***回(游戏分析)
  2. 蘑菇与熊游戏开发第二回(让蘑菇动起来)
  3. 蘑菇与熊游戏开发第三回(让熊动起来)
  4. 蘑菇与熊游戏开发第四回(熊碰撞边界处理)
  5. 蘑菇与熊游戏开发第五回(熊碰撞蘑菇处理)
  6. 蘑菇与熊游戏开发第六回(绘制奖品)
  7. 蘑菇与熊游戏开发第八回(完善游戏)

 

责任编辑:张伟 来源: HTML5China
相关推荐

2012-05-21 13:18:12

HTML5

2012-05-21 13:11:51

HTML5

2012-05-21 13:25:49

HTML5

2012-05-21 10:53:30

HTML5

2012-05-21 14:08:21

HTML5

2012-05-21 10:40:13

HTML5

2012-05-21 10:45:30

HTML5

2020-07-09 10:18:00

人工智能

2021-03-13 13:59:49

Python编码回测

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

2013-10-31 11:03:02

2013年度IT博客大熊子川

2011-05-31 13:18:14

51CTO 熊平 新

2015-11-17 11:15:23

2017-04-11 10:27:10

2017-07-04 15:32:28

周年庆极速狂奔小米手机

2012-09-07 10:20:53

2020-11-25 08:24:13

人脸识别
点赞
收藏

51CTO技术栈公众号