弒月繁楓
          helloworld
          posts - 0,comments - 0,trackbacks - 0
            1 
            2 /** 以下為俄羅斯方塊的方塊積木,因為每一個落下的積木經過操作后就由移動的狀態變為靜止狀態,
            3  *  這些積木的動作可以看作是多個進程在經過一系列操作后結束,所以可以通過繼承Thread進程類來實現*/
            4 public class ErsBlock extends Thread{
            5     //這里和ErsBox中的靜態參數一樣,作為參考值
            6     public final static int BOXES_ROWS=4;
            7     public final static int BOXES_COLS=4;
            8     //每一個積木移下一次所需的時間
            9     public int BETWEEN_LEVEls_DEGRESS_TIME=430;
           10     //有7種積木,每種4個變化
           11     public final static int BLOCK_KIND_NUMBER=7;
           12     public final static int BLOCK_STATUS_NUMBER=4;
           13     //16進制表示每種每一個變化的積木
           14     public final static int[][] STYLES={
           15         {0x0f00,0x4444,0x0f00,0x4444},
           16         {0x04e0,0x0464,0x00e4,0x04c4},
           17         {0x4620,0x6c00,0x4620,0x6c00},
           18         {0x2640,0xc600,0x2640,0xc600},
           19         {0x6220,0x1700,0x2230,0x0740},
           20         {0x6440,0x0e20,0x44c0,0x8e00},
           21         {0x0660,0x0660,0x0660,0x0660},
           22     }; 
           23     //畫布類+參數
           24     private GameCanvas canvas;
           25     //顯示積木,為一個4x4的格組
           26     private ErsBox[][] boxes=new ErsBox[BOXES_ROWS][BOXES_COLS];
           27     private int style,y,x;
           28     //記錄積木狀態
           29     private boolean pausing=false,moving=true;
           30     //主類構造,帶有每一個積木的位置信息,
           31     public ErsBlock(int style,int y,int x,GameCanvas canvas) {
           32         this.style=style;
           33         this.y=y;
           34         this.x=x;
           35         this.canvas=canvas;
           36         int key=0x8000;
           37         for(int i=0;i<boxes.length;i++){
           38             for(int j=0;j<boxes[i].length;j++){
           39                 boolean isColor=((style&key)!=0);
           40                 boxes[i][j]=new ErsBox(isColor);
           41                 key>>=1;
           42             }
           43         }
           44         display();
           45     }
           46     //時間與級數有關
           47     public void setLevelTime(int lev){
           48         BETWEEN_LEVEls_DEGRESS_TIME=lev;
           49     }
           50     //thread類,必須實現run函數,沒有停止前每到一個時間周期下落一次
           51     public void run(){
           52         while(moving){
           53             try{
           54                 sleep(BETWEEN_LEVEls_DEGRESS_TIME);//*(ErsBlocksGame.MAX_LEVEL-level+LEVEL_FLATNESS_GENE));
           55             }
           56             catch(InterruptedException ie){
           57                 ie.printStackTrace();
           58             }
           59             if(!pausing)
           60                 moving=(moveTo(y+1,x)&&moving);
           61         }
           62     }
           63     //3個方向移動
           64     public void moveLeft(){
           65         moveTo(y,x-1);
           66     }
           67     public void moveRight(){
           68         moveTo(y,x+1);
           69     }
           70     public void moveDown(){
           71         moveTo(y+1,x);
           72     }
           73     //變化為下一個
           74     public void turnNext(){
           75         for(int i=0;i<BLOCK_KIND_NUMBER;i++)
           76             for(int j=0;j<BLOCK_STATUS_NUMBER;j++){
           77                 if(STYLES[i][j]==style){
           78                     int newStyle=STYLES[i][(j+1)%BLOCK_STATUS_NUMBER];
           79                     turnTo(newStyle);
           80                     return ;
           81                 }
           82             }
           83     }
           84     //設置狀態
           85     public void pauseMove(){
           86         pausing=true;
           87     }
           88     public void resumeMove(){
           89         pausing=false;
           90     }
           91     public void stopMove(){
           92         moving=false;
           93     }
           94     //每一次移動都要先抹去痕跡
           95     private void erase(){
           96         for(int i=0;i<boxes.length;i++)
           97             for(int j=0;j<boxes[i].length;j++){
           98                 if(boxes[i][j].isColorBox()){
           99                     ErsBox box=canvas.getBox(i+y,j+x);
          100                     if(box==null)
          101                         continue;
          102                     box.setColor(false);
          103                 }
          104             }
          105     }
          106     //顯示操作后的積木
          107     private void display(){
          108         for(int i=0;i<boxes.length;i++)
          109             for(int j=0;j<boxes[i].length;j++){
          110                 if(boxes[i][j].isColorBox()){
          111                     ErsBox box=canvas.getBox(i+y,j+x);
          112                     if(box==null)
          113                         continue;
          114                     box.setColor(true);
          115                 }
          116             }
          117     }
          118     //判斷是否可以移動
          119     private boolean isMoveAble(int newRow,int newCol){
          120         erase();
          121         for(int i=0;i<boxes.length;i++)
          122             for(int j=0;j<boxes[i].length;j++){
          123                 if(boxes[i][j].isColorBox()){
          124                     ErsBox box=canvas.getBox(i+newRow,j+newCol);
          125                     if(box==null||(box.isColorBox())){
          126                         display();
          127                         return false;
          128                     }
          129                 }
          130             }
          131         display();
          132         return true;
          133     }
          134     //移動到下一個位置,這里synchronized表示這個動作不受其他進程的影響,單獨進行,
          135     //也就是可以邊下落,邊接受操作
          136     private synchronized boolean moveTo(int newRow,int newCol){
          137         if(!isMoveAble(newRow,newCol)||!moving)
          138             return false;
          139         erase();
          140         y=newRow;
          141         x=newCol;
          142         display();
          143         canvas.repaint();
          144         return true;
          145     }
          146     //判斷是否可以變化
          147     private boolean isTurnAble(int newStyle){
          148         int key=0x8000;
          149         erase();
          150         for(int i=0;i<boxes.length;i++)
          151             for(int j=0;j<boxes[i].length;j++){
          152                 if((newStyle&key)!=0){
          153                     ErsBox box=canvas.getBox(y+i,x+j);
          154                     if(box==null||box.isColorBox()){
          155                         display();
          156                         return false;
          157                     }
          158                 }
          159                 key>>=1;
          160             }
          161         display();
          162         return true;
          163     }
          164     //變化到下一個狀態
          165     private boolean turnTo(int newStyle){
          166         if(!isTurnAble(newStyle)||!moving)
          167             return false;
          168         erase();
          169         int key=0x8000;
          170         for(int i=0;i<boxes.length;i++)
          171             for(int j=0;j<boxes[i].length;j++){
          172                 boolean isColor=((newStyle&key)!=0);
          173                 boxes[i][j].setColor(isColor);
          174                 key>>=1;
          175             }
          176         style=newStyle;
          177         display();
          178         canvas.repaint();
          179         return true;
          180     }
          181 }
          182 
          posted on 2012-03-10 20:49 吖鑵_sysu 閱讀(467) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 城步| 武陟县| 兰溪市| 仁寿县| 昭平县| 云龙县| 丹凤县| 长兴县| 中江县| 平罗县| 静安区| 古蔺县| 阿勒泰市| 定结县| 广州市| 昂仁县| 卓尼县| 那曲县| 疏附县| 法库县| 沈阳市| 城步| 社会| 深州市| 广河县| 崇阳县| 云安县| 利川市| 库伦旗| 高台县| 易门县| 平顶山市| 乌海市| 雷州市| 清水县| 报价| 卢湾区| 广德县| 稻城县| 丁青县| 延长县|