自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

蘑菇與熊游戲開發(fā)第七回(熊碰到獎品處理)

開發(fā) 前端 游戲開發(fā)
給獎品加上一個存是否被撞過的屬性hit,默認值為false。當(dāng)獎品撞到的時候。改變hit的值為true。描繪獎品的時候判斷hit值是否有沒撞到,被撞到的話就不描繪。

第七回主要講熊撞到獎品之后,獎品消失

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

簡單說下原理:

給獎品加上一個存是否被撞過的屬性hit,默認值為false。當(dāng)獎品撞到的時候。改變hit的值為true。描繪獎品的時候判斷hit值是否有沒撞到,被撞到的話就不描繪

一、給獎品加hit屬性

  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;//獎品列位置      
  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. }    

三、在描繪獎品函數(shù)中加如判斷是否有被碰撞 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中文網(wǎng)</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;//水平速度,隨著熊的碰撞會發(fā)生改變      
  18.     var verticalSpeed = -speed; //垂直速度,開始肯定是要向上飄,所以要負數(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.     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.         //以當(dāng)前熊的中心位置為基準      
  71.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  72.         //根據(jù)當(dāng)前熊的角度輪換      
  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.         //鼠標(biāo)移動則蘑菇跟著移動         
  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是重疊的區(qū)域值      
  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.     //創(chuàng)建獎品數(shù)組      
  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.                 //裝入獎品數(shù)組,用來描繪      
  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坐標(biāo)        
  276.         mushroom.y = screenHeight - 40;//蘑菇Y(jié)坐標(biāo)         
  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>       

第七回講完,整個游戲功能的大概框架已經(jīng)出來了,成功就在眼前~

后面我們的任務(wù)就是去完善這個簡陋的游戲,比如說加開始按鈕、熊的生命數(shù)、顯示得分、獎品被碰到后旋轉(zhuǎn)再消失、蘑菇被碰到后顫抖幾下、音樂等等

講到這了,相信大家已經(jīng)對整個游戲的功能、流程差不多了解了。后面的回合就不講那么詳細了,浪費大家的時間是吧~

第八回,開始完善游戲,加開始按鈕、生命數(shù)、現(xiàn)實得分

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

【編輯推薦】

  1. 蘑菇與熊游戲開發(fā)***回(游戲分析)
  2. 蘑菇與熊游戲開發(fā)第二回(讓蘑菇動起來)
  3. 蘑菇與熊游戲開發(fā)第三回(讓熊動起來)
  4. 蘑菇與熊游戲開發(fā)第四回(熊碰撞邊界處理)
  5. 蘑菇與熊游戲開發(fā)第五回(熊碰撞蘑菇處理)
  6. 蘑菇與熊游戲開發(fā)第六回(繪制獎品)
  7. 蘑菇與熊游戲開發(fā)第八回(完善游戲)

 

責(zé)任編輯:張偉 來源: HTML5China
相關(guān)推薦

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 開發(fā)編程語言

2012-11-05 10:48:14

敏捷測試軟件測試

2020-03-31 19:22:04

微信實名制小游戲

2018-02-24 15:48:53

2017-07-04 15:32:28

周年慶極速狂奔小米手機

2011-05-31 13:18:14

51CTO 熊平 新

2013-10-31 11:03:02

2013年度IT博客大熊子川

2017-04-11 10:27:10

2015-11-17 11:15:23

2012-09-07 10:20:53

點贊
收藏

51CTO技術(shù)棧公眾號