HTML5小遊戲——看你有多色

來源:互聯網
上載者:User

標籤:cti   htm   special   參考   script      splay   spec   turn   

使用了封裝了canvas的create.js庫來實現的。

 

最終效果:

 

工程:

Rect.js

/* *  方塊類 */function Rect(n,color,specialColor){        createjs.Shape.call(this);        /**     * 設定方塊的類型     */    this.setRectType=function(type){        this._RectType=type;        switch(type){            case 1:                this.setColor(color);                break;            case 2:                this.setColor(specialColor);                break;        }    }        /**     * 擷取方塊的類型     */    this.getRectType=function(){        return this._RectType;    }        /**     * 設定方塊的顏色+繪製方塊      */    this.setColor=function(colorString){        this.graphics.beginFill(colorString);        this.graphics.drawRect(0,0,400/n-5,400/n-5);        this.graphics.endFill();    }        //方塊的預設類型是1    this.setRectType(1);}Rect.prototype=new createjs.Shape();

 

seeColor.js

/** * 繪製舞台 */var stage=new createjs.Stage("gameView");var gameView=new createjs.Container();stage.addChild(gameView);//var s=new createjs.Shape();//s.graphics.beginFill("#00FF00");//s.graphics.drawRect(0,0,100,100);//s.graphics.endFill();//gameView.addChild(s);createjs.Ticker.setFPS(30);createjs.Ticker.addEventListener("tick",stage);//特殊的那個方塊的容差var diffDegree=30;//n*n的矩陣var n=2;var maxN=7;//在隨機產生顏色的時候[0,500] [500,255*255*255]這兩個區間內的顏色將被排除var edgeColor=10;function addRect(){        //隨機顏色    var randR=Math.floor(Math.random()*255-edgeColor*2)+edgeColor;    var randG=Math.floor(Math.random()*255-edgeColor*2)+edgeColor;    var randB=Math.floor(Math.random()*255-edgeColor*2)+edgeColor;        //特殊方塊的顏色    var specialR=randR-diffDegree>edgeColor?randR-diffDegree:randR+diffDegree;    var specialG=randG-diffDegree>edgeColor?randG-diffDegree:randG+diffDegree;    var specialB=randB-diffDegree>edgeColor?randB-diffDegree:randB+diffDegree;        var color="rgb("+randR+","+randG+","+randB+")";    var specialColor="rgb("+specialR+","+specialG+","+specialB+")";        //特殊方塊的位置    var specialX=Math.floor(Math.random()*n);    var specialY=Math.floor(Math.random()*n);        //繪製所有方塊    for(var indexX=0;indexX<n;indexX++){        for(var indexY=0;indexY<n;indexY++){            var r=new Rect(n,color,specialColor);            gameView.addChild(r);            r.x=indexX;            r.y=indexY;            if(r.x==specialX && r.y==specialY){                r.setRectType(2);            }            r.x=indexX*(400/n);            r.y=indexY*(400/n);            if(r.getRectType()==2){                //點到特殊方塊的時候重繪                r.addEventListener("click",function(){                    if(n<maxN){                        ++n;                    }                    gameView.removeAllChildren();                    addRect();                });            }        }    }}addRect();

index.html

<!DOCTYPE html><html>    <head>        <meta charset="utf-8" />        <title>See Color</title>    </head>    <body>                <canvas id="gameView" width="400px" height="400px"></canvas>        <script type="text/javascript" src="js/easeljs-0.8.2.min.js"></script>        <script type="text/javascript" src="js/Rect.js"></script>        <script type="text/javascript" src="js/seeColor.js"></script>            </body></html>

 

github:https://github.com/BenDanChen/seeColor

 

 

參考資料:

極客學院視頻教程地址 http://www.jikexueyuan.com/course/167.html

create.js 

英文站 http://createjs.com/

中文站 http://www.createjs.cc/

HTML5小遊戲——看你有多色

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.