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

📄 gamedata.java

📁 Rusfk:是一个俄罗斯方块实现在手机上玩的所有游戏功能
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class GameData {
 
  //****************数据模块****************//
  class MyCell
  {
   public byte ab[][]=new byte[4][2];  //以旋转点所在单元为零点的单元相对坐标
   public byte nextindex=0;    //下一个单元的索引号
  }
  
  //游戏区各个典型单元的起始屏幕规格坐标
  byte gameboxab[][]={
       {5,1}, //# 0
       {5,1}, //l1 1
       {5,1}, //Z1 3
       {6,1}, //S1 5
       {5,1}, //T1 7
       {5,1}, //J1 11
       {6,1}, //L1 15
       };  
 
  //单元相对坐标,存储相对坐标信息和下次变形的索引号
  //[19][9]
  private byte tmpbox[][]={
    {0,0, 1, 0, 0, 1, 1, 1, 0}, //#
    {0,0, 1, 0, 2, 0,-1, 0, 2}, //l1
    {0,0, 0,-1, 0, 1, 0, 2, 1}, //l2
    {0,0,-1,-1, 0,-1, 1, 0, 4}, //Z1
    {0,0, 0, 1, 1,-1, 1, 0, 3}, //Z2
    {0,0,-1, 0, 0,-1, 1,-1, 6}, //S1
    {0,0,-1,-1,-1, 0, 0, 1, 5}, //S2
    {0,0,-1, 0, 1, 0, 0, 1, 8}, //T1
    {0,0, 0,-1, 0, 1,-1, 0, 9}, //T2
    {0,0,-1, 0, 1, 0, 0,-1,10}, //T3
    {0,0, 0,-1, 0, 1, 1, 0, 7}, //T4
    {0,0,-1, 0, 1, 0, 1, 1,12}, //J1
    {0,0, 0,-1, 0, 1,-1, 1,13}, //J2
    {0,0,-1,-1,-1, 0, 1, 0,14}, //J3
    {0,0, 0,-1, 0, 1, 1,-1,11}, //J4
    {0,0,-1, 0,-1, 1, 1, 0,16}, //L1
    {0,0,-1,-1, 0,-1, 0, 1,17}, //L2
    {0,0,-1, 0, 1, 0, 1,-1,18}, //L3
    {0,0, 0,-1, 0, 1, 1, 1,15}, //L4
    };
  
  //单元存在与否标志
  //[22][12]
  private byte tmpbin[][]={
    {9,9,9,9,9,9,9,9,9,9,9,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,0,0,0,0,0,0,0,0,0,0,9},
    {9,9,9,9,9,9,9,9,9,9,9,9},
    }; 
 
  MyCell  m_cell[] = new MyCell[20];  //单元的各种变形的信息
  private byte lines=0;
  private byte  m_nowboxindex=0;    //当前运动单元类型
  private byte  m_nowcentera=0;     //当前运动单元旋转方块的屏幕规格坐标
  private byte  m_nowcenterb=0;
  private byte    m_style=0;      //当前运动单元的典型类型索引,配合boxindex获取单元信息
  
  byte  bin1[][] = new byte[22][12];   //单元相对坐标信息(静态-已落地单元)
  byte  bin2[][] = new byte[22][12];   //单元相对坐标信息(动态-行进中单元)
  //典型单元的索引 
  byte boxindex[]= {
    0,2,3,5,9,11,15,
    }; 
  
  //**************功能模块***************//
  
  //构造函数,分配单元
  public GameData()
  {
   for(int i=0;i<20;++i)
    {
     m_cell[i]=new MyCell();
    }
  }
  
  //初始化变量,赋值单元信息和标志数组
  void InitRusHigh()
   {
    lines=0;        //收行数量
    m_nowboxindex=0;      //当前运动单元类型
    m_nowcentera=0;       //单元旋转方块的规格化坐标
    m_nowcenterb=0;
    m_style=0;
    
    for(int i=0;i<19;i++)
    {
     for(int j=0;j<9;j++)
     {
      if(j<8)
      {//赋值相对坐标
       int b=(j/2);
       int a=(j%2);
       m_cell[i].ab[b][a]=tmpbox[i][j];
      }
      else
      {//赋值下次变形索引号
       m_cell[i].nextindex=tmpbox[i][j];
      }
     }
    }
    
    for(int b=0;b<22;b++)
    {
     for(int a=0;a<12;a++)
     {//赋值标志
      bin1[b][a]=tmpbin[b][a];
      bin2[b][a]=tmpbin[b][a];
     }
    }
   }
  
  //设置旋转点信息
  void SetCenterinfo(byte a,byte b,byte boxindex)
   {
    m_nowcentera=a;
    m_nowcenterb=b;
    m_nowboxindex=boxindex;   
   }
  
  //清除动态标志
  void clearbinpath()
  {
   byte tmpx,tmpy;
   for(byte i=0;i<4;i++)
   {
    tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
    tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
    bin2[tmpy][tmpx]=0;
   }  
  }
  //设置动态标志
  void setbinpath()
  {
   byte tmpx,tmpy;
   for(byte i=0;i<4;i++)
   {
    tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
    tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
    bin2[tmpy][tmpx]=this.m_style;
   }  
  } 
  
  //设置当前单元典型号
  void setnowstyle(byte style)
  {
   this.m_style=(byte)(style+1);
  }
  
  //设置落地单元标志
  void setbindown()
  {
   this.clearbinpath();
   byte tmpx,tmpy;
   for(byte i=0;i<4;i++)
   {
    tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
    tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
    bin1[tmpy][tmpx]=this.m_style;
   }  
  }  
  
  //下降
  void  SetToDown()
   {
    clearbinpath();
    m_nowcenterb++;
    setbinpath();
   }
  //左移
  void  SetToLeft()
   {
    clearbinpath();
    m_nowcentera--;
    setbinpath();
   }
  //右移
  void  SetToRight()
   {
    clearbinpath();
    m_nowcentera++;
    setbinpath();
   }
  //变形
  void  SetToRound()
   {
    clearbinpath();
    m_nowboxindex=this.m_cell[m_nowboxindex].nextindex;
    setbinpath();
   }
 
  //各项碰装检测
  //检测左移
  //返回:TRUE可以左移
  boolean checkleft()
   {
    boolean ret=true;
    byte tmpx,tmpy;
    for(byte i=0;i<4;i++)
    {
     tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
     tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
     if(bin1[tmpy][tmpx-1]>0)
      {ret=false;break;}
    }  
    return ret;
   } 
  
  //检测右移
  //返回:TRUE可以右移
  boolean checkright()
   {
    boolean ret=true;
    byte tmpx,tmpy;
    for(byte i=0;i<4;i++)
    {
     tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
     tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
     if(bin1[tmpy][tmpx+1]>0)
      {ret=false;break;}
    }  
    return ret;
   }
  
  //检测是否可以下降
  //返回:TRUE可以下降
  boolean checkdown()
   {
    boolean ret=true;
    byte tmpx,tmpy;
    for(byte i=0;i<4;i++)
    {
     tmpx=(byte)(m_nowcentera+m_cell[m_nowboxindex].ab[i][0]);
     tmpy=(byte)(m_nowcenterb+m_cell[m_nowboxindex].ab[i][1]);
     if(bin1[tmpy+1][tmpx]>0)
      {ret=false;break;}
    }  
    return ret;
   }
  
  //检测是否可以变形
  //返回:TRUE可以变形
  boolean checkround()
   {
    boolean ret=true;
    byte tmpx,tmpy;
    int index=m_cell[m_nowboxindex].nextindex;
    for(int i=0;i<4;i++)
    {
     tmpx=(byte)(m_nowcentera+m_cell[index].ab[i][0]);
     tmpy=(byte)(m_nowcenterb+m_cell[index].ab[i][1]);
     if(bin1[tmpy][tmpx]>0)
      {ret=false;break;}
    }   
    return ret;
   }
  
  //检测游戏结束
  //返回:TRUE游戏结束
  boolean checkover()
   {
    boolean ret=false;
    for(byte x=4;x<=7;x++)
    {
     if(bin1[1][x]>0)
      {ret=true;break;}
    }   
    return ret;
   }
  
  //检测收行
  //返回: TRUE存在收行,lines收行数
  boolean checkline()
   {
    boolean ret=false;
    lines=0;
    
    for(byte i=0;i<4;i++)
    {
     for(byte y=20;y>=2;y--)
     {
      if(bin1[y][1]==0 || bin1[y][2]==0 || bin1[y][3]==0 || bin1[y][4]==0 || bin1[y][5]==0 || bin1[y][6]==0 || bin1[y][7]==0 || bin1[y][8]==0 || bin1[y][9]==0 || bin1[y][10]==0)
      {//当前检测行存在一个空位则继续往上寻找
        continue;
      }
      else
      {//当前检测行连接成功
       lines++;
       for(int y2=(y-1);y2>=1;y2--)
       {//上方单元标志移下一格
        for(byte x=1;x<=10;x++)
         {
          bin1[y2+1][x]=bin1[y2][x];
         }
       }
       break;
      }
     }
    }
    if(lines>0)
    {
     ret=true;
    }
    return ret;
   }
}

⌨️ 快捷键说明

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