俄罗斯方块 - 方块旋转移动CLASS

    技术2022-05-20  30

    public class Root//8个块的根类,具有一般性{        private GameTable gTable;//创建游戏桌子        private int x,y;//方块坐上角现在在桌子上的位置        private int[] upleft = {0,0};//储存判断方块的左上角在4*4矩阵所在位置        private int[] downright = {0,0};//方块的长度和宽度        public int[][] coordinate;//方块形状的4*4坐标

     

     

     private boolean begin()//初始化显示 {                judge();//记录方块在4*4矩阵中的实际坐标  //判断用于初始化的坐标是否被占用                x = (gTable.x-1)/2;//记录左上角在游戏桌的坐标                y = 0;                for(int i=0;i<=downright[0];i++)//判断游戏桌是否有被占用                    for(int j=0;j<=downright[1];j++)                        if(coordinate[i+upleft[0]][j+upleft[1]]==1)                            if(gTable.myTable[x+i][y+j]==1)                                return false;                write(0,0);                return true; }

     

     private boolean down()//向下移动 {            if((y+downright[1]) != (gTable.y-1))//判断是否已经到达游戏桌底部            {               for(int i=0;i<=downright[0];i++)                    for(int j=0;j<=downright[1];j++)                        if(coordinate[i+upleft[0]][j+upleft[1]]==1)//判断矩阵里方块的位置                        {                            if((upleft[1]+j)==3)                            {                                if(gTable.myTable[x+i][y+j+1]==1)//判断方块下方在游戏桌上是否有被占用                                    return false;                            }                            else                                if(coordinate[i+upleft[0]][j+upleft[1]+1]==0)//判断方块下方是否属于方块自身                                    if(gTable.myTable[x+i][y+j+1]==1)//判断方块下方在游戏桌上是否有被占用                                        return false;                        }                write(0,1);                return true;            }            else                return false; }

     

     private boolean left()//判断是否能向左移动 {               if(x>0)//判断方块是否在游戏盘左边沿               {                   for(int i=0;i<=downright[0];i++)                         for(int j=0;j<=downright[1];j++)                                if(coordinate[i+upleft[0]][j+upleft[1]]==1)//判断矩阵里方块的位置                                     if(i==0)                                     {                                         if(gTable.myTable[x+i-1][y+j]==1)//判断方块左方在游戏桌上是否有方块                                                      return false;                                     }                                     else                                         if(coordinate[i+upleft[0]-1][j+upleft[1]]==0)//判断方块左方是否属于方块自身                                                if(gTable.myTable[x-1][y+j]==1)//判断方块左方在游戏桌上是否有方块                                                      return false;                   write(-1,0);                   return true;               }               return false; }

     

     private boolean right()//同down() {               if((x+downright[0])<(gTable.x-1))//判断方块是否在游戏盘右边沿               {                   for(int i=0;i<=downright[0];i++)                         for(int j=0;j<=downright[1];j++)                                if(coordinate[i+upleft[0]][j+upleft[1]]==1)//判断矩阵里方块的位置                                {                                     if(i==downright[0])                                     {                                         if(gTable.myTable[x+i+1][y+j]==1)//判断方块右方在游戏桌上是否有方块                                             return false;                                     }                                     else                                         if(coordinate[i+upleft[0]+1][j+upleft[1]]==0)//判断方块右方是否属于方块自身                                                if(gTable.myTable[x+i+1][y+j]==1)//判断方块右方在游戏桌上是否有方块                                                      return false;                                }                   write(1,0);                   return true;               }               return false; }

     

     private boolean change()//同down() {            int coordinatetemp[][] = new int[8][8];//判断方块是否能变化时需要用到的coordinate的备份            int[] uplefttemp = {0,0};//判断方块是否能变化时需要用到的upleft的备份            int[] downrighttemp = {0,0};//判断方块是否能变化时需要用到的downright的备份

                for(int i=0;i<=downright[0];i++)//擦除变换前的方块               for(int j=0;j<=downright[1];j++)                   if(coordinate[i+upleft[0]][j+upleft[1]]==1)                       gTable.myTable[x+i][y+j]=0;

                //对变换前的4*4矩阵做备份,若无法变换则提取备份            for(int i=0;i<4;i++)                for(int j=0;j<4;j++)                    coordinatetemp[3-j][i]=coordinate[i][j];            for(int i=0;i<4;i++)                for(int j=0;j<4;j++)                    coordinate[i][j] = coordinatetemp[i][j];            for(int i=0;i<4;i++)                for(int j=0;j<4;j++)                    coordinatetemp[j][3-i]=coordinate[i][j];            uplefttemp[0] = upleft[0];            uplefttemp[1] = upleft[1];            downrighttemp[0] = downright[0];            downrighttemp[1] = downright[1];            judge();//记录变换后矩阵的数据

                //对是否能进行变换进行检测            for(int i=0;i<=downright[0];i++)                for(int j=0;j<=downright[1];j++)                    if(coordinate[i+upleft[0]][j+upleft[1]]==1)                    {                        if((y+downright[1])>(gTable.y-1)||(x+downright[0])>(gTable.x-1))//判断变换后是否超出下边框和右边框                        {                            upleft[0] = uplefttemp[0];                            upleft[1] = uplefttemp[1];                            downright[0] = downrighttemp[0];                            downright[1] = downrighttemp[1];                            for(i=0;i<4;i++)                                for(j=0;j<4;j++)                                    coordinate[i][j] = coordinatetemp[i][j];                            write(0,0);                            return false;                        }                        // 判断变换后的位置是否与变换前重叠,若无重叠,则再次判断游戏桌上变换后的位置是否有方块                        if(coordinatetemp[i+uplefttemp[0]][j+uplefttemp[1]]==0)                            if(gTable.myTable[x+i][y+j]==1)                            {                                upleft[0] = uplefttemp[0];                                upleft[1] = uplefttemp[1];                                downright[0] = downrighttemp[0];                                downright[1] = downrighttemp[1];                                for(i=0;i<4;i++)                                    for(j=0;j<4;j++)                                        coordinate[i][j] = coordinatetemp[i][j];                                write(0,0);                                return false;                                }                    }            write(0,0);//画上变换后的方块            return true; }

     

     private void downTo()//一下到底 {      boolean canDown=true;      while(canDown)  {   canDown=down();//循环向下移动一格  } }

     

     private void judge()//此函数为以上函数调用的函数,判断方块在4*4矩阵里左上角坐标,判断方块的宽度 {            int n = 0;//记录方块在4*4矩阵中的最下方方块的y轴坐标,

                upleft[0]=0;//初始化upleft            upleft[1]=0;            downright[0]=0;//初始化downright            downright[1]=0;            for(int i=0,q=0;i<4;i++)//q判断是否已记录方块左边缘在4*4矩阵的位置                for(int j=0;j<4;j++)                    if(coordinate[i][j]==1)                    {                        if(q==0)//第一个方块的x轴坐标为方块在4*4矩阵中左上角的x轴坐标                        {                            upleft[0] = i;                            upleft[1] = j;                            q = 1;                        }                        if(upleft[1] > j)//最上方方块的y轴坐标为方块在4*4中左上角的y轴坐标                            upleft[1] = j;                        if(n < j)//记录最下方方块的y轴坐标                            n = j;                        downright[0] = i-upleft[0];//判断方块的宽度                        downright[1] = n-upleft[1];//判断方块的高度                    } }

     

       private void write(int a,int b)//此函数为以上函数调用的函数,把移动后的方块画入游戏盘,a为x轴移动距离,b为y轴移动距离        {           for(int i=0;i<=downright[0];i++)//擦除移动前的方块               for(int j=0;j<=downright[1];j++)                   if(coordinate[i+upleft[0]][j+upleft[1]]==1)                       gTable.myTable[x+i][y+j]=0;            x+=a;            y+=b;            for(int i=0;i<=downright[0];i++)//画入移动后的方块                for(int j=0;j<=downright[1];j++)                    if(coordinate[i+upleft[0]][j+upleft[1]]==1)                        gTable.myTable[x+i][y+j]=1;        }}

     

     

     

     

     

    以上为方块的旋转移动的ROOT类,以4*4点阵形式运算和处理,游戏桌设计为15*20的点阵。

     

    当需要创建一个方块时,需要新建一个类,并继承ROOT类,并给coordinate赋值。

     

    在游戏桌上使用方块实例的方法

     

     

    以下为I型方块创建实例:

     

    public class One extends Root//第一个方块{        public One()        {                //I字型方块                coordinate = new int[][]                {                     {1,1,1,1},                     {0,0,0,0},                     {0,0,0,0},                     {0,0,0,0}                };

             }

     

             public static void main(String args[])         {             new One();         }}

     


    最新回复(0)