int color = g.getColor();
g.setColor( COLOR_WHITE );
g.drawRect( x, y, width, height );
g.setColor(c);
g.fillRect( x + 1, y + 1, width-2, height-2 );
g.setColor( COLOR_BLACK );
g.drawLine( x + width-1, y, x + width-1, y + height-1 );
g.drawLine( x, y + height-1, x + width-1, y + height-1 );
g.setColor(color);
}
public static boolean drawText(Graphics g, String str, int x, int y, int anchor, int color, int size) {
Font f_old,f_new;
int c_old;
try {
f_old = g.getFont();
f_new = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,size);
g.setFont(f_new);
c_old = g.getColor();
g.setColor(color);
g.drawString(str, x, y, anchor );
g.setColor(c_old);
g.setFont(f_old);
return true;
}catch (Exception ex) {
return false;
}
}
protected void paint(Graphics g){
//画背景
try{
Image image_Splash = Image.createImage("/back.png");
g.drawImage(image_Splash, 0, 0,Graphics.TOP | Graphics.LEFT);
}
catch(Exception ex) {
}
//画下一个要出现的方块
drawText(g, "下一个", 91, 5, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);
g.setColor(COLOR_GRAY);
g.drawRoundRect(91, 18, 26, 30, 2, 2);
g.setColor(COLOR_DARK_GRAY);
g.fillRoundRect(92, 19, 24, 28, 2, 2);
for(int i=0;i<=3;i++)
draw3DBlock(g, BlockInfo[FutureBlockType][8],
93+BlockInfo[FutureBlockType][i*2+1]*BLOCK_SIZE,
20+BlockInfo[FutureBlockType][i*2]*BLOCK_SIZE,
BLOCK_SIZE,BLOCK_SIZE);
drawText(g, "速度:"+String.valueOf(CurSpeed), 91, 60, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);
drawText(g, "行数:"+String.valueOf(BlockLines), 91, 75, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);
drawText(g, "成绩:", 91, 90, Graphics.TOP| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);
g.setColor(COLOR_GRAY);
g.drawRoundRect(91, 105, 26, 20, 2, 2);
g.setColor(COLOR_DARK_GRAY);
g.fillRoundRect(92, 106, 24, 18, 2, 2);
drawText(g, String.valueOf(BlockScore), 93, 107, Graphics.TOP| Graphics.LEFT, COLOR_WHITE, Font.SIZE_MEDIUM);
//画当前战况
for(int i=0;i<CANVAS_SIZE_HEIGHT-1;i++)
for(int j=1;j<CANVAS_SIZE_WIDTH-1;j++)
if (Gridmatrix[i][j]!=0)
draw3DBlock(g,Gridmatrix[i][j],CANVAS_OFFSET_X+j*BLOCK_SIZE,
CANVAS_OFFSET_Y+i*BLOCK_SIZE,
BLOCK_SIZE,BLOCK_SIZE);
if (!ISDOWN){
//画上新的方块
LastX=BlockX; LastY=BlockY; LastType=BlockType;
for(int i=0;i<=3;i++)
draw3DBlock(g,BlockInfo[BlockType][8],
CANVAS_OFFSET_X+BlockX*BLOCK_SIZE+BlockInfo[BlockType][i*2+1]*BLOCK_SIZE,
CANVAS_OFFSET_Y+BlockY*BLOCK_SIZE+BlockInfo[BlockType][i*2]*BLOCK_SIZE,
BLOCK_SIZE,BLOCK_SIZE);
}
}
private boolean feasible(){
for(int i=0;i<=3;i++)
if (Gridmatrix[BlockY+BlockInfo[BlockType][i*2]][BlockX+BlockInfo[BlockType][i*2+1]]!=0)
return false;
return true;
}
private void delline(){
for(int i=0;i<=3;i++)
Gridmatrix[BlockY+BlockInfo[BlockType][i*2]][BlockX+BlockInfo[BlockType][i*2+1]]=BlockInfo[BlockType][8];
int temp=4;
boolean CanSkip=false;
int i=CANVAS_SIZE_HEIGHT-2;
while((temp>0)&&(i>=1)){
CanSkip=false;
label1: for(int j=1;j<=CANVAS_SIZE_WIDTH-2;j++){
if (Gridmatrix[i][j]==0) {CanSkip=true; i--; break label1;}
}
if (!CanSkip){
temp--;
for(int k=i;k>=1;k--)
for(int l=1;l<=CANVAS_SIZE_WIDTH-2;l++)
Gridmatrix[k][l]=Gridmatrix[k-1][l];
BlockLines++;
BlockScore+=200;
if((BlockScore%2000)<200) CurSpeed++;
}
}
}
public void run() {
while (Blocker != null) {
if(!ISDOWN){
BlockY++;
if (!feasible()) {
ISDOWN=true; BlockY--; delline();
try {Thread.sleep(400);} catch (InterruptedException e){}
}
else{
repaint();
try {Thread.sleep(950-100*(int)BlockSpeed);} catch (InterruptedException e){}
}
}
else{ BlockScore+=50;
if((BlockScore%2000)<50) CurSpeed++;
ISDOWN=false;
repaint();
BlockSpeed=CurSpeed;
BlockType=FutureBlockType;
FutureBlockType=Math.abs(generator.nextInt()%19);
BlockX=4; LastX=BlockX;
BlockY=0; LastY=BlockY;
if (!feasible()) { init();}
}
}
Blocker = null;
}
protected void keyPressed(int keyCode) {
//处理按下键盘的事件,这是Canvas的实例方法
switch (getGameAction(keyCode)) {//将按键的值转化成方向常量
case Canvas.UP://向上
break;
case Canvas.DOWN://向下
BlockY++;
if (!feasible()) BlockY--;
repaint();
BlockSpeed=9;
//Blocker.run();
break;
case Canvas.LEFT://向左
BlockX--;
if (!feasible()) BlockX++;
break;
case Canvas.RIGHT://向右
BlockX++;
if (!feasible()) BlockX--;
break;
case Canvas.FIRE:
int tempBlockType=BlockType;
if (BlockType==1) BlockType=-1;
else if (BlockType==3) BlockType=1;
else if (BlockType==5) BlockType=3;
else if (BlockType==6) BlockType=5;
else if (BlockType==10) BlockType=6;
else if (BlockType==14) BlockType=10;
else if (BlockType==18) BlockType=14;
BlockType++;
if (!feasible()) BlockType=tempBlockType;
break;
default:
break;
}
repaint(); return;
}
}