前段时间写的称重游戏。哈哈
保存保存。
<style>
.box{
position:absolute;
width:25px;
height:25px;
z-index:9;
filter : progid:DXImageTransform.Microsoft.gradient(startColorStr=#80FCFCFC,endColorStr=#FFC3C3C3);
border:1px solid #999999;
display:block;
font-size:12px;
cursor:pointer;
}
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<body>
<table id="Layer1" class="box" style="left:33px;top:17px;" onMouseDown="startDrage(this)"><tr><td>1</td></tr></table>
<table id="Layer2" class="box" style="left:80px;top:17px;" onMouseDown="startDrage(this)"><tr><td>2</td></tr></table>
<table id="Layer3" class="box" style="left:160px;top:17px;" onMouseDown="startDrage(this)"><tr><td>3</td></tr></table>
<table id="Layer4" class="box" style="left:220px;top:17px;" onMouseDown="startDrage(this)"><tr><td>4</td></tr></table>
<table id="Layer5" class="box" style="left:270px;top:17px;" onMouseDown="startDrage(this)"><tr><td>5</td></tr></table>
<table id="Layer6" class="box" style="left:310px;top:17px;" onMouseDown="startDrage(this)"><tr><td>6</td></tr></table>
<table id="Layer7" class="box" style="left:370px;top:17px;" onMouseDown="startDrage(this)"><tr><td>7</td></tr></table>
<table id="Layer8" class="box" style="left:430px;top:17px;" onMouseDown="startDrage(this)"><tr><td>8</td></tr></table>
<table id="Layer9" class="box" style="left:490px;top:17px;" onMouseDown="startDrage(this)"><tr><td>9</td></tr></table>
<table id="Layer10" class="box" style="left:550px;top:17px;" onMouseDown="startDrage(this)"><tr><td>10</td></tr></table>
<table id="Layer11" class="box" style="left:610px;top:17px;" onMouseDown="startDrage(this)"><tr><td>11</td></tr></table>
<table id="Layer12" class="box" style="left:670px;top:17px;" onMouseDown="startDrage(this)"><tr><td>12</td></tr></table>
<div id="w1" style="position:absolute; left:253px; top:205px; width:128px; height:127px; z-index:2;border:2px solid #00ff00"></div>
<div id="w2" style="position:absolute; left:557px; top:203px; width:128px; height:127px; z-index:2; border:2px solid #00ff00"></div>
<div id="buttom" style="position:absolute; left:443px; top:259px; width:48px; height:24px; z-index:4">
<input type="button" name="Submit" value="称重" onClick="weighBall()">
</div>
<div id="answer" style="position:absolute; left:879px; top:235px; width:49px; height:53px; z-index:5; border:2px solid #ff0000"></div>
<div id="buttom2" style="position:absolute; left:885px; top:292px; width:41px; height:27px; z-index:6">
<input type="button" name="Submit" value="答案" onClick="ckAnswer()">
</div>
</body>
<script language="javascript">
var isDrag;
var xFloat;
var yFloat;
var veighCount;
var floatObj;
veighCount=3;
ballNum=12;
xFloat=0;
yFloat=0;
isDrag=false;
function startDrage(dObj)
{
xFloat=event.clientY-dObj.style.top.replace("px","");
yFloat=event.clientX-dObj.style.left.replace("px","");
isDrag=true;
floatObj=dObj;
}
function document.onmousemove()
{
if (isDrag)
{
floatObj.style.top=event.clientY-xFloat;
floatObj.style.left=event.clientX-yFloat;
if (floatObj.style.top<1 || floatObj.style.left<1)
{
isDrag=false;
}
}
}
function document.onmouseup()
{
isDrag=false;
}
function document.body.onload()
{
alert ("游戏说明:\n这里总共有12个砖块,其中有一个,和其他11个重量不相同(不知道是轻还是重),\n你有一个天平,要求在称过三次之后,找出那个特殊的砖块,\n绿色的方块代表天平,\n如果你找到答案,请把砖块拖到红色的方块中。")
}
function document.body.onselectstart()
{
return false;
}
function ckAnswer()
{
var answerBox;
var p1t;
var p1l;
var p2t;
var p2l;
var ballarr=new Array()
var ballNumInBox
answerBox=document.getElementById("answer")
p1t=answerBox.style.top.replace("px","");
p1l=answerBox.style.left.replace("px","");
p2t=answerBox.style.top.replace("px","")*1+answerBox.style.height.replace("px","")*1;
p2l=answerBox.style.left.replace("px","")*1+answerBox.style.width.replace("px","")*1;
ballarr=fandBallInBox(p1t,p1l,p2t,p2l)
ballNumInBox=ballarr.length
if (ballNumInBox==0)
{
alert ("请放入砖块!")
}
else if(ballNumInBox>1)
{
alert ("只能放入一个砖块!")
}
else if(ballarr[0].weight!=5)
{
alert ("恭喜你,选择正确,你是天才。")
}
else
{
alert ("选择错误。")
window.location.reload()
}
}
function weighBall()
{
if (veighCount<1)
{
alert ("只能称三次。请选择不同的砖块。")
return false;
}
veighCount--;
var leftBox;
var rightBox;
var leftW;
var rightW;
var p1t;
var p1l;
var p2t;
var p2l;
var ballarr=new Array()
var ballNumInBox
leftW=0;
rightW=0;
leftBox=document.getElementById("w1")
p1t=leftBox.style.top.replace("px","");
p1l=leftBox.style.left.replace("px","");
p2t=leftBox.style.top.replace("px","")*1+leftBox.style.height.replace("px","")*1;
p2l=leftBox.style.left.replace("px","")*1+leftBox.style.width.replace("px","")*1;
ballarr=fandBallInBox(p1t,p1l,p2t,p2l)
for (i=0;i<ballarr.length;i++)
{
leftW=leftW+ballarr[i].weight;
}
rightBox=document.getElementById("w2")
p1t=rightBox.style.top.replace("px","");
p1l=rightBox.style.left.replace("px","");
p2t=rightBox.style.top.replace("px","")*1+rightBox.style.height.replace("px","")*1;
p2l=rightBox.style.left.replace("px","")*1+rightBox.style.width.replace("px","")*1;
ballarr=fandBallInBox(p1t,p1l,p2t,p2l)
for (i=0;i<ballarr.length;i++)
{
rightW=rightW+ballarr[i].weight;
}
if (leftW>rightW)
{
alert ("左边重!")
}
else if(leftW<rightW)
{
alert ("右边重!")
}
else
{
alert ("两边相等!")
}
}
function fandBallInBox(a1,a2,b1,b2)
{
var tmpObj
var tmpArr=new Array()
var j;
j=0;
for (i=1;i<=ballNum;i++)
{
tmpObj=document.getElementById("Layer"+i)
if (getYCenter(tmpObj)>a1 && getYCenter(tmpObj)<b1 && getXCenter(tmpObj)>a2 && getXCenter(tmpObj)<b2)
{
tmpArr[j]=objBalls[i]
j++;
}
}
return tmpArr;
}
function getXCenter(obj)
{
var a;
a=obj.style.left.replace("px","")*1+obj.style.width.replace("px","")*1/2
return a;
}
function getYCenter(obj)
{
var a;
a=obj.style.top.replace("px","")*1+obj.style.height.replace("px","")*1/2
return a;
}
////////////////////////////
var ball=function()
{
this.weight=5
}
ball.prototype.showObj=function()
{
}
/////////////////////////////////////////
var objBalls=new Array()
for (i=1;i<=ballNum;i++)
{
objBalls[i]=new ball()
}
var ranNum
ranNum=Math.floor(Math.random()*12)+1
ranFl=Math.floor(Math.random()*2)
if (ranFl==0)
{
objBalls[ranNum].weight=objBalls[ranNum].weight+1
}
else
{
objBalls[ranNum].weight=objBalls[ranNum].weight-1
}
</script>
posted on 2005-10-15 13:08
登陆不起啦 阅读(286)
评论(0) 编辑 收藏 引用 所属分类:
Javascript 研究