Vml+Js算法:完成5个小球在网页运动(碰壁返回)的游戏,详细注释

lshdic 2003-08-21 02:39:31
<HTML xmlns:v>
<HEAD>
<META http-equiv='Content-Type' content='text/html;charset=gb2312'>
<Meta name='Gemeratpr' content='网络程序员伴侣(Lshdic)2004'>
<TITLE>物极必反</TITLE>
<STYLE>
v\:*{behavior:url(#default#VML);} //声明变量v为VML对象
*{font-size:12px;color:;}
a{text-decoration:none;}
a:hover{color:red;}
</STYLE>
</HEAD>
<BODY topmargin='2' leftmargin='2'>
<BASE target='_top'>
<div id=a style='table-Layout:fixed;width:100%;height:100%;border:1 solid black'></div>
<script>
//原作:风云舞,载自:http://www.lshdic.com/bbs
//以前在VB里很容易实现用我摸索的这套“物极必反”的算法完成物体碰壁返回的游戏,这次是DHTML版的,VB的下载来http://www.lshdic.com/download/lshdic/vb_xiaoguo.rar
var wid1,hei1,str1="" //定义全局变量,为提高运行速度
var xx=new Array(0,0,0,0,0) //分别存储5个球的X坐标
var yy=new Array(0,0,0,0,0) //分别存储5个球的Y坐标
var xjia=new Array(false,true,false,true,false) //分别判断5个球是否“物极”到了X极限
var yjia=new Array(true,false,true,false,true) //分别判断5个球是否“物极”到了Y极限
var ovalwid=new Array(0,0,0,0,0) //分别存储5个球随机的大小
wid1=a.offsetWidth-70;hei1=a.offsetHeight-70 //得到容器的宽和高
for(i=0;i<5;i++){ //首次向容器内塞进5个球,球属性是随机的
tempx=Math.round(Math.random()*wid1);tempy=Math.round(Math.random()*hei1);
tempcolor="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
ovalwid[i]=Math.round(Math.random()*70)+20;
xx[i]=tempx;yy[i]=tempy
str1+="<v:oval fillcolor='"+tempcolor+"' style='position:absolute;left:"+tempx+";top:"+tempy+";z-index:"+i+";width:"+ovalwid[i]+";height:"+ovalwid[i]+";' id='oval1'/>"
}
a.innerHTML=str1; //插入STR1,STR1是5个VML球的代码
function play1(){ //播放函数
wid1=a.offsetWidth;hei1=a.offsetHeight
for(i=0;i<5;i++){ //循环5次
if(wid1-xx[i]<ovalwid[i]+5)xjia[i]=false //到达X极限,值为FALSE
if(xx[i]<5)xjia[i]=true //到达X起点,值为TRUE
if(hei1-yy[i]<ovalwid[i]+5)yjia[i]=false
if(yy[i]<5)yjia[i]=true
if (xjia[i]==true)xx[i]+=5;else xx[i]-=5 //TRUE的话就++,FALSE的话就--
if (yjia[i]==true)yy[i]+=5;else yy[i]-=5
oval1[i].style.left=xx[i];oval1[i].style.top=yy[i] //更新球的位置
}}
setInterval("play1()",10) //10毫秒播放一次,一般CPU保证能消化~~~
</script>
</BODY>
</HTML>
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
coffee_cn 2004-01-14
  • 打赏
  • 举报
回复
good job
thanks
HStone 2003-11-28
  • 打赏
  • 举报
回复
稍微改了一下
<html xmlns:v>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=gb2312'>
<title>ÎK±Ø·´</title>
<style>
v\:*{behavior:url(#default#VML);}
*{font-size:12px;color:;}
a{text-decoration:none;}
a:hover{color:red;}
</style>
<script>
function Container(name, left, top, width, height) {
this.name = name;
this.left = left;
this.top = top;
this.width = width;
this.height = height;
this.obj = eval(name);
this.put = function(obj) {
obj.container = this;
this.obj.insertAdjacentHTML("beforeEnd", obj.getCode())
obj.obj = eval(obj.name);
}
this.checkLeft = function(x) {
return (left<=x);
}
this.checkRight = function(x) {
return (x<=left + width);
}
this.checkTop = function(y) {
return (top<=y);
}
this.checkBottom = function(y) {
return (y<=top + height);
}
}

function SHAPE(name, shape, x, y, width, height, color, zindex, xa, ya) {
this.name = name;
this.shape = shape;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.zindex = zindex;
this.xa = xa;
this.ya = ya;
this.obj = null;
this.container = null;

this.move = function(xa, ya) {
if (typeof(xa)!="undefined" && typeof(ya)!="undefined") {
this.xa = xa;
this.xa = ya;
}
with (this) {
if (!container.checkLeft(x) || !container.checkRight(x + xa + width))
xa = -xa;
x += xa;

if (!container.checkTop(y + ya) || !container.checkBottom(y + ya + height))
ya = -ya;
y += ya;

obj.style.left = x;
obj.style.top = y;
}
}
this.getCode = function() {
return "<v:" + shape + " fillcolor='" + this.color + "' style='position:absolute;left:" + this.x +";top:" + this.y + ";z-index:" + zindex + ";width:" + this.width + ";height:" + this.height + ";' id='" + this.name + "'/>";
}
}
</script>
</head>
<body topmargin='2' leftmargin='2'>
<div id=a style='table-Layout:fixed;width:100%;height:100%;border:1 solid black'></div>
<script>
var container = new Container("a", 0, 0, a.offsetWidth, a.offsetHeight);
var obj = new Array();
for (var i=0; i<5; i++) {
shape = Math.round(Math.random())==1?"oval":"rect";
size = Math.round(Math.random() * 70) + 20;
x = Math.round(Math.random() * (a.offsetWidth - size));
y = Math.round(Math.random() * (a.offsetHeight - size));
color = "rgb(" + Math.round(Math.random()*255) + "," + Math.round(Math.random()*255) + "," + Math.round(Math.random()*255) + ")";
xa = Math.round(Math.random()*10);
ya = Math.round(Math.random()*10);

obj[i] = new SHAPE("SHAPE" + i, shape, x, y, size, size, color, i, xa, ya);
container.put(obj[i]);
}
function move() {
for (i=0; i<obj.length; i++)
obj[i].move();
}
setInterval("move();",10);
</script>
</body>
</html>
超级大笨狼 2003-11-27
  • 打赏
  • 举报
回复
好如果能用鼠标碰它就好了。
dreamfly8848 2003-08-21
  • 打赏
  • 举报
回复
不错。
全息宇宙 2003-08-21
  • 打赏
  • 举报
回复
smallbear0425 2003-08-21
  • 打赏
  • 举报
回复
thanks very much!!!please go on!!!
sunicom 2003-08-21
  • 打赏
  • 举报
回复
好东西,好好学习

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧