📄 block.java
字号:
package lyp.game;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
class block implements Runnable
{
public int currentX,currentY,priorX,priorY;
protected Random rand;
protected Thread blockthread;
static int step,score=0;
static boolean over;
protected int priorrot;
protected int thisrot,nextrot,pattern,next;
protected int thisblock[][][],nextblock[][][];
protected int blockpattern1[][][] = {
{
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
};
protected int blockpattern2[][][] = {
{
{
0, 0, 1, 0}
, {
0, 0, 1, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 0, 0}
, {
0, 1, 1, 1}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 0, 0}
, {
0, 1, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
1, 1, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 0, 0}
}
};
protected int blockpattern3[][][] = {
{
{
0, 1, 0, 0}
, {
0, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 1}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 1, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 0, 1, 0}
, {
1, 1, 1, 0}
, {
0, 0, 0, 0}
}
};
protected int blockpattern4[][][] = {
{
{
0, 0, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 1, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 0, 0, 0}
, {
1, 1, 1, 1}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 1, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 0, 0, 0}
, {
1, 1, 1, 1}
, {
0, 0, 0, 0}
}
};
protected int blockpattern5[][][] = {
{
{
0, 0, 0, 0}
, {
1, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 1, 0}
, {
0, 1, 1, 0}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
1, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 1, 0}
, {
0, 1, 1, 0}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
};
protected int blockpattern6[][][] = {
{
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
1, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 0, 0, 0}
, {
0, 1, 1, 0}
, {
1, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 0, 1, 0}
, {
0, 0, 0, 0}
}
};
protected int blockpattern7[][][] = {
{
{
0, 0, 0, 0}
, {
1, 1, 1, 0}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 1, 0, 0}
, {
1, 1, 0, 0}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 1, 0, 0}
, {
1, 1, 1, 0}
, {
0, 0, 0, 0}
, {
0, 0, 0, 0}
}
, {
{
0, 1, 0, 0}
, {
0, 1, 1, 0}
, {
0, 1, 0, 0}
, {
0, 0, 0, 0}
}
};
public block()
{
over=false;
thisblock=new int [4][4][4];
nextblock=new int [4][4][4];
rand=new Random();
next=nextrot=2;
getthisblock();
blockthread=new Thread(this);
blockthread.start();
}
public synchronized void getthisblock()
{
pattern=next;
thisrot=nextrot;
next=Math.abs(rand.nextInt())%7+1;
nextrot=Math.abs(rand.nextInt())%4;
priorX=currentX=5;
switch(pattern)
{
case 1:
copy(thisblock,blockpattern1);
break;
case 2:
copy(thisblock,blockpattern2);
break;
case 3:
copy(thisblock,blockpattern3);
break;
case 4:
copy(thisblock,blockpattern4);
break;
case 5:
copy(thisblock,blockpattern5);
break;
case 6:
copy(thisblock,blockpattern6);
break;
case 7:
copy(thisblock,blockpattern7);
break;
}
int i,j;
boolean flag=false;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(thisblock[thisrot][i][j]!=0)
{
flag=true;
break;
}
}
if(flag)
break;
}
priorY=currentY=-i;
paint(gameCanvas.graphics);
}
protected void copy(int b[][][],int a[][][])
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
{
thisblock[i][j][k]=a[i][j][k];
}
}
protected void paint(Graphics g)
{
g.setColor(0,0,255);
for(int i=0;i<4;i++)
{
if(currentY+i<0||currentY+i>15)
continue;
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11)
continue;
if(thisblock[thisrot][i][j]==1)
{
g.fillRect((priorX+j)*map.briLen,(priorY+i)*map.briLen+gameCanvas.top,map.briLen-1,map.briLen-1);
}
}
}
g.setColor(0,255,0);
for(int i=0;i<4;i++)
{
if(currentY+i<0||currentY+i>15)
continue;
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11)
continue;
if(thisblock[thisrot][i][j]==1)
{
g.fillRect((currentX+j)*map.briLen,(currentY+i)*map.briLen+gameCanvas.top,map.briLen-1,map.briLen-1);
}
}
}
}
protected synchronized boolean isdown(map m)
{
currentY++;
boolean flag=false;
for(int i=0;i<4;i++)
{
if(currentY+i<0||currentY+i>15)
continue;
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11)
continue;
else if(thisblock[thisrot][i][j]*m.grid[currentY+i][currentX+j]!=0 ||
currentY+i==15 && i<3 && thisblock[thisrot][i+1][j]==1)
{
flag=true;
break;
}
}
if(flag)
break;
}
currentY--;
return flag;
}
protected void down()
{
currentY++;
priorX=currentX;
priorY=currentY-1;
}
protected boolean isright(map m)
{
currentX++;
boolean flag=false;
for(int i=0;i<4;i++)
{
if(currentY+i<0||currentY+i>15)
continue;
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11)
continue;
else if(thisblock[thisrot][i][j]*m.grid[currentY+i][currentX+j]!=0)
{
flag=true;
break;
}
}
if(flag)
break;
}
currentX--;
return flag;
}
protected void right()
{
priorX=currentX;
priorY=currentY;
currentX++;
}
protected boolean isleft(map m)
{
currentX--;
boolean flag=false;
for(int i=0;i<4;i++)
{
if(currentY+i<0||currentY+i>15)
continue;
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11)
continue;
else if(thisblock[thisrot][i][j]*m.grid[currentY+i][currentX+j]!=0)
{
flag=true;
break;
}
}
if(flag)
break;
}
currentX++;
return flag;
}
protected void left()
{
priorX=currentX;
priorY=currentY;
currentX--;
}
protected void rot()
{
priorX=currentX;
priorY=currentY;
priorrot=thisrot;
thisrot++;
if(thisrot==4)
thisrot=0;
}
public void run()
{
while(over==false)
{
try {
Thread.sleep((7-step)*300);
} catch (InterruptedException e)
{
e.printStackTrace();
}
if(!isdown(gameCanvas.mymap))
{
down();
paint(gameCanvas.graphics);
}
else
{
setmap(gameCanvas.mymap,gameCanvas.graphics);
if(over==false)
getthisblock();
}
}
}
public synchronized void setmap(map m,Graphics g)
{
for(int i=0;i<4;i++)
{
if(currentX+i<0 || currentX+i>11)
continue;
for(int j=0;j<4;j++)
{
if(currentY+j<0 || currentY+j>15)
continue;
if(thisblock[thisrot][j][i]==1)
{
m.grid[currentY+j][currentX+i]=1;
}
}
}
for(int i = 1;i<11;i++)
{
if(m.grid[0][i]==1)
{
over=true;
break;
}
}
if(over!=true)
{
m.submit(currentX,currentY);
}
else
{
myGame.display.setCurrent(myGame.go);
}
m.drawMap(g);
}
protected boolean isrot(map m)
{
int temp;
temp=thisrot;
rot();
boolean flag=false;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(currentX+j<0||currentX+j>11 || currentY+i<0 || currentY+i>15)
{
if(thisblock[thisrot][i][j]==1)
{
flag=true;
break;
}
continue;
}
else if(thisblock[thisrot][i][j]*m.grid[currentY+i][currentX+j]!=0)
{
flag=true;
break;
}
}
if(flag)
break;
}
thisrot=temp;
return flag;
}
protected void paintrot(Graphics g)
{
g.setColor(0,0,255);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(thisblock[priorrot][i][j]==1)
{
g.fillRect((priorX+j)*map.briLen,(priorY+i)*map.briLen+gameCanvas.top,map.briLen-1,map.briLen-1);
}
}
g.setColor(0,255,0);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(thisblock[thisrot][i][j]==1)
{
g.fillRect((currentX+j)*map.briLen,(currentY+i)*map.briLen+gameCanvas.top,map.briLen-1,map.briLen-1);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -