js實現(xiàn)超級瑪麗小游戲
本文實例為大家分享了js超級瑪麗小游戲的具體代碼,供大家參考,具體內(nèi)容如下
怎么用通過按鍵,來控制圖片的位置
這個小游戲,用面向?qū)ο髸芊奖悖挥妹嫦驅(qū)ο髸苈闊┖苈闊?,比如以后要講解的坦克大戰(zhàn)的游戲,要是用純的面向過程或函數(shù)式的方式寫,那維護起來會非常的麻煩。
游戲分析:
看看如何通過按鈕來控制mario的位置
設(shè)計相關(guān)的對象(Mario x y ...)
onclick屬性:當(dāng)用戶點擊某個對象時調(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)文章
深入淺析JavaScript面向?qū)ο蠛驮秃瘮?shù)
這篇文章主要介紹了深入淺析JavaScript面向?qū)ο蠛驮秃瘮?shù)的相關(guān)資料,需要的朋友可以參考下2016-02-02詳解template標(biāo)簽用法(含vue中的用法總結(jié))
這篇文章主要介紹了template標(biāo)簽用法(含vue中的用法總結(jié)),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-01-01layer.confirm()右邊按鈕實現(xiàn)href的例子
今天小編就為大家分享一篇layer.confirm()右邊按鈕實現(xiàn)href的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09用javascript獲取textarea中的光標(biāo)位置
Javascript一向以他的靈活隨意而著稱,這也使得它的功能可以非常的強大,而由于沒有比較好的調(diào)試工具,又使得它使用起來困難重重,尤其使對于一些初學(xué)者,更是感覺到無從下手。今天探討的問題是用javascript獲取textarea中光標(biāo)的位置。2008-05-05