jsDOM編程-烏龜抓小雞遊戲

來源:互聯網
上載者:User

標籤:


<html>
 <head>
  <title>js烏龜抓小雞遊戲 </title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <script type="text/javascript">
        //響應使用者的點擊按鈕或者鍵盤按鈕
       function move(obj){
      //alert(obj.keyCode);
        //得到烏龜圖片的高度與寬度
          var wu_width = 100;
          var wu_height = 75;
       //得到小雞圖片的高度與寬度
         var cock_width = 100;
         var cock_height = 75;
         var  wugui = document.getElementById(‘wugui‘); //得到烏龜所在的div
          //可以判讀使用者是通過點擊滑鼠還是按下鍵盤
        
         if(window.event){
           var key = obj.keyCode;//keyCode  鍵盤值
         }
         //得到烏龜的top值 left值
         var wu_top = wugui.style.top;
         var wu_left = wugui.style.left;
         //將烏龜的top值 left值 去掉px並轉化為數字
         wu_top = parseInt(wu_top.substr(0,wu_top.indexOf("p")));
         wu_left = parseInt(wu_left.substr(0,wu_left.indexOf("p")));
         //判斷使用者點擊或按下的是哪個按鈕
         if((obj.value == ‘向下走‘) || (key == 40)){
              wugui.style.top = (wu_top+10)+‘px‘;
              wu_top += 10;
         }else if((obj.value == ‘向右走‘) || (key == 39 )){
             wugui.style.left = (wu_left+10)+‘px‘;
             wu_left += 10;
        
         }else if((obj.value == ‘向左走‘) || (key == 37 )){
             wugui.style.left = (wu_left-10)+‘px‘;
             wu_left -= 10;
        
         }else if((obj.value == ‘向上走‘) || (key == 38 )){
                wugui.style.top = (wu_top-10)+‘px‘;
                wu_top -= 10;
        
         }
         //得到小雞當前的top 與 left
         var cock = document.getElementById(‘cock‘);
         var cock_top = cock.style.top;
         var cock_left = cock.style.left;
         cock_top = parseInt(cock_top.substr(0,cock_top.indexOf(‘p‘)));
         cock_left = parseInt(cock_left.substr(0,cock_left.indexOf(‘p‘)));
         var y = Math.abs(cock_top-wu_top); //兩點間縱座標的絕對值
         var x = Math.abs(cock_left-wu_top);//兩點間橫座標的絕對值
         /*
                :window.event代表著事件對象的狀態。只有在事件發生時生效。如滑鼠、鍵盤等事件被觸發時這個對象才存在。
                window.event這隻在IE下是這樣的。他並不是標準。。也就是說其他瀏覽器並不支援。
                在W3C標準支援的瀏覽器下事件對向是引發事件涵數的第一個參數,
                參數名隨意,在下面的例子中我的事件對象的參數名是evt,也可以定義為其他名字.為了相容IE瀏覽器可以這樣寫
         */
         //判讀烏龜是否抓住小雞
         var xx = 0;
         var yy = 0;
         //判斷縱向
         if(wu_top < cock_top){
             if(y<wu_height){
                yy = 1;
             }
         }else{
            if(y<cock_height){
               yy = 1;
            }
         }
         //判斷橫向
         if(wu_width<cock_width){
             if(x<wu_width){
                 xx = 1;
             }
         }else{
            if(x<cock_width){
               xx = 1;
            }
         }
         if((xx==1)&&(yy==1)){
            window.alert(‘烏龜挺快嘛!贊‘);
            wugui.style.top = 120+‘px‘;
            wugui.style.left = 100+‘px‘;
         }
       }
       //自由移動小雞的位置
                  function fRandomBy(under, over){
                   switch(arguments.length){
                     case 1: return parseInt(Math.random()*under+1);
                     case 2: return parseInt(Math.random()*(over-under+1) + under);
                     default: return 0;
             }
           }
                   function movecock(){
                     var random = fRandomBy(-20,20);
                     var ji = document.getElementById(‘cock‘);
                     var ji_top = ji.style.top;
                     var ji_left = ji.style.left;
                      ji_top = parseInt(ji_top.substr(0,ji_top.indexOf(‘p‘)));
                      ji_left = parseInt(ji_left.substr(0,ji_left.indexOf(‘p‘)));
                     /*alert(document.body.scrollHeight);
                      var body_width = parseInt(document.body.scrollHeight.substr(0,document.body.scrollHeight.indexOf(‘p‘));
                      var body_hieght= parseInt(document.body.scrollWidth.substr(0,document.body.scrollWidth.indexOf(‘p‘));
                      if(ji_left >=body_width || ji_top >= body_height){
                            ji_top -= random;
                           ji_left -= random;
                      }
                      */
                      ji_top += random;
                      ji_left += random;
                      ji.style.top = ji_top + ‘px‘;
                      ji.style.left = ji_left + ‘px‘;
                      //alert(ji_top + ‘==>‘+ji_left);

                   };
                  var mytimer=setInterval(‘movecock()‘,500);
                  /*function reRun(){
                     mytimer=setInterval(‘movecock()‘,500);
                  }*/
    
    </script>
 </head>
 <body onkeydown="return move(event)" >
    <table border="1">
        <tr>
           <td> </td>
           <td><input type="button" value="向上走" onclick="move(this)"/></td>
           <td> </td>
        </tr>
         <tr>
           <td><input type="button" value="向左走" onclick="move(this)"/></td>
           <td><input type="button" value="向下走" onclick="move(this)"/></td>
           <td><input type="button" value="向右走" onclick="move(this)"/></td>
        </tr>
    </table>
    <!--把烏龜放在一個div裡-->
    <div id="wugui" style="position:absolute;left:100px;top:120px">
       <img src="images/wugui.jpg" width="100px" height="75px"/>
    </div>
    <!--把小雞放在一個div裡-->
    <div id="cock" style="position:absolute;left:200px;top:200px">
     <img src="images/xiaoji.jpg" width="100px" height="75px"/>
    </div>
 </body>
</html>
對烏龜抓住小雞的演算法的圖解

 

jsDOM編程-烏龜抓小雞遊戲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.