巢穴

about:blank

<转>两个有意思的javascript

<html>
<head>
<title>盒子游戏</title>
<style type="text/css">
body
{font-size:10pt;}
.table_back
{width:300px;height:300px;}
.floor
{border:1px solid black;cursor:pointer;}
.closed
{background-color:#808080;cursor:default;}
.edge
{border-color:#dfdfdf;}
.mickey
{background:red;cursor:default;}
.spider
{background:green;cursor:default;}
</style>
</head>
<body>
<script type="text/javascript">
/*--
标题:盒子游戏
设计:王集鹄
博客:http://blog.csdn.net/zswang
日期:2009年12月5日
--
*/


function Box(options) {
    options 
= options || {};
    
var self = this;
    
this.rowCount = options.rowCount || 15// 行数
    this.colCount = options.colCount || 15// 列数
    this.closedCount = options.closedCount || 3// 起始关闭数
    this.mickeyCount = options.mickeyCount || 1// 老鼠的数量
    this.parent = options.parent || document.body; // 容器
    this.onnewmickey = options.onnewmickey; // 初始化老鼠的事件
    this.table_back = document.createElement("table");
    
this.table_back.className = "table_back";
    
this.table_back.border = 1;
    
this.playing = false// 是否在进行中
    this.floors = {}// 矩阵地板矩阵 [y,x]
    for (var i = 0; i < this.rowCount; i++{
        
var tr = this.table_back.insertRow(-1);
        
for (var j = 0; j < this.colCount; j++{
            
var td = tr.insertCell(-1);
            
this.floors[i + "," + j] = new Floor({
                td: td
                , box: 
this
                , pos: 
{ x: j, y: i }
            }
);
        }

    }


    
this.mickeys = []; // 老鼠集合
    for (var i = 0; i < this.mickeyCount; i++{
        
var mickey = new Mickey({box: this});
        mickey.index 
= i;
        
if (this.onnewmickey) this.onnewmickey(mickey);
        
this.mickeys.push(mickey);
    }

    
this.parent.appendChild(this.table_back);
}


// 重新开始
Box.prototype.replay = function () {
    
this.playing = true;
    
for (var i = 0; i < this.rowCount; i++{
        
for (var j = 0; j < this.colCount; j++{
            
this.floors[i + "," + j].setState("blank");
        }

    }

    
for (var i = 0; i < this.closedCount; i++{
        
this.floors[
            Math.floor(
this.rowCount * Math.random())
            
+ "," + Math.floor(this.colCount * Math.random())
        ].setState(
"closed");
    }

    
var j = -(this.mickeys.length / 2);
    
for (var i = 0; i < this.mickeys.length; i++, j++{
        
if (this.mickeys.length % 2 == 0 && i == this.mickeys.length / 2)
            j
++;
        
this.mickeys[i].move({
            x: Math.floor(
this.colCount / 2)
            , y: Math.floor(
this.rowCount / 2 + j)
        }
);
    }

}


// 下一个周期
Box.prototype.kick = function() {
    
if (!this.playing) return;
    
var count = 0;
    
var freedom = false;
    
for (var i = 0; i < this.mickeys.length; i++{
        
if (this.mickeys[i].run()) count++;
        
if (this.mickeys[i].freedom) freedom = true;
    }

    
if (freedom) // 有老鼠跑了
        this.gameover();
    
else if (!count) this.win();
}


// 游戏结束
Box.prototype.gameover = function() {
    
if (!this.playing) return;
    
this.playing = false;
    
if (this.ongameover) this.ongameover();
}


Box.prototype.win 
= function() {
    
if (!this.playing) return;
    
this.playing = false;
    
if (this.onwin) this.onwin();
}


// 地板
function Floor(options) {
    options 
= options || {};
    
var self = this;
    
this.td = options.td || {};
    
this.td.innerHTML = "&nbsp;";
    
this.td.onclick = function() {
        
if (!self.box.playing) return// 游戏已经结束
        if (self.state != "blank"return;
        self.setState(
"closed");
        self.box.kick();
    }

    
this.box = options.box || {};
    
this.pos = options.pos || {};
    
this.state = "blank";
    
this.doChange();
}


// 状态变化
Floor.prototype.doChange = function() {
    
var className = "floor";
    
if (this.pos.x * this.pos.y == 0
        
|| this.pos.x == this.box.colCount - 1
        
|| this.pos.y == this.box.rowCount - 1// 边缘
        className += " edge";
    className 
+= " " + this.state;
    
if (this.td.className != className)
        
this.td.className = className;
}


Floor.prototype.setState 
= function(state) {
    
if (this.state == state) return;
    
this.state = state;
    
if (this.box.next) this.box.next();
    
this.doChange();
}


// 老鼠类
function Mickey(options) {
    options 
= options || {};
    
this.box = options.box || {};
    
this.route = null// 逃跑路线
    this.pos = {}// 所在位置
    this.move(this.pos);
    
this.freedom = false;
    
this.name = "mickey";
    
this.offsets = [ // 移动规则
        {x: -1, y: 0}
        , 
{x: 0, y: -1}
        , 
{x: +1, y: 0}
        , 
{x: 0, y: +1}
    ];
}


// 移动位置
Mickey.prototype.move = function(pos) {
    pos 
= pos || {};
    
if (pos.x == this.x && pos.y == this.y) return;
    
if (this.box.floors[this.pos.y + "," + this.pos.x])
        
this.box.floors[this.pos.y + "," + this.pos.x].setState("blank");
    
this.pos = pos;
    
this.box.floors[this.pos.y + "," + this.pos.x].setState(this.name);
    
this.freedom = this.pos.x == 0 || this.pos.y == 0 
        
|| this.pos.x == this.box.colCount - 1
        
|| this.pos.y == this.box.rowCount - 1;
}


// 老鼠跑
Mickey.prototype.run = function() {
    
var directions = { route: [], pos: this.pos };
    
this.flags = {};
    
this.route = [];
    
if (this.search(this.pos, this.route)) {
        
this.move(this.route.shift());
        
return true;
    }

}


// 搜索逃跑路径
Mickey.prototype.search = function(pos, route) {
    
if (this.flags[pos.y + "," + pos.x]) return false;
    
this.flags[pos.y + "," + pos.x] = true// 标记已经扫描

    
// 搜索直观的路径
    var directions = {}// 每个方向的路径
    var min = -1// 最短的距离
    for (var i = 0; i < this.offsets.length; i++{
        directions[i] 
= [];
        
for (var j = 1; ; j++{
            
var test = {
                x: pos.x 
+ this.offsets[i].x * j
                , y: pos.y 
+ this.offsets[i].y * j
            }
;
            
if (!(test.x >= 0 && test.y >= 0
                
&& test.x < this.box.colCount && test.y < this.box.rowCount 
                
&& !this.flags[test.y + "," + test.x] // 未搜索过
                && this.box.floors[test.y + "," + test.x].state == "blank")) {
                directions[i] 
= [];
                
break;
            }

            directions[i].push(test);
            
if (test.x == 0 || test.y == 0
                
|| test.x == this.box.colCount - 1
                
|| test.y == this.box.rowCount -1{
                
if (min < 0 || directions[min].length > directions[i].length) min = i;
                
break;
            }

        }

    }

    
if (min >= 0{
        
for (var i = 0; i < directions[min].length; i++)
            route.push(directions[min][i]);
        
return true;
    }


    
// 回溯搜索
    var k = Math.floor(Math.random() * this.offsets.length);
    
for (var i = 0; i < this.offsets.length; i++{
        
var test = {
            x: pos.x 
+ this.offsets[(k + i) % this.offsets.length].x
            , y: pos.y 
+ this.offsets[(k + i) % this.offsets.length].y
        }
;
        
if (test.x >= 0 && test.y >= 0
            
&& test.x < this.box.colCount && test.y < this.box.rowCount 
            
&& !this.flags[test.y + "," + test.x] // 未搜索过
            && this.box.floors[test.y + "," + test.x].state == "blank"// 非空地
            route.push(test);
            
if (test.x == 0 || test.y == 0
                
|| test.x == this.box.colCount - 1
                
|| test.y == this.box.rowCount -1)
                
return true;
            
if (this.search(test, route)) return true;
            route.pop();
        }

    }

    
return false;
}


var button = document.createElement("input");
button.type 
= "button";
button.value 
= "第一关";
button.onclick 
= function() 
    box1.replay(); 
}
;
document.body.appendChild(button);

var box1 = new Box();
box1.replay();

var button = document.createElement("input");
button.type 
= "button";
button.value 
= "第二关";
button.onclick 
= function() 
    box2.replay(); 
}
;
document.body.appendChild(button);

var box2 = new Box({mickeyCount: 2});

box2.replay();

var button = document.createElement("input");
button.type 
= "button";
button.value 
= "第三关";
button.onclick 
= function() 
    box3.replay(); 
}
;
document.body.appendChild(button);
var box3 = new Box({
    onnewmickey: 
function(mickey) {
        mickey.offsets 
= [
            
{x: -1, y: 0}
            , 
{x: 0, y: -1}
            , 
{x: +1, y: 0}
            , 
{x: 0, y: +1}
            , 
{x: -1, y: +1}
            , 
{x: -1, y: -1}
            , 
{x: +1, y: -1}
            , 
{x: +1, y: +1}
        ];
        mickey.name 
= 'spider';
    }

}
);

box3.replay();
box1.ongameover 
= box2.ongameover = box3.ongameover = function() {
    alert(
"跑掉了。o(╯□╰)o");
}

box1.onwin 
= box2.onwin = box3.onwin = function() {
    alert(
"O(∩_∩)O成功!");
}

</script>
</body>
</html>

两个脚本都是在csdn上无意中看到的..第一个挺有意思的游戏..堵箱子..玩法就不说了..很简单..
第二个是一个烟花的脚本..08奥运会那个大脚烟花..这个是真的很有意思..
<html>
    
<head>
        
<title>K.G Script3D 2.00 Beta - Map UpFire</title>
    
</head>
    
<body bgcolor="#000000" text="#ffffff" style="overflow:hidden;">
        
<!--<p id="idText" style="color:rgb(255,255,255)">提示:改变窗口大小后请刷新,以便观赏最大视野效果。</p>-->
    
</body>
    
<script type="text/javascript">
//K.G Script3D 2.00 Beta
//
3D Script for DHTML
//
Copyright - KiteGirl [China] 2009
var pubElements = null;
var pubX = pubY = pubZ = 0;
var pubR = pubG = pubB = 0;
var pubViewAngle = 0;
var pubViewWidth = 0;
var pubViewHeight = 0;
var pubViewFogOver = 0;
var pubViewFogFull = 0;
var pubAt = 0;
var pubCellLen = 0;
var pubBaseX = pubBaseY = pubScale = 0;
var pubBaseZ = 0;
var pubTimerCode = 0;
var pubOnTimer = 0;
var pubTimeout = 0;
var pubMapValStrs = 0;
var pubPy = 0;
var pubPyDt = 0;
var pubSSG_reCosVa = 0;
var pubSSG_reVa = 0;
var pubMapsIndex = 0;
function rgb(b, g, r) {
    
return "#" + 
        (
"00" + Math.floor(r).toString(16)).replace(/^.*(\w{2}$)/g, "$1"+
        (
"00" + Math.floor(g).toString(16)).replace(/^.*(\w{2}$)/g, "$1"+
        (
"00" + Math.floor(b).toString(16)).replace(/^.*(\w{2}$)/g, "$1");
}

window.onload 
= function() {
    pubSSG_reCosVa 
= 0;
    pubSSG_reVa 
= 0;
    pubViewAngle 
= 45;
    pubViewWidth 
= (document.body.clientWidth || document.documentElement.clientWidth) - 10;
    pubViewHeight 
= (document.body.clientHeight || document.documentElement.clientHeight) - 10;
    
    pubBaseZ 
= 2000;
    pubViewFogOver 
= 3700;
    pubViewFogFull 
= 1500;
    pubCellLen 
= 255;
    pubPy 
= 0;
    pubPyDt 
= 110;
    tMap 
= "21,3,22,3,23,3,18,4,19,4,20,4,24,4,25,4,17,5,26,5,17,6,26,6,17,7,26,7,17,8,26,8,17,9," +
        
"26,9,16,10,26,10,16,11,26,11,15,12,26,12,13,13,14,13,26,13,11,14,12,14,26,14,9,15,10,15," +
        
"26,15,8,16,25,16,8,17,25,17,4,18,5,18,8,18,25,18,2,19,3,19,6,19,8,19,25,19,2,20,6,20,8," +
        
"20,25,20,2,21,6,21,9,21,10,21,24,21,2,22,6,22,10,22,11,22,12,22,23,22,3,23,4,23,5,23,13," +
        
"23,14,23,15,23,16,23,17,23,18,23,19,23,20,23,21,23,22,23,10,24,11,24,25,24,26,24,9,25,12," +
        
"25,15,25,16,25,24,25,27,25,8,26,12,26,14,26,17,26,20,26,21,26,24,26,28,26,9,27,11,27,14," +
        
"27,17,27,19,27,22,27,25,27,27,27,10,28,14,28,17,28,19,28,22,28,26,28,15,29,16,29,19,29," +
        
"22,29,20,30,21,30";
    pubMapValStrs 
= tMap.split(/,/);
    tValLen 
= pubMapValStrs.length;
    pubCellLen 
= tValLen / 2;
    pubX 
= new Array(pubCellLen);
    pubY 
= new Array(pubCellLen);
    pubZ 
= new Array(pubCellLen)
    
    GetMap1();
    pubElements 
= ElementsCreate(pubCellLen);
    NewColor();
    
    ElementsView(pubElements, pubX, pubY, pubZ, pubCellLen, pubViewAngle, pubViewWidth, pubViewHeight, pubViewWidth 
/ 2, pubViewHeight / 2, pubBaseZ, pubViewFogFull, pubViewFogOver, pubR, pubG, pubB, 9);
    TimeLoop();
}

function GetMap1() {
    
for (var tCi = 0; tCi < pubCellLen; tCi++{
        pubX[tCi] 
= 16 - parseInt(pubMapValStrs[tCi * 2]) - 16;
        pubZ[tCi] 
= (parseInt(pubMapValStrs[tCi * 2 + 1]) - 16* 2;
        pubY[tCi] 
= 500 - pubZ[tCi];
    }

}

function GetMap2() {
    
for (var tCi = 0; tCi < pubCellLen; tCi++{
        pubX[tCi] 
= parseInt(pubMapValStrs[tCi * 2]) - 16 + 16;
        pubZ[tCi] 
= (parseInt(pubMapValStrs[tCi * 2 + 1]) - 16* 2;
        pubY[tCi] 
= 500 - pubZ[tCi];
    }

}

function NewColor() {
    
var tRGB = new Array(3);
    tRGB[
0= Math.random() * 0.5 + 0.5;
    tRGB[
1= Math.random() * 0.5 + 0.5;
    tRGB[
2= Math.random() * 0.5 + 0.5;
    tRGB[Math.floor(Math.random() 
* 3)] = 1;
    pubR 
= tRGB[0* 255;
    pubG 
= tRGB[1* 255;
    pubB 
= tRGB[2* 255;
}

function TimeLoop() {
    
//pubOnTimer = Timer;
    if (pubAt < 1500) pubAt = pubAt + 50;
    pubPy 
= pubPy + pubPyDt;
    pubPyDt 
= pubPyDt - 5;
    
if (pubAt > 1440{
        pubAt 
= pubPy = 0;
        pubPyDt 
= 110;
        pubMapsIndex 
= (pubMapsIndex + 1% 2;
        
if (pubMapsIndex == 0{
                GetMap1();
        }
 else {
                GetMap2();
        }

        NewColor();
    }

    ElementsView(pubElements, pubX, pubY, pubZ, pubCellLen, pubViewAngle, pubViewWidth, pubViewHeight, 
        pubViewWidth 
/ 2, pubViewHeight / 2, pubBaseZ, pubViewFogFull, pubViewFogOver, pubR, pubG, pubB, 9);
    pubTimerCode 
= window.setTimeout("TimeLoop();"33);
}

function ElementsCreate(pLength) {
    
var oElms = new Array(pLength + 1);
    
for (tEi = 0; tEi < pLength; tEi++{
        
with (document) {
            oElms[tEi] 
= createElement("SPAN");
            body.appendChild(oElms[tEi]);
        }

        
with (oElms[tEi]) {
            innerHTML 
= "*";
            style.position 
= "absolute";
            style.color 
= "#000000";
        }

    }

    
return oElms;
}

function ElementsView(pElements, pXs, pYs, pZs, pLength, pVa, pVw, pVh, pBx, pBy, pBz, pCf, pCe, pRs, pGs, pBs, pVcs) {
    
for (tEi = 0; tEi < pLength; tEi++{
        tSc 
= ((pubAt / 100+ 1);
        tZ 
= pZs[tEi] * tSc + pBz + (1000 - pubAt) * 2;
        tX 
= pXs[tEi] * tSc;
        tY 
= pYs[tEi] + 500 - pubPy;
        
var tVx = tVy = tVcs = 0;
        
if (tZ > 0) ShadowScenoGraph(tX, tY, tZ, pVa, pVw, pBx, pBy, pVcs, function (oVx, oVy, oVcs) { tVx = oVx; tVy = oVy; tVcs = oVcs; });
        
if (tVcs < 0) tVcs = Math.abs(tVcs);
        tVc 
= LocateOverView(tVx, tVy, tZ, pVw, pVh);
        
if (tVc) {
            
with (pElements[tEi].style) {
                color 
= "#000000";
                fontSize 
= "9";
                top 
= 0
                left 
= 0
            }

        }
 else {
            
with (pElements[tEi].style) {
                color 
= FogColor(tZ, pCf, pCe, pRs, pGs, pBs);
                fontSize 
= tVcs + 1;
                top 
= tVy;
                left 
= tVx;
                zIndex 
= tVcs + 1;
            }

        }

    }

}

function FogColor(pZ, pVf, pVe, pR, pG, pB) {
    
if (pZ > pVe) {
        tCs 
= 0;
        oColor 
= 0;
    }
 else if (pZ < pVf) {
        tCs 
= 1;
    }
 else {
        tCs 
= (pVe - pZ) / (pVe - pVf);
    }

    tB 
= tCs * pB;
    tG 
= tCs * pG;
    tR 
= tCs * pR;
    
return rgb(tB, tG, tR);
}

function LocateOverView(pX, pY, pZ, pVw, pVh) {
    
return (pX > pVw) | (pX < 0| (pY > pVh) | (pY < 0| (pZ < 0);
}

function ShadowScenoGraph(iX, iY, iZ, iVa, iVw, iVbx, iVby, iVcs, func) {
    
if (pubSSG_reVa != iVa) {
        pubSSG_reVa 
= iVa;
        pubSSG_reCosVa 
= Math.cos(Radian(pubSSG_reVa));
    }

    tZw 
= iZ * pubSSG_reCosVa;
    
if (tZw != 0{
            oVcs 
= iVcs * iVw / tZw;
            tVps 
= oVcs / 2;
            oVx 
= (iX * iVw) / tZw + iVbx - tVps;
            oVy 
= (iY * iVw) / tZw + iVby - tVps;
    }
 else {
            oVcs 
= oVx = oVy = 0;
    }

    func(oVx, oVy, oVcs);
}

    
function SphereLocate(iR, iAv, iAp, oX, oY, oZ) {
    tRv 
= Radian(iAv);
    tRp 
= Radian(iAp);
    tR_Sin_Rv 
= iR * Math.sin(tRv);
    oX 
= tR_Sin_Rv * Math.cos(tRp);
    oY 
= tR_Sin_Rv * Math.sin(tRp);
    oZ 
= iR * Math.cos(tRv);
}

function Radian(pA) {
    
return pA * 71 / 4068;
}

    
</script>
</html>


本文来自CSDN博客,转载请标明出处:http:
//blog.csdn.net/zswang/archive/2009/01/13/3771948.aspx

posted on 2009-12-07 11:57 Vincent 阅读(246) 评论(0)  编辑 收藏 引用 所属分类: 游戏


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理