成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

蘑菇與熊游戲開發第六回(繪制獎品)

開發 前端 游戲開發
由于獎品特別多,而且是有序的,所以我們使用一個數組來裝所有獎品的位置。需要到的全局變量、初始化托全局變量、定義獎品數據及實例、把獎品裝進數組.....

第六回主要講怎么把獎品描繪上去

預期達到的效果: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

2012-03-16 20:37:20

2020-12-07 16:20:53

Python 開發編程語言

2010-02-22 09:48:57

Oracle埃里森

2013-12-04 16:21:02

Android游戲引擎libgdx教程

2012-09-21 15:23:27

Java項目Java開發

2011-05-24 10:45:56

開發

2011-10-19 13:32:33

開發

2011-05-23 14:53:46

PHP51技術沙龍

2010-06-29 19:23:20

UML活動圖
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 在线观看黄色电影 | 国产精品久久久久久久久久久久久 | 色久伊人| 欧美视频在线一区 | 成人亚洲精品久久久久软件 | 国产精品一区二区视频 | 精精国产xxxx视频在线野外 | 97免费在线观看视频 | 国产精品久久久久久久久图文区 | 777zyz色资源站在线观看 | 一区二区三区不卡视频 | 亚洲午夜av久久乱码 | www中文字幕 | 99久久久久久99国产精品免 | 黑人巨大精品欧美一区二区免费 | 国产蜜臀97一区二区三区 | 亚洲国产精品99久久久久久久久 | 中文一区 | 9191在线播放 | 国产成人精品一区二区三区 | 婷婷亚洲综合 | 五月天激情综合网 | 国产亚洲一区二区精品 | 欧美性生交大片免费 | 中文字幕一区二区三区日韩精品 | 81精品国产乱码久久久久久 | 天堂中文字幕av | 在线资源视频 | 国产精品一区在线 | 在线伊人网| 国产精品久久国产精品久久 | 亚洲欧美日韩久久 | a级网站 | 激情一区二区三区 | 免费久草| 亚洲精品麻豆 | 成年人黄色免费视频 | 欧美日韩亚洲一区 | 激情网站在线观看 | 不卡av在线 | 久久99精品久久久久久狂牛 |