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

蘑菇與熊游戲開發(fā)第八回(完善游戲)

第八回合中主要是完善游戲,給游戲加上開始按鈕、生命數(shù)、得分

成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、成都網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團(tuán)成都企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。核心團(tuán)隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:成都食品包裝袋等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致贊揚!

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

一、添加開始按鈕

A、html代碼中加入開始按鈕,并定位他于畫布的中間

 
 
 
  1.      

開始圖片下載地址:http://www.html5china.com/html5games/mogu/images/START-Button.png

B、全局變量

 
 
 
  1. var gameRunning = false;//游戲運行狀態(tài)      
  2. var gameloopId;//記住循環(huán)的變量    

C、原來是游戲自己開始沒有暫停的、寫一個開始暫停的函數(shù)

 
 
 
  1. //開始或者暫停游戲      
  2. function ToggleGameplay()      
  3. {      
  4.     gameRunning = !gameRunning;      
  5.     if(gameRunning)      
  6.     {      
  7.         $("#BtnImgStart").hide();      
  8.          gameloopId = setInterval(GameLoop, 10);       
  9.     }else      
  10.     {      
  11.         clearInterval(gameloopId);      
  12.     }      
  13. }    

D、添加開始按鈕事件

 
 
 
  1. //事件處理         
  2. function AddEventHandlers()         
  3. {         
  4.     //鼠標(biāo)移動則蘑菇跟著移動         
  5.     $("#container").mousemove(function(e){         
  6.         mushroom.x = e.pageX - (mushroom.image.width/2);         
  7.     });       
  8.     //開始按鈕         
  9.     $("#BtnImgStart").click(function (){              
  10.         ToggleGameplay();      
  11.     });      
  12. }     

注意,要把$(window).ready(function(){})函數(shù)中的setInterval(GameLoop, 10);去掉

#p#

二、添加生命數(shù)條

A、需要的全局變量

 
 
 
  1. var lives=3;//3條生命數(shù)      
  2. var livesImages = new Array();//生命圖片數(shù)組    

B、生命圖片初始化

 
 
 
  1. //生命圖片因為只有6個,所以最多只能6個      
  2. for(var x=0; x<6; x++)      
  3. {      
  4.     livesImages[x] = new Image();         
  5.     livesImages[x].src = "images/lives" + x + ".png";      
  6. }    

C、繪制生命條

 
 
 
  1. //描繪生命條      
  2. function DrawLives()      
  3. {      
  4.     ctx.drawImage(livesImages[lives], 0, 0);      
  5. }    

D、當(dāng)熊碰到底線時,減一條生命

 
 
 
  1. //熊碰到下面邊界      
  2. if(animal.y>screenHeight - animal.image.height)      
  3. {      
  4.     lives -=1;//生命減1     
  5.     //當(dāng)還有生命條時,暫停游戲,熊回到中間位置,顯出開始按鈕    
  6.     if(lives>0)      
  7.     {      
  8.         horizontalSpeed = speed; //初始化水平速度     
  9.         verticalSpeed = -speed; //初始化垂直速度    
  10.         animal.x = parseInt(screenWidth/2); //初始化熊的x坐標(biāo)    
  11.         animal.y = parseInt(screenHeight/2); //初始化熊的y坐標(biāo)      
  12.         $("#BtnImgStart").show(); //顯示開始按鈕     
  13.         ToggleGameplay(); //暫停游戲     
  14.         GameLoop(); //初始化繪圖     
  15.     }      
  16. }    

E、當(dāng)生命條數(shù)等于0或者獎品消滅完,游戲結(jié)束

 
 
 
  1. //結(jié)束游戲      
  2. function GameOver()      
  3. {      
  4.     gameRunning = false;      
  5.     clearInterval(gameloopId);      
  6.     alert("游戲結(jié)束!");      
  7. }    

在熊撞到底線的代碼中,加入判斷,當(dāng)生命數(shù)=0時,游戲結(jié)束

 
 
 
  1. if(lives<=0)      
  2.     GameOver();    

在繪制獎品函數(shù)中加入判斷,當(dāng)獎品被消滅完時,游戲結(jié)束

 
 
 
  1. //繪制獎品,把獎品顯示在畫布上      
  2. function DrawPrizes()      
  3. {      
  4.     for(var x=0; x
  5.     {      
  6.         currentPrize = prizes[x];                 
  7.         //假如沒有被撞擊,則描繪      
  8.         if(!currentPrize.hit)      
  9.         {      
  10.             ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);      
  11.         }      
  12.     }      
  13.     if(AllPrizesHit())      
  14.     {      
  15.         GameOver();      
  16.     }      
  17. }      
  18. //判斷是否撞完獎品,假如其中有一個      
  19. function AllPrizesHit()      
  20. {      
  21.     for(var c=0; c
  22.     {      
  23.         checkPrize = prizes[c];      
  24.         if(checkPrize.hit == false)      
  25.             return false;      
  26.                   
  27.     }      
  28.     return true;      
  29. }    

#p#

三、添加得分

A、定義全局變量

 
 
 
  1. var score = 0;//分?jǐn)?shù)      
  2. var scoreImg = new Image();//分?jǐn)?shù)板    

B、初始化分?jǐn)?shù)板

 
 
 
  1. scoreImg.src = "images/score.png";//分?jǐn)?shù)板    

C、給獎品加一個分?jǐn)?shù)屬性。這樣在撞獎品時才能知道得到多少分

 
 
 
  1. function Prize() {};      
  2. Prize.prototype = new GameObject();//繼承游戲?qū)ο驡ameObject      
  3. Prize.prototype.row = 0;//獎品行位置      
  4. Prize.prototype.col = 0;//獎品列位置      
  5. Prize.prototype.hit = false;//是否被撞過      
  6. Prize.prototype.point = 0;//分?jǐn)?shù)    

D、在初始化獎品數(shù)組中加入分?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.             {      
  14.                 prize.image = flowerImg;//鮮花放在***行      
  15.                 prize.point = 3;//鮮花3分      
  16.             }      
  17.             if(x==1)      
  18.             {      
  19.                 prize.image = acornImg;//豫子剛在第2行      
  20.                 prize.point = 2;//橡子2分      
  21.             }      
  22.             if(x==2)      
  23.             {      
  24.                 prize.image = leafImg;//葉子放在第3行      
  25.                 prize.point = 1;//葉子1分      
  26.             }      
  27.                       
  28.             prize.row = x;      
  29.             prize.col = y;      
  30.             prize.x = 20 * prize.col + 10;//x軸位置      
  31.             prize.y = 20 * prize.row + 40;//y軸位置      
  32.             //裝入獎品數(shù)組,用來描繪      
  33.             prizes[count] = prize;      
  34.             count++;      
  35.         }      
  36.     }      
  37. }    

E、當(dāng)熊撞到獎品時,根據(jù)碰撞獎品的分?jǐn)?shù)增加總分

 
 
 
  1. //撞到獎品      
  2. function HasAnimalHitPrize()      
  3. {      
  4.     //取出所有獎品      
  5.     for(var x=0; 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.                 score += prize.point;      
  19.             }      
  20.         }      
  21.     }      
  22.      
  23. }    

F、繪制分?jǐn)?shù)

 
 
 
  1. //描繪分?jǐn)?shù)      
  2. function DrawScore()      
  3. {      
  4.     ctx.drawImage(scoreImg, screenWidth-(scoreImg.width),0);//分?jǐn)?shù)板      
  5.     ctx.font = "12pt Arial";      
  6.     ctx.fillText("" + score, 425, 25);  //得分      
  7. }    

#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.     var gameRunning = false;//游戲運行狀態(tài)      
  24.     var gameloopId;//記住循環(huán)的變量      
  25.     var lives=3;//3條生命數(shù)      
  26.     var livesImages = new Array();//生命圖片數(shù)組      
  27.     var score = 0;//分?jǐn)?shù)      
  28.     var scoreImg = new Image();//分?jǐn)?shù)板      
  29.     //公用 定義一個游戲物體戲?qū)ο?nbsp;        
  30.     function GameObject()         
  31.     {         
  32.         this.x = 0;         
  33.         this.y = 0;         
  34.         this.image = null;         
  35.     }         
  36.              
  37.     //定義蘑菇Mushroom 繼承游戲?qū)ο驡ameObject         
  38.     function Mushroom() {};         
  39.     Mushroom.prototype = new GameObject();//游戲?qū)ο驡ameObject         
  40.     //蘑菇實例         
  41.     var mushroom = new Mushroom();        //循環(huán)描繪物體        
  42.            
  43.     //定義動物熊 Animal 繼承 游戲?qū)ο驡ameObject      
  44.     function Animal() {};      
  45.     Animal.prototype = new GameObject();//游戲?qū)ο驡ameObject      
  46.     Animal.prototype.angle = 0;//動物的角度,目前中(即作為動物它在屏幕上旋轉(zhuǎn)退回)      
  47.     //定義熊實例       
  48.     var animal = new Animal();      
  49.           
  50.     //定義獎品數(shù)組Prizes和對象Prize,繼承游戲?qū)ο驡ameObject      
  51.     var prizes = new Array();      
  52.     function Prize() {};      
  53.     Prize.prototype = new GameObject();//繼承游戲?qū)ο驡ameObject      
  54.     Prize.prototype.row = 0;//獎品行位置      
  55.     Prize.prototype.col = 0;//獎品列位置      
  56.     Prize.prototype.hit = false;//是否被撞過      
  57.     Prize.prototype.point = 0;//分?jǐn)?shù)      
  58.           
  59.     function GameLoop()         
  60.     {         
  61.         //清除屏幕         
  62.         ctx.clearRect(0, 0, screenWidth, screenHeight);         
  63.         ctx.save();         
  64.         //繪制背景         
  65.         ctx.drawImage(backgroundForestImg, 0, 0);         
  66.         //繪制蘑菇         
  67.         ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  68.         //繪制獎品      
  69.         DrawPrizes();      
  70.         //繪制生命條      
  71.         DrawLives();      
  72.         //繪制分?jǐn)?shù)板      
  73.         DrawScore();              
  74.         //繪制熊      
  75.         //改變移動動物X和Y位置      
  76.         animal.x += horizontalSpeed;      
  77.         animal.y += verticalSpeed;      
  78.         //改變翻滾角度      
  79.         animal.angle += bearAngle;      
  80.         //以當(dāng)前熊的中心位置為基準(zhǔn)      
  81.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  82.         //根據(jù)當(dāng)前熊的角度輪換      
  83.         ctx.rotate(animal.angle * Math.PI/180);      
  84.         //描繪熊      
  85.         ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  86.      
  87.         ctx.restore();      
  88.         //檢測是否碰到邊界      
  89.         HasAnimalHitEdge();      
  90.         //檢測熊碰撞蘑菇      
  91.         HasAnimalHitMushroom();      
  92.         //檢測熊碰撞獎品      
  93.         HasAnimalHitPrize();      
  94.         }         
  95.     //加載圖片         
  96.     function LoadImages()         
  97.     {         
  98.         mushroomImg.src = "images/mushroom.png";//蘑菇         
  99.         backgroundForestImg.src = "images/forest1.jpg";//森林背景圖        
  100.         bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的      
  101.         flowerImg.src = "images/flower.png";//獎品花      
  102.         acornImg.src = "images/acorn.png";//獎品橡子      
  103.         leafImg.src = "images/leaf.png";//獎品葉子      
  104.         scoreImg.src = "images/score.png";//分?jǐn)?shù)板      
  105.         //生命圖片因為只有6個,所以最多只能6個      
  106.         for(var x=0; x<6; x++)      
  107.         {      
  108.             livesImages[x] = new Image();         
  109.             livesImages[x].src = "images/lives" + x + ".png";      
  110.         }      
  111.               
  112.         mushroom.image = mushroomImg;         
  113.         animal.image = bearEyesClosedImg;      
  114.         backgroundForestImg.onload = function(){GameLoop(); };      
  115.     }       
  116.     //熊碰撞邊界      
  117.     function HasAnimalHitEdge()      
  118.     {      
  119.         //熊碰到右邊邊界      
  120.         if(animal.x>screenWidth - animal.image.width)      
  121.         {      
  122.             if(horizontalSpeed > 0)//假如向右移動      
  123.                 horizontalSpeed =-horizontalSpeed;//改變水平速度方向      
  124.         }      
  125.         //熊碰到左邊邊界      
  126.         if(animal.x<-10)      
  127.         {      
  128.             if(horizontalSpeed < 0)//假如向左移動      
  129.                 horizontalSpeed = -horizontalSpeed;//改變水平速度方向      
  130.         }      
  131.         //熊碰到下面邊界      
  132.         if(animal.y>screenHeight - animal.image.height)      
  133.         {      
  134.             lives -=1;//生命減1      
  135.             if(lives>0)      
  136.             {      
  137.                 horizontalSpeed = speed;      
  138.                 verticalSpeed = -speed;      
  139.                 animal.x = parseInt(screenWidth/2);      
  140.                 animal.y = parseInt(screenHeight/2);      
  141.                 $("#BtnImgStart").show();      
  142.                 ToggleGameplay();      
  143.                 GameLoop();      
  144.             }      
  145.         }      
  146.         //熊碰到上邊邊界      
  147.         if(animal.y<0)      
  148.         {      
  149.             verticalSpeed = -verticalSpeed;      
  150.         }      
  151.         if(lives<=0)      
  152.             GameOver();      
  153.     }      
  154.     //事件處理         
  155.     function AddEventHandlers()         
  156.     {         
  157.         //鼠標(biāo)移動則蘑菇跟著移動         
  158.         $("#container").mousemove(function(e){         
  159.             mushroom.x = e.pageX - (mushroom.image.width/2);         
  160.         });       
  161.         //開始按鈕         
  162.         $("#BtnImgStart").click(function (){              
  163.             ToggleGameplay();      
  164.         });      
  165.     }       
  166.     //檢測2個物體是否碰撞      
  167.     function CheckIntersect(object1, object2, overlap)      
  168.     {      
  169.         //    x-軸                      x-軸      
  170.         //  A1------>B1 C1              A2------>B2 C2      
  171.         //  +--------+   ^              +--------+   ^      
  172.         //  | object1|   | y-軸         | object2|   | y-軸      
  173.         //  |        |   |              |        |   |      
  174.         //  +--------+  D1              +--------+  D2      
  175.         //      
  176.         //overlap是重疊的區(qū)域值      
  177.         A1 = object1.x + overlap;      
  178.         B1 = object1.x + object1.image.width - overlap;      
  179.         C1 = object1.y + overlap;      
  180.         D1 = object1.y + object1.image.height - overlap;      
  181.            
  182.         A2 = object2.x + overlap;      
  183.         B2 = object2.x + object2.image.width - overlap;      
  184.         C2 = object2.y + overlap;      
  185.         D2 = object2.y + object2.image.width - overlap;      
  186.            
  187.         //假如他們在x-軸重疊      
  188.         if(A1 > A2 && A1 < B2     
  189.            || B1 > A2 && B1 < B2)      
  190.         {      
  191.             //判斷y-軸重疊      
  192.             if(C1 > C2 && C1 < D1     
  193.            || D1 > C2 && D1 < D2)      
  194.             {      
  195.                 //碰撞      
  196.                 return true;      
  197.             }      
  198.            
  199.         }      
  200.         return false;      
  201.     }      
  202.     //動物碰撞蘑菇      
  203.     function HasAnimalHitMushroom()      
  204.     {      
  205.         //假如碰撞      
  206.         if(CheckIntersect(animal, mushroom, 5))      
  207.         {      
  208.             //假如碰撞的位置屬于蘑菇的左下位置      
  209.             if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))      
  210.             {      
  211.                 horizontalSpeed = -speed;//反彈      
  212.             }      
  213.             //假如碰撞的位置屬于蘑菇的左上位置      
  214.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))      
  215.             {      
  216.                 //反彈速度減半      
  217.                 horizontalSpeed = -speed/2;      
  218.             }      
  219.             //假如碰撞的位置屬于蘑菇的右上位置      
  220.             else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))      
  221.             {      
  222.                 horizontalSpeed = speed/2;      
  223.             }      
  224.             else      
  225.             {      
  226.                 horizontalSpeed = speed;      
  227.             }      
  228.             verticalSpeed = -speed;//改變垂直速度。也即動物向上移動      
  229.            
  230.         }      
  231.     }      
  232.     //創(chuàng)建獎品數(shù)組      
  233.     function InitPrizes()      
  234.     {      
  235.         var count=0;      
  236.         //一共3行   
    文章名稱:蘑菇與熊游戲開發(fā)第八回(完善游戲)
    網(wǎng)頁鏈接:http://uogjgqi.cn/article/codcspc.html
掃二維碼與項目經(jīng)理溝通

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

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