js實(shí)現(xiàn)超級(jí)瑪麗小游戲
本文實(shí)例為大家分享了js超級(jí)瑪麗小游戲的具體代碼,供大家參考,具體內(nèi)容如下
怎么用通過(guò)按鍵,來(lái)控制圖片的位置這個(gè)小游戲,用面向?qū)ο髸?huì)很方便,不用面向?qū)ο髸?huì)很麻煩很麻煩,比如以后要講解的坦克大戰(zhàn)的游戲,要是用純的面向過(guò)程或函數(shù)式的方式寫(xiě),那維護(hù)起來(lái)會(huì)非常的麻煩。
游戲分析:
看看如何通過(guò)按鈕來(lái)控制mario的位置
設(shè)計(jì)相關(guān)的對(duì)象(Mario x y ...)
onclick屬性:當(dāng)用戶(hù)點(diǎn)擊某個(gè)對(duì)象時(shí)調(diào)用的事件句柄
素材
代碼在目錄:超級(jí)瑪利亞.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è)計(jì)Mario類(lèi) function Mario(){ this.x=0; this.y=0; //移動(dòng) 順時(shí)針 0->上 1->右 2->下 3->左 this.move=function(direct){ switch(direct){ case 0: //向上 //window.alert('mario 右移動(dòng)'); //這里為了改變 img的left 和top,我們需要得到 img元素。需要用到j(luò)avascript的DOM編程。img 對(duì)象 var mymario=document.getElementById(’mymario’); //取出 img 的top值 //window.alert(mymario.style.top); //怎樣去掉50px的px var top=mymario.style.top; //px占據(jù)兩個(gè),即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'; //開(kāi)始移動(dòng)2px,看怎么拼接的,字符串和數(shù)值之間的轉(zhuǎn)換 //此時(shí)mario就可以向下移動(dòng)了,把上面的打印調(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對(duì)象 var mario=new Mario(); </script> </head> <body> <div class='gamediv'> <img src='http://m.piao2010.com/bcjs/person.png' /> <!--用到了絕對(duì)定位--> </div> <table border='1px' class='controlcenter'> <tr > <td colspan='3' >游戲鍵盤(pán)</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果2. IntelliJ IDEA導(dǎo)入jar包的方法3. 刪除docker里建立容器的操作方法4. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法5. IDEA調(diào)試源碼小技巧之辨別抽象類(lèi)或接口多種實(shí)現(xiàn)類(lèi)的正確路徑6. Django實(shí)現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁(yè)面,并展示7. Jquery使用原生AJAX方法請(qǐng)求數(shù)據(jù)8. JS如何在數(shù)組指定位置插入元素9. Java源碼解析之ClassLoader10. PHP下對(duì)緩沖區(qū)的控制
