packagecode;
//import java.awt.*;
//import java.awt.Canvas;
//import java.awt.event.*;
//import javax.swing.*;
importjava.util.Random;
importjavax.microedition.lcdui.*;//写界面所需要的包
/** *//**
* 俄罗斯方块
* 高雷
* 2007年11月30日
*/
publicclasscGameextendsCanvasimplementsRunnable
...{
privateRandom rand;
privateThread thread;
privateGraphics gb;
privateImage buffer;
privateImage gameOverImg;//游戏结束
privatestaticfinalints_width =240;
privatestaticfinalints_height =320;
privatestaticfinalints_box_w =16;
privatestaticfinalints_box_h =16;
privatestaticfinalints_box_w_sum =10;//操作区域宽 格子数
privatestaticfinalints_box_h_sum =20;//操作区域高 格子数
privatestaticfinalints_line_between_x = s_box_w * s_box_w_sum;//分割线x位置
publicstaticfinalintUP = -1;
publicstaticfinalintDOWN = -2;
publicstaticfinalintLEFT = -3;
publicstaticfinalintRIGHT = -4;
publicstaticfinalintinit_x =3;//当前方块初始化坐标X
publicstaticfinalintinit_y =0;//当前方块初始化坐标y
publicstaticints_box_x = init_x;//当前方块坐标X
publicstaticints_box_y = init_y;//当前方块坐标Y
privatestaticintlevel =1;//等级
privatestaticintsuccess =0;//得分
privatestaticlonggoDownDelayTime[] =//1800; //下降延迟时间
...{
1000,900,800,700,
600,500,400,
300,200,100
};
privatestaticintlevel_up = (int)(goDownDelayTime[0]-goDownDelayTime[level]);//升级成绩
privatestaticbooleanisShowReseau =true;//是否现实网格
privatestaticshorts_next_box =0;//下一个方块编号
privatestaticshortboxColor;//当前box的颜色
// private static final Color gameBG = new Color( 0x333333 ); //游戏区域背景颜色
privatestaticfinalintgameBG =0x333333;//游戏区域背景颜色
// private static final Color gameColor[] = new Color[]
privatestaticfinalintgameColor[] =newint[]
...{
0x444444,//new Color( 0x444444 ), //网格颜色
0xEEEEEE,//new Color( 0xEEEEEE ), //方块颜色
0xEE0000,//new Color( 0xEE0000 ),
0x00EE00,//new Color( 0x00EE00 ),
0x0000EE,//new Color( 0x0000EE ),
0xEE00EE,//new Color( 0xEE00EE ),
0xEEEE00,//new Color( 0xEEEE00 ),
0x00EEEE//new Color( 0x00EEEE )
};
privatestaticfinalshortbox_sum[][] =newshort[][]//所有方块图形
...{
...{0x0660,0x0660,0x0660,0x0660},
...{0x2222,0x00F0,0x2222,0x00F0},
...{0x0264,0x0630,0x0264,0x0630},
...{0x0462,0x0360,0x0462,0x0360},
...{0x02E0,0x4460,0x0740,0x0622},
...{0x0E20,0x2260,0x0470,0x0644},
...{0x0464,0x00E4,0x04C4,0x04E0}
};
privatestaticshortnext_box[] =newshort[]...{0x0660,0x0660,0x0660,0x0660};
privatestaticshortbox[] =newshort[]...{0x0660,0x0660,0x0660,0x0660};
privatestaticshortmap[][];//地图
privatestaticshortbox_state =0;//当前BOX的状态//旋转方向
privatestaticshortmatrix[][] =//定义矩阵用来计算出box_sum的方块
...{
...{0x1000,0x0100,0x0010,0x0001},
...{0x2000,0x0200,0x0020,0x0002},
...{0x4000,0x0400,0x0040,0x0004},
...{ (short)0x8000,0x0800,0x0080,0x0008}
};
publiccGame()
...{
setFullScreenMode(true);//设置游戏为全屏幕模式,该函数只能在支持midp2.0的手机上使用
// s_width = getWidth(); //得到屏幕尺寸 宽
// s_height= getHeight(); //得到屏幕尺寸 高
rand =newRandom( System.currentTimeMillis() );
try
...{
//gameOverImg = Toolkit.getDefaultToolkit().getImage("src/pics/laser.png");
gameOverImg = Image.createImage("/pics/laser.png");
}catch(Exception e)...{}
//setSize( s_width, s_height ); //设置画布
initGame();//游戏初始化
thread =newThread(this);
thread.start();
}
privatevoidinitGame()
...{
level =1;//等级
success =0;//得分
map =newshort[s_box_h_sum][s_box_w_sum];
setNextBox();//设置下一个BOX
setBox();//将下一个BOX设置成当前BOX
setGameOver(false);//恢复游戏
}
privatevoidsetBox()//将next_box设置成当前可控制box
...{
box_state =0;//box 状态
s_box_x = init_x;//当前方块坐标X
s_box_y = init_y;//当前方块坐标Y
boxColor = s_next_box;//设置当前BOX颜色
System.arraycopy( next_box,0, box,0, next_box.length );//box = next_box
goDownPreTime = System.currentTimeMillis();//设置好当前BOX后 计时
setNextBox();//设置下一个BOX
if( !isCanMove() )
...{
setGameOver(true);
}
}
publicstaticbooleanisGameOver =false;
publicstaticlongupdatas =0;
publicstaticlongfps =0;
privatelongstartTime, beginTime, endTime;
privatelongdelay =25;
privatelongupTime =25;
publicvoidrun()
...{
while(true)
...{
try
...{
beginTime = System.currentTimeMillis();
updatas++;
updata( updatas );
repaint();
endTime = System.currentTimeMillis();
upTime = endTime-beginTime;
if( upTime
...{
fps =1000/delay;
thread.sleep(delay-upTime);
}
else
fps =1000/upTime;
}catch(Exception e)...{ }
}
}
voidsetGameOver(boolean_isGameOver )
...{
isGameOver = _isGameOver;
}
publicvoidupdata(longupdatas )
...{
}
publicvoidupdate(Graphics g)
...{
paint(g);
}
publicstaticintoffx =0;
publicstaticintoffy =0;
publicvoidpaint(Graphics g)
...{
try
...{
if( buffer ==null)
...{
buffer = Image.createImage( s_width, s_height );//设置画布缓冲区
gb = buffer.getGraphics();//得到绘图设备
}
// gb.translate( offx, offy );
// gb.setColor( new Color( 0x0 ) ); //初始化 画布颜色
gb.setColor(0x0);//初始化 画布颜色
gb.setClip (0,0, s_width, s_height);//初始化 画布区域
gb.fillRect(0,0, s_width, s_height);//初始化 画布填充
paintReseau( gb );//绘制网格
paintNextBox( gb );//绘制下一BOX
paintMap( gb );//绘制地图上不可以动BOX
paintBox( gb, s_box_x, s_box_y );//绘制当前可控制BOX
// gb.setColor( new Color( 0xFF3333 ) ); //分割线颜色
gb.setColor(0xFF3333);//分割线颜色
gb.drawLine( s_line_between_x,0, s_line_between_x, s_height );//分割线
// gb.drawString( "FPS:"+fps, s_line_between_x+10,10 ); //祯数
// gb.drawString( "等级:"+level, s_line_between_x+10,30 ); //等级
// gb.drawString( "得分:"+success, s_line_between_x+10,50 ); //分数
gb.drawString("FPS:"+fps, s_line_between_x+10,10, g.TOP|g.LEFT );//祯数
gb.drawString("等级:"+level, s_line_between_x+10,30, g.TOP|g.LEFT );//等级
gb.drawString("得分:"+success, s_line_between_x+10,50, g.TOP|g.LEFT );//分数
if( isGameOver )
...{
// gb.drawImage( gameOverImg, (getWidth()-offx-gameOverImg.getWidth(null))/2, (getHeight()-gameOverImg.getHeight(null))/2 , null );
gb.drawImage( gameOverImg, s_width>>1, s_height>>1, g.HCENTER|g.VCENTER );
}
// gb.translate( -offx, -offy );
}
catch(Exception e)
...{
System.out.println("err at paint.e====="+e);
}
// g.drawImage( buffer, offx, offy, null); //将画布缓冲区绘制到屏幕//偏移 (2,2)
g.drawImage( buffer, offx, offy,0);//将画布缓冲区绘制到屏幕//偏移 (2,2)
}
privatevoidpaintReseau( Graphics g )//绘制网格
...{
g.setColor( gameBG );
g.fillRect(0,0, s_line_between_x, s_height );
if( isShowReseau )
...{
g.setColor( gameColor[0] );
for(inti=0; i
...{
g.drawLine( i*s_box_h,0, i*s_box_h, s_height );
}
for(intj=0; j
...{
g.drawLine(0, j*s_box_w, s_line_between_x, j*s_box_w );
}
}
}
privatevoidpaintBox( Graphics g,intoff_x,intoff_y )
...{
for(inti=0; i<4; i++ )//行
...{
for(intj=0; j<4; j++ )//列
...{
if( (box[box_state] & matrix[i][j]) == matrix[i][j] )
...{
g.setColor( gameColor[ boxColor ] );
g.fillRect( (off_x+j)*s_box_w, (off_y+i)*s_box_h, s_box_w, s_box_h );
g.setColor( gameBG );
g.drawRect( (off_x+j)*s_box_w+1, (off_y+i)*s_box_h+1, s_box_w-2, s_box_h-2);
}
}
}
goDown();//BOX是否下降
}
privatevoidpaintNextBox( Graphics g )
...{
intoff_x = s_line_between_x+( s_width - s_line_between_x -4*s_box_w )/2;
intoff_y = s_height/2;
g.translate( off_x, off_y );
g.setColor( gameBG );
g.fillRect(0,0,4*s_box_w,4*s_box_h );
if( isShowReseau )//显示格式
...{
g.setColor( gameColor[0] );
for(inti=0; i<5; i++ )// |
...{
g.drawLine( i*s_box_h,0, i*s_box_h,4*s_box_h );
}
for(intj=0; j<5; j++ )// -
...{
g.drawLine(0, j*s_box_w,4*s_box_w, j*s_box_w );
}
}
for(inti=0; i<4; i++ )//行
...{
for(intj=0; j<4; j++ )//列
...{
if( (next_box[0] & matrix[i][j]) == matrix[i][j] )
...{
g.setColor( gameColor[ s_next_box ] );
g.fillRect( j*s_box_w, i*s_box_h, s_box_w, s_box_h );
g.setColor( gameBG );
g.drawRect( j*s_box_w+1, i*s_box_h+1, s_box_w-2, s_box_h-2);
}
}
}
g.translate( -off_x, -off_y );
}
privatelonggoDownPreTime =0;//上次下降时间
privatelongcurrTime =0;//当前时间
privatevoidgoDown()//当前BOX下降
...{
if( isGameOver )//游戏结束
return;
//isKeyDown按了向下移动就需要检查 不需要时间
if( isKeyDown==1|| System.currentTimeMillis() - goDownPreTime >= goDownDelayTime[level] )
...{
s_box_y++;
goDownPreTime = System.currentTimeMillis();
if( !isCanMove() )
...{
isKeyDown =0;//没有按下
s_box_y--;
setMap();//将BOX放进map
setBox();//新的BOX
}
}
}
privatevoidsetMap()
...{
for(inti=0; i<4; i++ )//行
...{
for(intj=0; j<4; j++ )//列
...{
if( ( box[box_state] & matrix[i][j] ) == matrix[i][j] )//是格子
...{
map[s_box_y+i][s_box_x+j] = boxColor;
}
}
}
//检测是否可以消去一行
intline_success =0;
for(inti=0; i
...{
if( isFullLine( i ) )//这行可以消去
...{
setNullLine( i );//设置第i行为空
setGoDownMap( i );//地图第i行以上的向下移动一行
line_success++;
}
}
success += line_success*line_success;//设置得分
level_up = (int)(goDownDelayTime[0]-goDownDelayTime[level]);
if( success >= level_up )//设置升级
...{
level %= goDownDelayTime.length;
level ++;
}
}
privatevoidpaintMap( Graphics g )
...{
for(inti=0; i
...{
for(intj=0; j
...{
if( map[i][j] >0)//是格子//绘制格子
...{
g.setColor( gameColor[ map[i][j] ] );
g.fillRect( j*s_box_w, i*s_box_h, s_box_w, s_box_h );
g.setColor( gameBG );
g.drawRect( j*s_box_w+1, i*s_box_h+1, s_box_w-2, s_box_h-2);
}
}
}
}
privatebooleanisFullLine(intline)//是否一行已经满了
...{
for(intj=0; j
...{
if( map[line][j] <=0)
...{
returnfalse;
}
}
returntrue;
}
privatevoidsetNullLine(intline )//设置地图上的这一行 空
...{
for(intj=0; j
...{
map[line][j] =0;
}
}
privatevoidsetGoDownMap(intline )//设置地图line以上的每行都向下移动一行
...{
for(inti=line; i>0; i-- )//行
...{
for(intj=0; j
...{
map[i][j] = map[i-1][j];//向下移动一行
}
}
}
privatebooleanisCanMove()
...{
for(inti=0; i<4; i++ )//行
...{
for(intj=0; j<4; j++ )//列
...{
if( ( box[box_state] & matrix[i][j] ) == matrix[i][j] )//是格子
...{
if( s_box_x+j <0)//左边界检测
...{
System.out.println("left s_box_x="+s_box_x+" matrix["+i+"]["+j+"]="+matrix[i][j]);
returnfalse;
}
if( s_box_x+j > s_box_w_sum-1)//右边界检测
...{
System.out.println("right s_box_x="+s_box_x+" matrix["+i+"]["+j+"]="+matrix[i][j]);
returnfalse;
}
if( s_box_y+i > s_box_h_sum-1)//下边界检测
...{
System.out.println("down s_box_y="+s_box_y+" matrix["+i+"]["+j+"]="+matrix[i][j]);
returnfalse;
}
//地图格子检测
if( map[s_box_y+i][s_box_x+j] >0)
returnfalse;
}
}
}
returntrue;
}
privateshortisKeyDown =0;//0没有按下,1按下,2抬起
// public boolean keyDown(Event evt, int key)
publicvoidkeyPressed(intkey )
...{
key = getKeyCode( key );
switch( key )
...{
caseUP://顺时针旋转
isKeyDown =0;//0没有按下
box_state ++;
box_state %=4;
if( !isCanMove() )
...{
box_state --;
if( box_state<0)
box_state =3;
}
break;
caseDOWN://向下移动
if( isKeyDown ==2)
isKeyDown =1;
if( isKeyDown ==1)
...{
s_box_y ++;
if( !isCanMove() )
s_box_y --;
}
break;
caseLEFT://向左移动BOX
isKeyDown =0;//0没有按下
s_box_x --;
if( !isCanMove() )
s_box_x ++;
break;
caseRIGHT://向右移动BOX
isKeyDown =0;//0没有按下
s_box_x ++;
if( !isCanMove() )
s_box_x --;
break;
case53://数字5键
if( isGameOver )//游戏结束
initGame();//重新游戏
break;
case42:
if( isGameOver )//游戏结束
// System.exit(0); //退出游戏
Tetris.s_midlet.destroyApp(true);
break;
case48:
setBox();//新的BOX
break;
case49://是否显示网格
isShowReseau = !isShowReseau;
break;
}
repaint();//重新绘制屏幕
// return true;
}
publicvoidkeyRepeated(intkey )
...{
keyPressed( key );
}
publicvoidsetNextBox()
...{
s_next_box = (short)rand.nextInt( box_sum.length );
System.arraycopy( box_sum[s_next_box],0, next_box,0, next_box.length );
s_next_box++;
}
publicintgetKeyCode(intkey )
...{
System.out.println("key="+key );
switch( key )
...{
case1004:// up
case119:// w
case87:// W
case50:// 2
returnUP;
case1005:// down
case115:// s
case83:// S
case56:// 8
returnDOWN;
case1006:// left
case97:// a
case65:// A
case52:// 4
returnLEFT;
case1007:// right
case100:// d
case68:// D
case54:// 6
returnRIGHT;
default:
returnkey;
}
}
// public boolean keyUp(Event evt, int key)
publicvoidkeyReleased(intkey )
...{
isKeyDown =2;//释放按键
// return true;
}
// public boolean mouseDown(Event evt, int x, int y)
// {
// try
// {
System.out.println( "x="+x+" y="+y );
// }catch( Exception e){e.printStackTrace();}
this.repaint();
// return true;
// }
// public boolean mouseMove(Event evt, int x, int y)
// {
// try
// {
// //System.out.println( "x="+x+" y="+y );
// }catch( Exception e){e.printStackTrace();}
// return true;
// }
// public static void main(String[] args)
// {
// JFrame frame = new JFrame("俄罗斯方块 北京|雷神 QQ:38929568");
// final cGame dc = new cGame();
// frame.getContentPane().add(dc, BorderLayout.CENTER);
//
JButton button = new JButton("刷新");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dc.repaint();
}
});
frame.getContentPane().add(button, BorderLayout.SOUTH);
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(dc.s_width+10, dc.s_height+30);
// frame.setVisible(true);
// }
}