rbcanvas.java
来自「俄罗斯方块的功能完全实现.是个不错的程序哦,有兴趣的同志下来看看吧」· Java 代码 · 共 289 行
JAVA
289 行
package elsfk;
import javax.microedition.lcdui.*;
public class RbCanvas extends Canvas implements Runnable
{int[][] bcell=new int[4][4];
boolean pause=false;
int score;
int x,y;
Image copy;
Graphics gg;
int width,height;
int pWidth,pHeight;//屏幕的尺寸
private int[][] grid;
BoardCells bc;
int l,t;
Board board;
public RbCanvas()
{bc=new BoardCells();
board=new Board(100,180,10);
l=Board.BOARDWIDTH/Board.GRIDWIDTH-1;
t=Board.BOARDHEIGHT/Board.GRIDWIDTH-1;
pWidth=this.getWidth();
pHeight=this.getHeight();
x=Board.BOARDWIDTH/Board.GRIDWIDTH/2-2;
y=0;
grid=new int[Board.BOARDHEIGHT/Board.GRIDWIDTH][Board.BOARDWIDTH/Board.GRIDWIDTH];
copy=Image.createImage(pWidth,pHeight);//双缓存
gg=copy.getGraphics();
gg.setColor(0x00000000);
gg.fillRect(0,0,pWidth,pHeight);
board.DrawBoard(gg,5,5,true);
for(int i=0;i<Board.BOARDHEIGHT/Board.GRIDWIDTH;i++)
for(int j=0;j<Board.BOARDWIDTH/Board.GRIDWIDTH;j++)
grid[i][j]=0;
}
protected void start()//线程
{Thread t=new Thread(this);
bcell=bc.SetNewCells();
t.start();
}
protected synchronized void stop()throws InterruptedException
{pause=true;
wait();
}
protected synchronized void resume()
{notifyAll();
}
protected void keyPressed(int keyCode)//键盘事件
{int g=getGameAction(keyCode);
if(g==FIRE)
{clearCell(x,y,bcell);
bcell=bc.rotate();
if(x==l)
{x=l-3;
copycell(x,y,bcell);}
if(x==l-1)
{if(GetRowCol(bcell)[0]==1)
x=l-3;
else if(GetRowCol(bcell)[0]==GetRowCol(bcell)[1])
{}
else x=l-2;
copycell(x,y,bcell);}
if(x==l-2)
{if(GetRowCol(bcell)[0]==1)
x=l-3;
copycell(x,y,bcell);}
if(y==t)
{y=t-3;
copycell(x,y,bcell);}
if(y==t-1)
{if(GetRowCol(bcell)[1]==1)
y=t-3;
else if(GetRowCol(bcell)[1]==2)
y=t-1;
else y=t-2;
copycell(x,y,bcell);}
if(y==t-2)
{if(GetRowCol(bcell)[1]==1)
y=t-3;
copycell(x,y,bcell);}
else
copycell(x,y,bcell);
repaint();
}
if(g==LEFT)
{//if(grid[y][0]==0&&grid[y+1][0]==0&&grid[y+2][0]==0&&grid[y+3][0]==0)
{clearCell(x,y,bcell);
if(Movable(x-1,y,bcell))
{x-=1;
copycell(x,y,bcell);
repaint();}
else {copycell(x,y,bcell);
repaint();}
}
}
if(g==RIGHT)
{//if(grid[y][l]==0&&grid[y+1][l]==0&&grid[y+2][l]==0&&grid[y+3][l]==0)
{clearCell(x,y,bcell);
if(Movable(x+1,y,bcell))
{x+=1;
copycell(x,y,bcell);
repaint();}
else {copycell(x,y,bcell);
repaint();}}
}
if(g==DOWN)
{
//if((y+4)<=Board.BOARDHEIGHT/Board.GRIDWIDTH-1)
clearCell(x,y,bcell);
if(Movable(x,y+1,bcell))
{y+=1;
copycell(x,y,bcell);
repaint();
}
else {copycell(x,y,bcell);
repaint();}
}
}
protected void keyRepeated(int keyCode)
{
if(hasRepeatEvents())
{int g=getGameAction(keyCode);
if(g==DOWN)
{
//if((y+4)<=Board.BOARDHEIGHT/Board.GRIDWIDTH-1)
{clearCell(x,y,bcell);
if(Movable(x,y+1,bcell))
{y+=1;
copycell(x,y,bcell);
repaint();
}
else {copycell(x,y,bcell);
repaint();}
}
}
if(g==LEFT)
{}
if(g==RIGHT)
{}
if(g==FIRE)
{}
}
}
private void clearCell(int x,int y,int[][] bcell)
{int i,j,getx,gety;
int[] rc= new int[2];
rc=GetRowCol(bcell);
getx=rc[0];
gety=rc[1];
for(i=1;i<=getx;i++)
for(j=1;j<=gety;j++)
{if(bcell[i-1][j-1]==1)
grid[y+i-1][x+j-1]=0;
}
}
private void copycell(int x,int y,int[][] bcell)
{int i,j,getx,gety;
int[] rc= new int[2];
rc=GetRowCol(bcell);
getx=rc[0];
gety=rc[1];
for(i=1;i<=getx;i++)
for(j=1;j<=gety;j++)
{if(bcell[i-1][j-1]==1)
grid[y+i-1][x+j-1]=1;
}
}
private boolean Movable(int x,int y,int[][] bcell)
{int i,j,getx,gety;
int[] rc= new int[2];
rc=GetRowCol(bcell);
getx=rc[0];
gety=rc[1];
if((y+getx)>Board.BOARDHEIGHT/Board.GRIDWIDTH||(x+gety)>l+1||y<0||x<0)
{return false;
}
for(i=1;i<=getx;i++)
for(j=1;j<=gety;j++)
{if(grid[y+i-1][x+j-1]*bcell[i-1][j-1]==1)
return false;
}
return true;
}
private int[] GetRowCol(int[][] bcell)
{int i,j;
int[] rcsz=new int[2];
rcsz[0]=0;
rcsz[1]=0;
for(i=1;i<=4;i++)
{if(bcell[i-1][0]==1||bcell[i-1][1]==1||bcell[i-1][2]==1||bcell[i-1][3]==1)
rcsz[0]++;
if(bcell[0][i-1]==1||bcell[1][i-1]==1||bcell[2][i-1]==1||bcell[3][i-1]==1)
rcsz[1]++;
}
return rcsz;
}
protected void paint(Graphics g)
{gg.setColor(0x00000000);
gg.fillRect(0,0,pWidth,pHeight);
gg.setColor(0x00ffffff);
gg.drawString(String.valueOf(score),130,30,Graphics.LEFT|Graphics.TOP);
board.DrawBoard(gg,5,5,true);
for(int i=0;i<=Board.BOARDHEIGHT/Board.GRIDWIDTH-1;i++)
for(int j=0;j<=Board.BOARDWIDTH/Board.GRIDWIDTH-1;j++)
{if(grid[i][j]==1)
{gg.setColor(0x00ffffff);
gg.fillRect(j*Board.GRIDWIDTH+board.board_x,i*Board.GRIDWIDTH+board.board_y,
Board.GRIDWIDTH,Board.GRIDWIDTH);
}
}
g.drawImage(copy,0,0,Graphics.LEFT|Graphics.TOP);
}
public void run()
{while(pause==true)
{try{
stop();
}
catch(InterruptedException e1)
{System.out.println("aaaaaaa");}
}
while(true)
{if(x==-1) x=0;
{
clearCell(x,y,bcell);
if(Movable(x,y+1,bcell)) //这之前的句子有错!!!
{ y+=1;
copycell(x,y,bcell);
repaint();
}
else
{copycell(x,y,bcell);
repaint();
score+=clearLine();
y=0;
x=Board.BOARDWIDTH/Board.GRIDWIDTH/2-2;
bcell=bc.SetNewCells();
copycell(x,y,bcell);
repaint();}
try{
Thread.sleep(500);
}
catch(InterruptedException e)
{System.out.println("sleep err:"+e.getMessage());
}
}
}
}
public int clearLine()
{int clear=0;
boolean xH=true;
for(int i=Board.BOARDHEIGHT/Board.GRIDWIDTH-1;i>=4;i--)
{xH=true;
for(int j=0;j<=Board.BOARDWIDTH/Board.GRIDWIDTH-1;j++)
{if(grid[i][j]==0)
{xH=false;
break;}
}
if(xH==true)
{clear+=1;
for(int m=i;m>=4;m--)
for(int n=0;n<=l-1;n++)
{grid[m][n]=grid[m-1][n];//为什么要整行赋值,而不能整行赋值,比如:for(int t=i;t>=4;t++) grid[i]=grid[i-1];
}
i=i+1;}//由于每次所消行的前已行也可能是可以消去的,而完成一次外层循环后i要减1,这就导致跳过了一行,所以i要加1
}
return clear;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?