⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 action.java

📁 JAVA俄罗斯方块源码,复制到java即可运行
💻 JAVA
字号:
package diamond;

import java.awt.Rectangle;
/**
    这个类为动作类,主要是用来实现方块的旋转,把主窗体frame当参数传进来是为了得到主窗体中
的属性graphicNum的值并直接旋转正在下落的图形,属性graphicNum是当前正在下落的图形的形状,
frame.state是当前的图形旋转到的状态,类中有1个旋转方法Rolling()、6个旋转实现方法和两个
 判断方法OutOfBounds()。
   注:方块不需要旋转所以不需要写旋转方法。
 */
public class Action {
    private Frame1 frame;
    public Action(Frame1 frame) {
        this.frame = frame;
    }

    public void Rolling( ){       //主旋转方法,frame.graphicNum是图形的形状
        switch(frame.graphicNum){
           case 0:
               rollG0();
              break;
           case 1:
               //方块
              break;
           case 2:
               rollG2();
              break;
           case 3:
               rollG3();
              break;
           case 4:
               rollG4();
              break;
           case 5:
               rollG5();
              break;
           case 6:
               rollG6();
        }
    }

    private void rollG2( ){     //坚条的旋转,frame.state是当前的图形旋转到的状态,下标2是当前图形的形状
       if (frame.state[2] == 0){
           if ( OutOfBounds(frame.shape[0].getX()+36, frame.shape[0].getY()+36,
                            frame.shape[1].getX()+18, frame.shape[1].getY()+18,
                            frame.shape[3].getX()-18, frame.shape[3].getY()-18) ) return;
           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+36, frame.shape[0].getY()+36, 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()+18, frame.shape[1].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-18, frame.shape[3].getY()-18, 18, 18));
           frame.state[2] = 1;
       }else if (frame.state[2] == 1){
           if ( OutOfBounds(frame.shape[0].getX()-36, frame.shape[0].getY()-36,
                            frame.shape[1].getX()-18, frame.shape[1].getY()-18,
                            frame.shape[3].getX()+18, frame.shape[3].getY()+18) ) return;
           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-36, frame.shape[0].getY()-36, 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()-18, frame.shape[1].getY()-18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+18, frame.shape[3].getY()+18, 18, 18));
           frame.state[2] = 0;
       }
    }

    private void rollG3( ){      //T形的旋转
       if (frame.state[3] == 0){
           frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()-18, frame.shape[2].getY()-18, 18, 18));
           frame.state[3] = 1;
       }else if (frame.state[3] == 1){
            if ( OutOfBounds(frame.shape[3].getX()+18, frame.shape[3].getY()-18 ) ) return;
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+18, frame.shape[3].getY()-18, 18, 18));
           frame.state[3] = 2;
       }else if (frame.state[3] == 2){
           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+18, frame.shape[0].getY()+18, 18, 18));
           frame.state[3] = 3;
       }else if (frame.state[3] == 3){
            if ( OutOfBounds(frame.shape[0].getX()-18, frame.shape[0].getY()-18,
                             frame.shape[2].getX()+18, frame.shape[2].getY()+18,
                             frame.shape[3].getX()-18, frame.shape[3].getY()+18) ) return;
           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-18, frame.shape[0].getY()-18, 18, 18));
           frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()+18, frame.shape[2].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-18, frame.shape[3].getY()+18, 18, 18));
           frame.state[3] = 0;
       }
    }

    private void rollG4( ){      //反L形的旋转
       if (frame.state[4] == 0){
            if ( OutOfBounds( frame.shape[0].getX(), frame.shape[0].getY()-36,
                              frame.shape[1].getX()+18, frame.shape[1].getY()+18,
                              frame.shape[3].getX()-18, frame.shape[3].getY()-18) ) return;
           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX(), frame.shape[0].getY()-36, 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()+18, frame.shape[1].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-18, frame.shape[3].getY()-18, 18, 18));
           frame.state[4] = 1;
       }else if (frame.state[4] == 1){
           if ( OutOfBounds(frame.shape[0].getX()+36, frame.shape[0].getY(),
                             frame.shape[1].getX()-18, frame.shape[1].getY()+18,
                             frame.shape[3].getX()+18, frame.shape[3].getY()-18) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+36, frame.shape[0].getY(), 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()-18, frame.shape[1].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+18, frame.shape[3].getY()-18, 18, 18));
           frame.state[4] = 2;
       }else if (frame.state[4] == 2){
           if ( OutOfBounds(frame.shape[0].getX(), frame.shape[0].getY()+36,
                            frame.shape[1].getX()-18, frame.shape[1].getY()-18,
                            frame.shape[3].getX()+18, frame.shape[3].getY()+18) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX(), frame.shape[0].getY()+36, 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()-18, frame.shape[1].getY()-18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+18, frame.shape[3].getY()+18, 18, 18));
           frame.state[4] = 3;
       }else if (frame.state[4] == 3){
           if ( OutOfBounds(frame.shape[0].getX()-36, frame.shape[0].getY(),
                            frame.shape[1].getX()+18, frame.shape[1].getY()-18,
                            frame.shape[3].getX()-18, frame.shape[3].getY()+18) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-36, frame.shape[0].getY(), 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()+18, frame.shape[1].getY()-18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-18, frame.shape[3].getY()+18, 18, 18));
           frame.state[4] = 0;
       }
    }

    private void rollG5( ){      //L形的旋转
        if (frame.state[5] == 0){
            if ( OutOfBounds(frame.shape[0].getX()+18, frame.shape[0].getY()+18,
                            frame.shape[2].getX()-18, frame.shape[2].getY()-18,
                            frame.shape[3].getX()-36, frame.shape[3].getY()) ) return;

            frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+18, frame.shape[0].getY()+18, 18, 18));
            frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()-18, frame.shape[2].getY()-18, 18, 18));
            frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-36, frame.shape[3].getY(), 18, 18));
            frame.state[5] = 1;
        }else if (frame.state[5] == 1){
            if ( OutOfBounds(frame.shape[0].getX()-18, frame.shape[0].getY()+18,
                           frame.shape[2].getX()+18, frame.shape[2].getY()-18,
                           frame.shape[3].getX(), frame.shape[3].getY()-36) ) return;

            frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-18, frame.shape[0].getY()+18, 18, 18));
            frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()+18, frame.shape[2].getY()-18, 18, 18));
            frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX(), frame.shape[3].getY()-36, 18, 18));
            frame.state[5] = 2;
        }else if (frame.state[5] == 2){
            if ( OutOfBounds(frame.shape[0].getX()-18, frame.shape[0].getY()-18,
                            frame.shape[2].getX()+18, frame.shape[2].getY()+18,
                            frame.shape[3].getX()+36, frame.shape[3].getY()) ) return;

            frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-18, frame.shape[0].getY()-18, 18, 18));
            frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()+18, frame.shape[2].getY()+18, 18, 18));
            frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+36, frame.shape[3].getY(), 18, 18));
            frame.state[5] = 3;
        }else if (frame.state[5] == 3){
            if ( OutOfBounds(frame.shape[0].getX()+18, frame.shape[0].getY()-18,
                            frame.shape[2].getX()-18, frame.shape[2].getY()+18,
                            frame.shape[3].getX(), frame.shape[3].getY()+36) ) return;

            frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+18, frame.shape[0].getY()-18, 18, 18));
            frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()-18, frame.shape[2].getY()+18, 18, 18));
            frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX(), frame.shape[3].getY()+36, 18, 18));
            frame.state[5] = 0;
        }
    }

    private void rollG6( ){      //反Z形的旋转
       if (frame.state[6] == 0){
           if ( OutOfBounds(frame.shape[0].getX()+36, frame.shape[0].getY(),
                            frame.shape[1].getX()+18, frame.shape[1].getY()+18,
                            frame.shape[3].getX()-18, frame.shape[3].getY()+18) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+36, frame.shape[0].getY(), 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()+18, frame.shape[1].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()-18, frame.shape[3].getY()+18, 18, 18));
           frame.state[6] = 1;
       }else if (frame.state[6] == 1){
           if ( OutOfBounds(frame.shape[0].getX()-36, frame.shape[0].getY(),
                            frame.shape[1].getX()-18, frame.shape[1].getY()-18,
                            frame.shape[3].getX()+18, frame.shape[3].getY()-18) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-36, frame.shape[0].getY(), 18, 18));
           frame.shape[1].setBounds(new Rectangle(frame.shape[1].getX()-18, frame.shape[1].getY()-18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX()+18, frame.shape[3].getY()-18, 18, 18));
           frame.state[6] = 0;
       }
    }

    private void rollG0( ){      //Z形的旋转
       if (frame.state[0] == 0){
           if ( OutOfBounds(frame.shape[0].getX()-18, frame.shape[0].getY()+18,
                            frame.shape[2].getX()-18, frame.shape[2].getY()-18,
                            frame.shape[3].getX(), frame.shape[3].getY()-36) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()-18, frame.shape[0].getY()+18, 18, 18));
           frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()-18, frame.shape[2].getY()-18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX(), frame.shape[3].getY()-36, 18, 18));
           frame.state[0] = 1;
       }else if (frame.state[0] == 1){
           if ( OutOfBounds(frame.shape[0].getX()+18, frame.shape[0].getY()-18,
                            frame.shape[2].getX()+18, frame.shape[2].getY()+18,
                            frame.shape[3].getX(), frame.shape[3].getY()+36) ) return;

           frame.shape[0].setBounds(new Rectangle(frame.shape[0].getX()+18, frame.shape[0].getY()-18, 18, 18));
           frame.shape[2].setBounds(new Rectangle(frame.shape[2].getX()+18, frame.shape[2].getY()+18, 18, 18));
           frame.shape[3].setBounds(new Rectangle(frame.shape[3].getX(), frame.shape[3].getY()+36, 18, 18));
           frame.state[0] = 0;
       }
   }

   //判断移动的图形将要到位置是否过了边界或已经有图,2个参数是移动块将要移动到的坐位置坐标
   private boolean OutOfBounds(int sX, int sY){
       if ( sX<0 || sX >=162 ||(frame.backShape[(sX/18) + (sY/18)*10].getIcon() != null)
            ||(frame.backShape[(sX/18) + (sY/18)*10].getIcon() != null) ){
           return true;
        }
      return false;
   }

   //上面方法的重载,有6个坐标做参数
   private boolean OutOfBounds(int s0X, int s0Y, int s2X, int s2Y, int s3X, int s3Y){
       if ( s0X<0 || s0X>162 || s2X<0 || s2X>162 || s3X<0 ||s3X>162
            ||(frame.backShape[(s0X/18) + (s0Y/18)*10].getIcon() != null)
            ||(frame.backShape[(s2X/18) + (s2Y/18)*10].getIcon() != null)
            ||(frame.backShape[(s3X/18) + (s3Y/18)*10].getIcon() != null) ){
           return true;
        }
      return false;
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -