📄 cgame.java
字号:
}
g.translate( -off_x, -off_y );
}
private long goDownPreTime = 0; //上次下降时间
private long currTime = 0; //当前时间
private void goDown() //当前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
}
}
}
private void setMap()
{
for( int i=0; i<4; i++ ) //行
{
for( int j=0; j<4; j++ ) //列
{
if( ( box[box_state] & matrix[i][j] ) == matrix[i][j] ) //是格子
{
map[s_box_y+i][s_box_x+j] = boxColor;
}
}
}
//检测是否可以消去一行
int line_success = 0;
for( int i=0; i<s_box_h_sum; 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 ++;
}
}
private void paintMap( Graphics g )
{
for( int i=0; i<s_box_h_sum; i++ ) //行
{
for( int j=0; j<s_box_w_sum; 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 );
}
}
}
}
private boolean isFullLine(int line) //是否一行已经满了
{
for( int j=0; j<s_box_w_sum; j++ ) //列
{
if( map[line][j] <= 0 )
{
return false;
}
}
return true;
}
private void setNullLine( int line ) //设置地图上的这一行 空
{
for( int j=0; j<s_box_w_sum; j++ ) //列
{
map[line][j] = 0;
}
}
private void setGoDownMap( int line ) //设置地图line以上的每行都向下移动一行
{
for( int i=line; i>0; i-- ) //行
{
for( int j=0; j<s_box_w_sum; j++ ) //列
{
map[i][j] = map[i-1][j]; //向下移动一行
}
}
}
private boolean isCanMove()
{
for( int i=0; i<4; i++ ) //行
{
for( int j=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]);
return false;
}
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]);
return false;
}
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]);
return false;
}
//地图格子检测
if( map[s_box_y+i][s_box_x+j] > 0 )
return false;
}
}
}
return true;
}
private short isKeyDown = 0; //0没有按下,1按下,2抬起
// public boolean keyDown(Event evt, int key)
public void keyPressed( int key )
{
key = getKeyCode( key );
switch( key )
{
case UP: //顺时针旋转
isKeyDown = 0; //0没有按下
box_state ++;
box_state %= 4;
if( !isCanMove() )
{
box_state --;
if( box_state<0 )
box_state = 3;
}
break;
case DOWN: //向下移动
if( isKeyDown == 2 )
isKeyDown = 1;
if( isKeyDown == 1 )
{
s_box_y ++;
if( !isCanMove() )
s_box_y --;
}
break;
case LEFT: //向左移动BOX
isKeyDown = 0; //0没有按下
s_box_x --;
if( !isCanMove() )
s_box_x ++;
break;
case RIGHT: //向右移动BOX
isKeyDown = 0; //0没有按下
s_box_x ++;
if( !isCanMove() )
s_box_x --;
break;
case 53: //数字5键
if( isGameOver ) //游戏结束
initGame(); //重新游戏
break;
case 42:
if( isGameOver ) //游戏结束
// System.exit(0); //退出游戏
Tetris.s_midlet.destroyApp(true);
break;
case 48:
setBox(); //新的BOX
break;
case 49: //是否显示网格
isShowReseau = !isShowReseau;
break;
}
repaint(); //重新绘制屏幕
// return true;
}
public void keyRepeated( int key )
{
keyPressed( key );
}
public void setNextBox()
{
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++;
}
public int getKeyCode( int key )
{
System.out.println( "key="+key );
switch( key )
{
case 1004: // up
case 119: // w
case 87: // W
case 50: // 2
return UP;
case 1005: // down
case 115: // s
case 83: // S
case 56: // 8
return DOWN;
case 1006: // left
case 97: // a
case 65: // A
case 52: // 4
return LEFT;
case 1007: // right
case 100: // d
case 68: // D
case 54: // 6
return RIGHT;
default:
return key;
}
}
// public boolean keyUp(Event evt, int key)
public void keyReleased( int key )
{
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);
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -