日期:2014-05-16  浏览次数:20670 次

闲着蛋疼,用JQUERY写个小小游戏,要分的来咯! - Web 开发 / Ajax
今晚闲着蛋疼,长夜漫漫,无心睡眠,写了个JQUERY插件,给同学们一个小小游戏。

效果图:


玩法:


插件代码:
JScript code

$.fn.game = function(options)
{
    var parms = {
        block:"block",
        atler:"#E3E3E3",
        width:40,
        height:40,
        row:0,
        cell:0,
        time:60
    };
    if(options) {
        $.extend(parms, options);
    };
    var _this = $(this);
    var _score = 0;
    var _scoreFrame = 0;
    var _timeFrame;
    var _timeContent;
    var _timeUse = parms.time;
    var _time;
    var _table;
    var _tr;
    var _td;
    var _random;
    var color = [
        { index:1, rgb:"#006600" },
        { index:2, rgb:"#006666" },
        { index:3, rgb:"#990000" },
        { index:4, rgb:"#663399" },
        { index:5, rgb:"#FF6600" },
        { index:6, rgb:"#000" }
    ];

    var initStatus = function()
    {
        _scoreFrame = $("<div/>").css("margin-bottom","10px").css("color","#FF0000").css("font-family","微软雅黑").html("分数:"+_score).appendTo(_this);
        _timeFrame = $("<div/>").css("width","500").css("height","20").css("padding","1px").css("margin-bottom","10px").css("border","1px #ccc solid").appendTo(_this);
        _timeContent = $("<div/>").css("width","100%").css("height","20").css("background","#009900").appendTo(_timeFrame);
        _time = setInterval(changeTime,1000);
    }
    
    var changeTime = function()
    {
        if(_timeUse>0)
        {
            _timeUse--;
            _timeContent.css("width",(_timeUse/parms.time)*100+"%");
        }
        else
        {
            alert("Sorry,时间到了!");
            clearInterval(_time);
        }
    }

    var initBlock = function()
    {
        _table = $("<table/>").attr("id",parms.block).attr("cellPadding","0").attr("cellSpacing","0").css("border","1px #ccc solid").appendTo(_this);
        for(var i=0; i<parms.row; i++)
        {
            _tr = $("<tr/>").appendTo(_table);
            for(var j=0; j<parms.cell; j++)
            {
                _td = $("<td/>").attr("id",i+"-"+j).css("width",parms.width).css("height",parms.height).appendTo(_tr);
                _td.click(function(){
                    clickFunc(this.id);
                });
                if((parseInt(i)+parseInt(j))%2==0)
                {
                    _td.css("background",parms.atler);
                }
                if(random(20)<10)
                {
                    _random = random(color.length);
                    _td.attr("id",i+"-"+j+"-"+color[_random-1].index);
                    _td.css("background",color[_random-1].rgb);
                    _td.unbind("click");
                }
            }
        }
    }
    
    var selectRowCell = function(row,cell)
    {
        return $("#block tr:eq("+row+") td:eq("+cell+")");
    }
    
    var clickFunc = function(id)
    {
        if(typeof id != "string") return;
        var _arr = id.split('-');
        var _current,_left,_right,_top,_bottom;
        var _left_id,_right_id,_top_id,_bottom_id;
        for(var i=_arr[1]-1; i>=0; i--)
        {
            _current = selectRowCell(_arr[0],i);
            if(_current.attr("id").split('-').leng