android平台俄罗斯方块游戏完整代码
2012-12-02 14:31:58 来源:WEB开发网核心提示: public boolean availableForTile(int[][] tile,int x,int y) //方块旋转判断 {for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) {if (tile[i][j] != 0) { i
public boolean availableForTile(int[][] tile,int x,int y) //方块旋转判断
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (tile[i][j] != 0) {
if (!isSpace(x + i, y + j)) {
return false;
}
}
}
}
return true;
}
public boolean rotateOnCourt() { // 是否能够旋转,能返回true,方块按向右旋转
int tempX = 0, tempY = 0;
int tempShape;
int[][] tempTile = new int[4][4];
tempShape = k;
if (tempShape % 4 > 0) {
tempShape--;
} else {
tempShape += 3;
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
tempTile[i][j] = statefang.state[tempShape][i][j];
}
}
if (tempShape % 4 > 0) {
tempShape--;
} else {
tempShape += 3;
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
tempTile[i][j] = statefang.state[tempShape][i][j];
}
}
tempX = of_x;
tempY = of_y;
boolean canTurn = false;
cleanstate(of_x,of_y);
if( availableForTile(tempTile,tempX,tempY) )
{
canTurn = true;
}
else if(availableForTile(tempTile,tempX-1,tempY) )
{
canTurn = true;
tempX--;
}
else if(availableForTile(tempTile,tempX-2,tempY) )
{
canTurn =true;
tempX -=2;
}
else if(availableForTile(tempTile,tempX+1,tempY) )
{
canTurn = true;
tempX++;
}
else if(availableForTile(tempTile,tempX+2,tempY) )
{
canTurn = true;
tempX += 2;
}
if (canTurn) {
k = tempShape;
of_x = tempX;
of_y = tempY;
return true;
}
for(int i=0;i<4;i++) // 执行旋转之前屏幕数组和颜色数组回到当前值
{
for(int j=0;j<4;j++)
{
if(statefang.state[k][i][j] != 0)
{
m_screen[of_x+i][of_y+j] = 1;
m_color[of_x+i][of_y+j] = state;
}
}
}
return false;
}
public boolean isSpace(int x,int y) // 判断x、y 这个方块是否在超越边界 以及是否为原始值0
{
if(x< 0 || x>=of_Width)
return false;
if(y<0 || y>=of_Height)
return false;
if(m_screen[x][y] == 0)
return true;
return false;
}
public boolean moveDownOn() // 判断能否继续下落
{
int n = 0;
for(int i=0;i<4;i++)
{
for(int j =0;j<4;j++)
{
if(statefang.state[k][i][j] != 0)
{
n = i;
while(n<3 && statefang.state[k][n+1][j] != 0)
n++;
if(!isSpace(of_x+n+1,of_y+j))
return false;
}
}
}
return true;
}
public int cleanFullLine() // 清楚满行的行
{
int num = 0,n=0,linenum = 0;
num = brigeLine();
for(int i = of_x+3;i>=of_x-1;i--)
{
if(fullLine(i))
{
n = i;
for(int r=0;r<of_Height;r++)
{
log_color[r] = m_color[n][r];
}
linenum++;
for(int j = n;j>=num;j--)
{
for(int k=0;k<of_Height;k++)
{
if(j == num)
{
m_screen[j][k] = 0;
m_color[j][k] = -1;
}
else
{
m_screen[j][k] = m_screen[j-1][k];
m_color[j][k] = m_color[j-1][k];
}
}
}
for(int t=0;t<of_Height;t++) // 画出满行去掉的剪切态
{
m_Canvas.drawBitmap(m_GameResources.m_Bitmaps[log_color[t]],24+t*18,(n-4)*18+25,null);
}
MusicPlay.playSound(R.raw.delete1+linenum,0);
i = n+1;
}
}
return linenum;
}
public int brigeLine() //找出哪行没有方块 并返回行值 否则返回0
{
for(int i = of_x+3;i>0;i--)
{
int j = 0;
while(j<of_Height && m_screen[i][j] == 0)
j++;
if(j == of_Height)
return i+1;
}
return 0;
}
public boolean fullLine(int hang) // 判断改行是否是满行
{
int j = 0;
while(j<of_Height && m_screen[hang][j] == 1)
j++;
if(j == of_Height)
return true;
return false;
}
tempY = of_y;
boolean canTurn = false;
cleanstate(of_x,of_y);
if( availableForTile(tempTile,tempX,tempY) )
{
canTurn = true;
}
else if(availableForTile(tempTile,tempX-1,tempY) )
{
canTurn = true;
tempX--;
}
else if(availableForTile(tempTile,tempX-2,tempY) )
{
canTurn =true;
tempX -=2;
}
else if(availableForTile(tempTile,tempX+1,tempY) )
{
canTurn = true;
tempX++;
}
else if(availableForTile(tempTile,tempX+2,tempY) )
{
canTurn = true;
tempX += 2;
}
if (canTurn) {
k = tempShape;
of_x = tempX;
of_y = tempY;
return true;
}
for(int i=0;i<4;i++) // 执行旋转之前屏幕数组和颜色数组回到当前值
{
for(int j=0;j<4;j++)
{
if(statefang.state[k][i][j] != 0)
{
m_screen[of_x+i][of_y+j] = 1;
m_color[of_x+i][of_y+j] = state;
}
}
}
return false;
}
public boolean isSpace(int x,int y) // 判断x、y 这个方块是否在超越边界 以及是否为原始值0
{
if(x< 0 || x>=of_Width)
return false;
if(y<0 || y>=of_Height)
return false;
if(m_screen[x][y] == 0)
return true;
return false;
}
public boolean moveDownOn() // 判断能否继续下落
{
int n = 0;
for(int i=0;i<4;i++)
{
for(int j =0;j<4;j++)
{
if(statefang.state[k][i][j] != 0)
{
n = i;
while(n<3 && statefang.state[k][n+1][j] != 0)
n++;
if(!isSpace(of_x+n+1,of_y+j))
return false;
}
}
}
return true;
}
public int cleanFullLine() // 清楚满行的行
{
int num = 0,n=0,linenum = 0;
num = brigeLine();
for(int i = of_x+3;i>=of_x-1;i--)
{
if(fullLine(i))
{
n = i;
for(int r=0;r<of_Height;r++)
{
log_color[r] = m_color[n][r];
}
linenum++;
for(int j = n;j>=num;j--)
{
for(int k=0;k<of_Height;k++)
{
if(j == num)
{
m_screen[j][k] = 0;
m_color[j][k] = -1;
}
else
{
m_screen[j][k] = m_screen[j-1][k];
m_color[j][k] = m_color[j-1][k];
}
}
}
for(int t=0;t<of_Height;t++) // 画出满行去掉的剪切态
{
m_Canvas.drawBitmap(m_GameResources.m_Bitmaps[log_color[t]],24+t*18,(n-4)*18+25,null);
}
MusicPlay.playSound(R.raw.delete1+linenum,0);
i = n+1;
}
}
return linenum;
}
public int brigeLine() //找出哪行没有方块 并返回行值 否则返回0
{
for(int i = of_x+3;i>0;i--)
{
int j = 0;
while(j<of_Height && m_screen[i][j] == 0)
j++;
if(j == of_Height)
return i+1;
}
return 0;
}
public boolean fullLine(int hang) // 判断改行是否是满行
{
int j = 0;
while(j<of_Height && m_screen[hang][j] == 1)
j++;
if(j == of_Height)
return true;
return false;
}
更多精彩
赞助商链接