js實(shí)現(xiàn)超級瑪麗小游戲
本文實(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)文章
JavaScript 中的12種循環(huán)遍歷方法【總結(jié)】
本文給大家總結(jié)了12種JavaScript 中的1循環(huán)遍歷方法,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05
使用CSS3的scale實(shí)現(xiàn)網(wǎng)頁整體縮放
QQ郵箱的網(wǎng)頁整體縮放效果,原來實(shí)現(xiàn)方法如此簡單,下面有個實(shí)現(xiàn)示例,大家可以參考下2014-03-03
javascript中parseInt()函數(shù)的定義和用法分析
這篇文章主要介紹了javascript中parseInt()函數(shù)的定義和用法,較為詳細(xì)的分析了parseInt()函數(shù)的定義及具體用法,以及參數(shù)使用時的注意事項,需要的朋友可以參考下2014-12-12
使用JavaScript實(shí)現(xiàn)一個小程序之99乘法表
這篇文章主要介紹了使用JavaScript實(shí)現(xiàn)一個小程序之99乘法表的相關(guān)資料,需要的朋友可以參考下2017-09-09
js replace 與replaceall實(shí)例用法詳解
這篇文章介紹了js replace 與replaceall實(shí)例用法詳解,有需要的朋友可以參考一下2013-08-08

