蘑菇与熊游戏开发第六回(绘制奖品)

开发 前端 游戏开发
由于奖品特别多,而且是有序的,所以我们使用一个数组来装所有奖品的位置。需要到的全局变量、初始化托全局变量、定义奖品数据及实例、把奖品装进数组.....

第六回主要讲怎么把奖品描绘上去

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

由于奖品特别多,而且是有序的,所以我们使用一个数组来装所有奖品的位置

一、需要到的全局变量

  1. var flowerImg = new Image();//奖品鲜花      
  2. var leafImg = new Image();//奖品叶子      
  3. var acornImg = new Image();//奖品橡子     

鲜花图片下载:http://www.html5china.com/html5games/mogu/images/flower.png

叶子图片下载:http://www.html5china.com/html5games/mogu/images/leaf.png

橡子图片下载:http://www.html5china.com/html5games/mogu/images/acorn.png

二、初始化托全局变量

  1. //加载图片         
  2. function LoadImages()         
  3. {         
  4.     mushroomImg.src = "images/mushroom.png";//蘑菇         
  5.     backgroundForestImg.src = "images/forest1.jpg";//森林背景图        
  6.     bearEyesClosedImg.src = "images/bear_eyesclosed.png";//闭着眼睛的      
  7.     flowerImg.src = "images/flower.png";//奖品花      
  8.     acornImg.src = "images/acorn.png";//奖品橡子      
  9.     leafImg.src = "images/leaf.png";//奖品叶子      
  10.    
  11.     mushroom.image = mushroomImg;         
  12.     animal.image = bearEyesClosedImg;      
  13. }     

三、定义奖品数据及实例

  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;//奖品列位置    

四、把奖品装进数组

  1. //创建奖品数组      
  2. function InitPrizes()      
  3. {      
  4.     var count=0;      
  5.     //一共3行      
  6.     for(var x=0; x<3; x++)      
  7.     {      
  8.         //一共23列      
  9.         for(var y=0; y<23; y++)      
  10.         {      
  11.             prize = new Prize();      
  12.             if(x==0)      
  13.                 prize.image = flowerImg;//鲜花放在第一行      
  14.             if(x==1)      
  15.                 prize.image = acornImg;//豫子刚在第2行      
  16.             if(x==2)      
  17.                 prize.image = leafImg;//叶子放在第3行      
  18.                       
  19.             prize.row = x;      
  20.             prize.col = y;      
  21.             prize.x = 20 * prize.col + 10;//x轴位置      
  22.             prize.y = 30 * prize.row + 20;//y轴位置      
  23.             //装入奖品数组,用来描绘      
  24.             prizes[count] = prize;      
  25.             count++;      
  26.         }      
  27.     }      
  28. }    

五、从数组中取出奖品并描绘

  1. //绘制奖品,把奖品显示在画布上      
  2. function DrawPrizes()      
  3. {      
  4.     for(var x=0; x<prizes.length; x++)      
  5.     {      
  6.         currentPrize = prizes[x];      
  7.         ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);      
  8.     }      
  9. }    

六、在游戏循环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. DrawPrizes();             
  12. //绘制熊      
  13. //改变移动动物X和Y位置      
  14. animal.x += horizontalSpeed;      
  15. animal.y += verticalSpeed;      
  16. //改变翻滚角度      
  17. animal.angle += bearAngle;      
  18. //以当前熊的中心位置为基准      
  19.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  20. //根据当前熊的角度轮换      
  21.     ctx.rotate(animal.angle * Math.PI/180);      
  22. //描绘熊      
  23.     ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  24.      
  25.       ctx.restore();      
  26. //检测是否碰到边界      
  27. HasAnimalHitEdge();      
  28. //检测熊碰撞蘑菇      
  29. HasAnimalHitMushroom();      
  30.      
  31.       }       

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

第六回就讲到这了,第七回讲描绘熊碰到奖品,奖品消失的事件

原文链接:http://www.html5china.com/course/20110101_1498.html

【编辑推荐】

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

2012-05-21 13:32:45

HTML5

2012-05-21 13:18:12

HTML5

2012-05-21 14:08:21

HTML5

2012-05-21 13:11:51

HTML5

2012-05-21 10:40:13

HTML5

2012-05-21 10:53:30

HTML5

2012-05-21 10:45:30

HTML5

2013-05-20 16:12:23

2012-12-24 09:07:09

iOSUnity3D

2013-05-20 17:33:44

Android游戏开发自定义View

2013-12-04 16:21:02

Android游戏引擎libgdx教程

2020-12-07 16:20:53

Python 开发编程语言

2012-03-16 20:37:20

2010-02-22 09:48:57

Oracle埃里森

2012-09-21 15:23:27

Java项目Java开发

2010-03-16 19:41:15

无线AP无线产品D-Link

2010-06-29 19:23:20

UML活动图

2011-05-24 10:45:56

开发

2011-10-19 13:32:33

开发
点赞
收藏

51CTO技术栈公众号