中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用html5實現蘑菇與熊游戲

發布時間:2021-11-18 13:31:15 來源:億速云 閱讀:146 作者:iii 欄目:web開發

這篇文章主要講解了“怎么用html5實現蘑菇與熊游戲”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用html5實現蘑菇與熊游戲”吧!

一、添加開始按鈕

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

<img id="BtnImgStart" style="position: absolute; left: 200px; top: 200px; cursor: pointer; float: left; display: block; " src="./images/START-Button.png">

B、全局變量

var gameRunning = false;//游戲運行狀態      var gameloopId;//記住循環的變量

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

//開始或者暫停游戲      function ToggleGameplay()      {          gameRunning = !gameRunning;          if(gameRunning)          {              $("#BtnImgStart").hide();               gameloopId = setInterval(GameLoop, 10);           }else          {              clearInterval(gameloopId);          }      }

D、添加開始按鈕事件

//事件處理         function AddEventHandlers()         {             //鼠標移動則蘑菇跟著移動             $("#container").mousemove(function(e){                 mushroom.x = e.pageX - (mushroom.image.width/2);             });           //開始按鈕             $("#BtnImgStart").click(function (){                      ToggleGameplay();          });      }

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

二、添加生命數條

A、需要的全局變量

var lives=3;//3條生命數      var livesImages = new Array();//生命圖片數組

B、生命圖片初始化

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

C、繪制生命條

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

D、當熊碰到底線時,減一條生命

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

E、當生命條數等于0或者獎品消滅完,游戲結束

//結束游戲      function GameOver()      {          gameRunning = false;          clearInterval(gameloopId);          alert("游戲結束!");      }

在熊撞到底線的代碼中,加入判斷,當生命數=0時,游戲結束

if(lives<=0)          GameOver();

在繪制獎品函數中加入判斷,當獎品被消滅完時,游戲結束

//繪制獎品,把獎品顯示在畫布上      function DrawPrizes()      {          for(var x=0; x<prizes.length; x++)          {              currentPrize = prizes[x];                         //假如沒有被撞擊,則描繪              if(!currentPrize.hit)              {                  ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);              }          }          if(AllPrizesHit())          {              GameOver();          }      }      //判斷是否撞完獎品,假如其中有一個      function AllPrizesHit()      {          for(var c=0; c<prizes.length; c++)          {              checkPrize = prizes[c];              if(checkPrize.hit == false)                  return false;                            }          return true;      }

三、添加得分

A、定義全局變量

var score = 0;//分數      var scoreImg = new Image();//分數板

B、初始化分數板

scoreImg.src = "images/score.png";//分數板

C、給獎品加一個分數屬性。這樣在撞獎品時才能知道得到多少分

function Prize() {};      Prize.prototype = new GameObject();//繼承游戲對象GameObject      Prize.prototype.row = 0;//獎品行位置      Prize.prototype.col = 0;//獎品列位置      Prize.prototype.hit = false;//是否被撞過      Prize.prototype.point = 0;//分數

D、在初始化獎品數組中加入分數

//創建獎品數組      function InitPrizes()      {          var count=0;          //一共3行          for(var x=0; x<3; x++)          {              //一共23列              for(var y=0; y<23; y++)              {                  prize = new Prize();                  if(x==0)                  {                      prize.image = flowerImg;//鮮花放在***行                      prize.point = 3;//鮮花3分                  }                  if(x==1)                  {                      prize.image = acornImg;//豫子剛在第2行                      prize.point = 2;//橡子2分                  }                  if(x==2)                  {                      prize.image = leafImg;//葉子放在第3行                      prize.point = 1;//葉子1分                  }                                        prize.row = x;                  prize.col = y;                  prize.x = 20 * prize.col + 10;//x軸位置                  prize.y = 20 * prize.row + 40;//y軸位置                  //裝入獎品數組,用來描繪                  prizes[count] = prize;                  count++;              }          }      }

E、當熊撞到獎品時,根據碰撞獎品的分數增加總分

//撞到獎品      function HasAnimalHitPrize()      {          //取出所有獎品          for(var x=0; x<prizes.length; x++)          {              var prize = prizes[x];              //假如沒有碰撞過              if(!prize.hit)              {                  //判斷碰撞                  if(CheckIntersect(prize, animal, 0))                  {                      prize.hit = true;                      //熊反彈下沉                      verticalSpeed = speed;                      //總分增加                      score += prize.point;                  }              }          }           }

F、繪制分數

//描繪分數      function DrawScore()      {          ctx.drawImage(scoreImg, screenWidth-(scoreImg.width),0);//分數板          ctx.font = "12pt Arial";          ctx.fillText("" + score, 425, 25);  //得分      }

綜合上面的代碼, 到此第八回的完整代碼如下:

<!DOCTYPE>        <html>        <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <title>繪制獎品-html5中文網</title>        <!-- 要記得引用jquery-1.4.2.js -->     <script type="text/javascript" src="./js/jquery-1.4.2.js"></script>        <script type="text/javascript" >            //全局變量             var backgroundForestImg = new Image();//森林背景圖             var mushroomImg = new Image();//蘑菇           var bearEyesClosedImg = new Image();//閉著眼睛的熊熊           var ctx;//2d畫布             var screenWidth;//畫布寬度             var screenHeight;//畫布高度           var speed = 2;//不變常量,從新開始的速度            var horizontalSpeed = speed;//水平速度,隨著熊的碰撞會發生改變          var verticalSpeed = -speed; //垂直速度,開始肯定是要向上飄,所以要負數,隨著熊的碰撞會發生改變          var bearAngle = 2;//熊旋轉的速度          var flowerImg = new Image();//獎品鮮花          var leafImg = new Image();//獎品葉子          var acornImg = new Image();//獎品橡子          var gameRunning = false;//游戲運行狀態          var gameloopId;//記住循環的變量          var lives=3;//3條生命數          var livesImages = new Array();//生命圖片數組          var score = 0;//分數          var scoreImg = new Image();//分數板          //公用 定義一個游戲物體戲對象             function GameObject()             {                 this.x = 0;                 this.y = 0;                 this.image = null;             }                          //定義蘑菇Mushroom 繼承游戲對象GameObject             function Mushroom() {};             Mushroom.prototype = new GameObject();//游戲對象GameObject             //蘑菇實例             var mushroom = new Mushroom();        //循環描繪物體                       //定義動物熊 Animal 繼承 游戲對象GameObject          function Animal() {};          Animal.prototype = new GameObject();//游戲對象GameObject          Animal.prototype.angle = 0;//動物的角度,目前中(即作為動物它在屏幕上旋轉退回)          //定義熊實例           var animal = new Animal();                    //定義獎品數組Prizes和對象Prize,繼承游戲對象GameObject          var prizes = new Array();          function Prize() {};          Prize.prototype = new GameObject();//繼承游戲對象GameObject          Prize.prototype.row = 0;//獎品行位置          Prize.prototype.col = 0;//獎品列位置          Prize.prototype.hit = false;//是否被撞過          Prize.prototype.point = 0;//分數                    function GameLoop()             {                 //清除屏幕                 ctx.clearRect(0, 0, screenWidth, screenHeight);                 ctx.save();                 //繪制背景                 ctx.drawImage(backgroundForestImg, 0, 0);                 //繪制蘑菇                 ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);               //繪制獎品              DrawPrizes();              //繪制生命條              DrawLives();              //繪制分數板              DrawScore();                      //繪制熊              //改變移動動物X和Y位置              animal.x += horizontalSpeed;              animal.y += verticalSpeed;              //改變翻滾角度              animal.angle += bearAngle;              //以當前熊的中心位置為基準              ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));              //根據當前熊的角度輪換              ctx.rotate(animal.angle * Math.PI/180);              //描繪熊              ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));                   ctx.restore();              //檢測是否碰到邊界              HasAnimalHitEdge();              //檢測熊碰撞蘑菇              HasAnimalHitMushroom();              //檢測熊碰撞獎品              HasAnimalHitPrize();              }             //加載圖片             function LoadImages()             {                 mushroomImg.src = "images/mushroom.png";//蘑菇                 backgroundForestImg.src = "images/forest1.jpg";//森林背景圖                bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的              flowerImg.src = "images/flower.png";//獎品花              acornImg.src = "images/acorn.png";//獎品橡子              leafImg.src = "images/leaf.png";//獎品葉子              scoreImg.src = "images/score.png";//分數板              //生命圖片因為只有6個,所以最多只能6個              for(var x=0; x<6; x++)              {                  livesImages[x] = new Image();                     livesImages[x].src = "images/lives" + x + ".png";              }                            mushroom.image = mushroomImg;                 animal.image = bearEyesClosedImg;              backgroundForestImg.onload = function(){GameLoop(); };          }           //熊碰撞邊界          function HasAnimalHitEdge()          {              //熊碰到右邊邊界              if(animal.x>screenWidth - animal.image.width)              {                  if(horizontalSpeed > 0)//假如向右移動                      horizontalSpeed =-horizontalSpeed;//改變水平速度方向              }              //熊碰到左邊邊界              if(animal.x<-10)              {                  if(horizontalSpeed < 0)//假如向左移動                      horizontalSpeed = -horizontalSpeed;//改變水平速度方向              }              //熊碰到下面邊界              if(animal.y>screenHeight - animal.image.height)              {                  lives -=1;//生命減1                  if(lives>0)                  {                      horizontalSpeed = speed;                      verticalSpeed = -speed;                      animal.x = parseInt(screenWidth/2);                      animal.y = parseInt(screenHeight/2);                      $("#BtnImgStart").show();                      ToggleGameplay();                      GameLoop();                  }              }              //熊碰到上邊邊界              if(animal.y<0)              {                  verticalSpeed = -verticalSpeed;              }              if(lives<=0)                  GameOver();          }          //事件處理             function AddEventHandlers()             {                 //鼠標移動則蘑菇跟著移動                 $("#container").mousemove(function(e){                     mushroom.x = e.pageX - (mushroom.image.width/2);                 });               //開始按鈕                 $("#BtnImgStart").click(function (){                          ToggleGameplay();              });          }           //檢測2個物體是否碰撞          function CheckIntersect(object1, object2, overlap)          {              //    x-軸                      x-軸              //  A1------>B1 C1              A2------>B2 C2              //  +--------+   ^              +--------+   ^              //  | object1|   | y-軸         | object2|   | y-軸              //  |        |   |              |        |   |              //  +--------+  D1              +--------+  D2              //              //overlap是重疊的區域值              A1 = object1.x + overlap;              B1 = object1.x + object1.image.width - overlap;              C1 = object1.y + overlap;              D1 = object1.y + object1.image.height - overlap;                         A2 = object2.x + overlap;              B2 = object2.x + object2.image.width - overlap;              C2 = object2.y + overlap;              D2 = object2.y + object2.image.width - overlap;                         //假如他們在x-軸重疊              if(A1 > A2 && A1 < B2                || B1 > A2 && B1 < B2)              {                  //判斷y-軸重疊                  if(C1 > C2 && C1 < D1                || D1 > C2 && D1 < D2)                  {                      //碰撞                      return true;                  }                         }              return false;          }          //動物碰撞蘑菇          function HasAnimalHitMushroom()          {              //假如碰撞              if(CheckIntersect(animal, mushroom, 5))              {                  //假如碰撞的位置屬于蘑菇的左下位置                  if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.25))                  {                      horizontalSpeed = -speed;//反彈                  }                  //假如碰撞的位置屬于蘑菇的左上位置                  else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.5))                  {                      //反彈速度減半                      horizontalSpeed = -speed/2;                  }                  //假如碰撞的位置屬于蘑菇的右上位置                  else if((animal.x + animal.image.width/2) < (mushroom.x + mushroom.image.width*0.75))                  {                      horizontalSpeed = speed/2;                  }                  else                  {                      horizontalSpeed = speed;                  }                  verticalSpeed = -speed;//改變垂直速度。也即動物向上移動                         }          }          //創建獎品數組          function InitPrizes()          {              var count=0;              //一共3行              for(var x=0; x<3; x++)              {                  //一共23列                  for(var y=0; y<23; y++)                  {                      prize = new Prize();                      if(x==0)                      {                          prize.image = flowerImg;//鮮花放在***行                          prize.point = 3;//3分                      }                      if(x==1)                      {                          prize.image = acornImg;//豫子剛在第2行                          prize.point = 2;//2分                      }                      if(x==2)                      {                          prize.image = leafImg;//葉子放在第3行                          prize.point = 1;//1分                      }                                                prize.row = x;                      prize.col = y;                      prize.x = 20 * prize.col + 10;//x軸位置                      prize.y = 20 * prize.row + 40;//y軸位置                      //裝入獎品數組,用來描繪                      prizes[count] = prize;                      count++;                  }              }          }          //繪制獎品,把獎品顯示在畫布上          function DrawPrizes()          {              for(var x=0; x<prizes.length; x++)              {                  currentPrize = prizes[x];                             //假如沒有被撞擊,則描繪                  if(!currentPrize.hit)                  {                      ctx.drawImage(currentPrize.image, prizes[x].x, prizes[x].y);                  }              }              if(AllPrizesHit())              {                  GameOver();              }          }                    //撞到獎品          function HasAnimalHitPrize()          {              //取出所有獎品              for(var x=0; x<prizes.length; x++)              {                  var prize = prizes[x];                  //假如沒有碰撞過                  if(!prize.hit)                  {                      //判斷碰撞                      if(CheckIntersect(prize, animal, 0))                      {                          prize.hit = true;                          //熊反彈下沉                          verticalSpeed = speed;                          //總分增加                          score += prize.point;                      }                  }              }               }          //判斷是否撞完獎品,假如其中有一個          function AllPrizesHit()          {              for(var c=0; c<prizes.length; c++)              {                  checkPrize = prizes[c];                  if(checkPrize.hit == false)                      return false;                                    }              return true;          }          //開始或者暫停游戲          function ToggleGameplay()          {              gameRunning = !gameRunning;              if(gameRunning)              {                  $("#BtnImgStart").hide();                   gameloopId = setInterval(GameLoop, 10);               }else              {                  clearInterval(gameloopId);              }          }          //結束游戲          function GameOver()          {              gameRunning = false;              clearInterval(gameloopId);              alert("游戲結束!");          }          //描繪生命條          function DrawLives()          {              ctx.drawImage(livesImages[lives], 0, 0);          }          //描繪分數          function DrawScore()          {              ctx.drawImage(scoreImg, screenWidth-(scoreImg.width),0);//分數板              ctx.font = "12pt Arial";              ctx.fillText("" + score, 425, 25);  //得分          }          //初始化             $(window).ready(function(){                  AddEventHandlers();//添加事件                LoadImages();                         ctx = document.getElementById('canvas').getContext('2d'); //獲取2d畫布                    screenWidth = parseInt($("#canvas").attr("width")); //畫布寬度               screenHeight = parseInt($("#canvas").attr("height"));                 //初始化蘑菇              mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐標                mushroom.y = screenHeight - 40;//蘑菇Y坐標                 //初始化熊              animal.x = parseInt(screenWidth/2);              animal.y = parseInt(screenHeight/2);               //初始化獎品              InitPrizes();                        });                        </script>        </head>                <body>            <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">                <canvas id="canvas" width="480" height="320" >               瀏覽器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">請下載</a>支持html5的瀏覽器來觀看               </canvas>            </div>               <img id="BtnImgStart" style="position: absolute; left: 200px; top: 200px; cursor: pointer; float: left; display: block; " src="./images/START-Button.png">              </body>        </html>

感謝各位的閱讀,以上就是“怎么用html5實現蘑菇與熊游戲”的內容了,經過本文的學習后,相信大家對怎么用html5實現蘑菇與熊游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

遵义市| 原平市| 哈尔滨市| 轮台县| 马鞍山市| 易门县| 天柱县| 肇东市| 瓮安县| 隆林| 大新县| 蒙山县| 桐庐县| 涞水县| 武清区| 鄂尔多斯市| 枣强县| 隆安县| 和平区| 博爱县| 文化| 浦东新区| 阳朔县| 江北区| 班玛县| 承德县| 靖宇县| 五大连池市| 吉首市| 涟源市| 四平市| 马边| 绵竹市| 克东县| 泽库县| 电白县| 获嘉县| 湾仔区| 思茅市| 永安市| 伊宁市|