av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

在洛江等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需求定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè),洛江網(wǎng)站建設(shè)費(fèi)用合理。

預(yù)期達(dá)到的效果:http://www.html5china.com/html5games/mogu/index5.html

由于獎品特別多,而且是有序的,所以我們使用一個數(shù)組來裝所有獎品的位置

一、需要到的全局變量

 
 
 
  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. }     

三、定義獎品數(shù)據(jù)及實例

 
 
 
  1. //定義獎品數(shù)組Prizes和對象Prize,繼承游戲?qū)ο驡ameObject      
  2. var prizes = new Array();      
  3. function Prize() {};      
  4. Prize.prototype = new GameObject();//繼承游戲?qū)ο驡ameObject      
  5. Prize.prototype.row = 0;//獎品行位置      
  6. Prize.prototype.col = 0;//獎品列位置    

四、把獎品裝進(jìn)數(shù)組

 
 
 
  1. //創(chuàng)建獎品數(shù)組      
  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.             //裝入獎品數(shù)組,用來描繪      
  24.             prizes[count] = prize;      
  25.             count++;      
  26.         }      
  27.     }      
  28. }    

五、從數(shù)組中取出獎品并描繪

 
 
 
  1. //繪制獎品,把獎品顯示在畫布上      
  2. function DrawPrizes()      
  3. {      
  4.     for(var x=0; x
  5.     {      
  6.         currentPrize = prizes[x];      
  7.         ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);      
  8.     }      
  9. }    

六、在游戲循環(huán)GameLoop()中加入描繪獎品的函數(shù),如下

 
 
 
  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. //以當(dāng)前熊的中心位置為基準(zhǔn)      
  19.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  20. //根據(jù)當(dāng)前熊的角度輪換      
  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.         
  2.         
  3.         
  4.         
  5. 蘑菇動起來-html5中文網(wǎng)        
  6.      
  7.         
  8.         
  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;//水平速度,隨著熊的碰撞會發(fā)生改變      
  18.     var verticalSpeed = -speed; //垂直速度,開始肯定是要向上飄,所以要負(fù)數(shù),隨著熊的碰撞會發(fā)生改變      
  19.     var bearAngle = 2;//熊旋轉(zhuǎn)的速度      
  20.     var flowerImg = new Image();//獎品鮮花      
  21.     var leafImg = new Image();//獎品葉子      
  22.     var acornImg = new Image();//獎品橡子      
  23.      
  24.     //公用 定義一個游戲物體戲?qū)ο?nbsp;        
  25.     function GameObject()         
  26.     {         
  27.         this.x = 0;         
  28.         this.y = 0;         
  29.         this.image = null;         
  30.     }         
  31.              
  32.     //定義蘑菇Mushroom 繼承游戲?qū)ο驡ameObject         
  33.     function Mushroom() {};         
  34.     Mushroom.prototype = new GameObject();//游戲?qū)ο驡ameObject         
  35.     //蘑菇實例         
  36.     var mushroom = new Mushroom();        //循環(huán)描繪物體        
  37.            
  38.     //定義動物熊 Animal 繼承 游戲?qū)ο驡ameObject      
  39.     function Animal() {};      
  40.     Animal.prototype = new GameObject();//游戲?qū)ο驡ameObject      
  41.     Animal.prototype.angle = 0;//動物的角度,目前中(即作為動物它在屏幕上旋轉(zhuǎn)退回)      
  42.     //定義熊實例       
  43.     var animal = new Animal();      
  44.           
  45.     //定義獎品數(shù)組Prizes和對象Prize,繼承游戲?qū)ο驡ameObject      
  46.     var prizes = new Array();      
  47.     function Prize() {};      
  48.     Prize.prototype = new GameObject();//繼承游戲?qū)ο驡ameObject      
  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.         //以當(dāng)前熊的中心位置為基準(zhǔn)      
  70.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  71.         //根據(jù)當(dāng)前熊的角度輪換      
  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.         //鼠標(biāo)移動則蘑菇跟著移動         
  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是重疊的區(qū)域值      
  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.     //創(chuàng)建獎品數(shù)組      
  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.                 //裝入獎品數(shù)組,用來描繪      
  227.                 prizes[count] = prize;      
  228.                 count++;      
  229.             }      
  230.         }      
  231.     }      
  232.     //繪制獎品,把獎品顯示在畫布上      
  233.     function DrawPrizes()      
  234.     {      
  235.         for(var x=0; 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坐標(biāo)        
  250.         mushroom.y = screenHeight - 40;//蘑菇Y(jié)坐標(biāo)         
  251.         //初始化熊      
  252.         animal.x = parseInt(screenWidth/2);      
  253.         animal.y = parseInt(screenHeight/2);       
  254.         //初始化獎品      
  255.         InitPrizes();      
  256.         setInterval(GameLoop, 10);         
  257.     });         
  258.        
  259.         
  260.         
  261.         
  262.         
  263.         
  264.             
  265.                
  266.         瀏覽器不支持html5,請下載支持html5的瀏覽器來觀看       
  267.                 
  268.     
        
  •                
  •        
  • 第六回就講到這了,第七回講描繪熊碰到獎品,獎品消失的事件


    網(wǎng)頁題目:蘑菇與熊游戲開發(fā)第六回(繪制獎品)
    當(dāng)前URL:http://uogjgqi.cn/article/djoscig.html
    掃二維碼與項目經(jīng)理溝通

    我們在微信上24小時期待你的聲音

    解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流