最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實(shí)現(xiàn)超級瑪麗小游戲

 更新時間:2020年03月18日 10:20:13   作者:Liangyi_J  
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)超級瑪麗小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js超級瑪麗小游戲的具體代碼,供大家參考,具體內(nèi)容如下

怎么用通過按鍵,來控制圖片的位置
這個小游戲,用面向?qū)ο髸芊奖?,不用面向?qū)ο髸苈闊┖苈闊热缫院笠v解的坦克大戰(zhàn)的游戲,要是用純的面向過程或函數(shù)式的方式寫,那維護(hù)起來會非常的麻煩。 

游戲分析:

看看如何通過按鈕來控制mario的位置

設(shè)計相關(guān)的對象(Mario x y ...)

onclick屬性:當(dāng)用戶點(diǎn)擊某個對象時調(diào)用的事件句柄 

素材

代碼在目錄:超級瑪利亞.html

<html> 
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
 <style>
 
  .gamediv{ 
  width: 500px; 
  height: 400px; 
  background-color: pink; 
 
 } 
  
 
 /*表格樣式*/ 
 .controlcenter{ 
  width: 200px; 
  height: 200px; 
  border: 1px solid red; 
  text-align:center; 
 
 } 
 
 </style> 
 
 <head> 
 
 <script language="javascript"> 
  //設(shè)計Mario類 
  function Mario(){ 
  this.x=0; 
  this.y=0; 
  //移動 順時針 0->上 1->右 2->下 3->左 
  this.move=function(direct){ 
   switch(direct){ 
 
   case 0: //向上 
    //window.alert("mario 右移動"); 
    //這里為了改變 img的left 和top,我們需要得到 img元素。需要用到j(luò)avascript的DOM編程。img 對象 
    var mymario=document.getElementById('mymario'); 
    //取出 img 的top值 
    //window.alert(mymario.style.top); 
 
    //怎樣去掉50px的px 
    var top=mymario.style.top; 
    //px占據(jù)兩個,即lenght-2 
    //window.alert(top.substr(0,top.length-2)); 
 
    //現(xiàn)在還是串,要轉(zhuǎn)成數(shù)值才能加減 
    top=parseInt(top.substr(0,top.length-2)); 
 
    //window.alert(top); 
    mymario.style.top=(top-2)+"px"; //開始移動2px,看怎么拼接的,字符串和數(shù)值之間的轉(zhuǎn)換 
 
    //此時mario就可以向下移動了,把上面的打印調(diào)試輸出代碼都注釋掉 
 
    break; 
 
   case 1: //向右 
 
    var mymario=document.getElementById('mymario'); 
    var left=mymario.style.left; 
    left=parseInt(left.substr(0,left.length-2)); 
    mymario.style.left=(left+2)+"px"; 
    break; 
 
   case 2: //向下 
 
    var mymario=document.getElementById('mymario'); 
    var top=mymario.style.top; 
    top=parseInt(top.substr(0,top.length-2)); 
    mymario.style.top=(top+2)+"px"; 
    break; 
 
   case 3: //向左 
 
    var mymario=document.getElementById('mymario'); 
    var left=mymario.style.left; 
    left=parseInt(left.substr(0,left.length-2)); 
    mymario.style.left=(left-2)+"px"; 
    break; 
 
   } 
 
  } 
 
  } 
 
 
  //創(chuàng)建Mario對象 
  var mario=new Mario(); 
 
 </script> 
 </head> 
 <body> 
 
 <div class="gamediv"> 
  <img id="mymario" src="person.png" style="left:100px; top:50px; position:absolute;" /> <!--用到了絕對定位--> 
  </div> 
  <table border="1px" class="controlcenter"> 
   <tr > 
   <td colspan="3" >游戲鍵盤</td> 
  </tr> 
  <tr> 
   <td>**</td> 
   <td><input type="button" value="向上" onclick="mario.move(0)" /></td>
   <!-- <td><input type="button" value="向上" onclick="marioMove(0)" /></td> -->
   <td>**</td> 
  </tr> 
 
  <tr>
 
   <td><input type="button" value="向左" onclick="mario.move(3)" /> </td>
   <td>**</td> 
   <td><input type="button" value="向右" onclick="mario.move(1)" /> </td> 
  </tr> 
 
  <tr> 
   <td>**</td> 
   <td><input type="button" value="向下" onclick="mario.move(2)" /> </td>
   <td>**</td> 
 
  </tr> 
 
  </table> 
 </body> 
 
</html> 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

西宁市| 托克托县| 天等县| 平塘县| 镇远县| 集安市| 黄梅县| 滁州市| 贵州省| 西宁市| 搜索| 班戈县| 成都市| 喜德县| 博客| 大埔县| 镶黄旗| 永济市| 天津市| 余姚市| 曲水县| 柞水县| 沅江市| 富蕴县| 苗栗县| 佛学| 江油市| 兖州市| 昭觉县| 垦利县| 南开区| 小金县| 奉化市| 志丹县| 景德镇市| 固始县| 新绛县| 新野县| 吐鲁番市| 芦溪县| 扶沟县|