📄 cgame.java
字号:
}
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 int successSort() //成绩排序
{
for( int i=0; i<Tetris.recoreMax; i++ )
{
if( success >= Tetris.success[i] )
return i;
}
return Tetris.recoreMax;
}
private static int successPlace = 0; //名次
private void setRecord(int num) //设置记录
{
successPlace = num;
System.out.println("successPlace="+successPlace);
for( int i=Tetris.recoreMax-1; i>=0; i-- )
{
if( i==num )
{
Tetris.success [i] = success;
Tetris.dateTime[i] = Tetris.getDate();
}else
{
Tetris.success [i] = Tetris.success [i-1];
Tetris.dateTime[i] = Tetris.dateTime[i-1];
}
}
}
/////////绘制成绩列表////////////
private static final int w1 = 45; //名次 后分隔线
private static final int w2 = 85; //成绩 后分隔线
private static final int font_h = 16; //字体高度
private void paintRecord( Graphics g ) //绘制记录
{
g.setColor( gameBG );
g.fillRect( 0, 0, s_width, s_height );
g.setColor( gameColor[1] ); //白色色
g.drawString("名次",5,2,g.TOP|g.LEFT);
g.drawString("成绩",w1,2,g.TOP|g.LEFT);
g.drawString("日期",w2,2,g.TOP|g.LEFT);
g.setColor( gameColor[0] ); //网格色
g.drawLine(w1,0,w1,s_height);
g.drawLine(w2,0,w2,s_height);
for(int i=1; i<=Tetris.recoreMax; i++ )
{
if( i == successPlace+1 )
g.setColor( gameColor[2] ); //红色
else
g.setColor( gameColor[1] ); //白色
g.drawString(""+i,5,font_h*i,g.TOP|g.LEFT);
g.drawString(""+Tetris.success [i-1],w1, font_h*i, g.TOP|g.LEFT);
g.drawString(""+Tetris.dateTime[i-1],w2, font_h*i, g.TOP|g.LEFT);
g.setColor( gameColor[0] ); //网格色
g.drawLine(0,font_h*i,s_width,16*i);
}
g.setColor( gameColor[6] ); //黄色
g.drawString("5:Replay",s_width>>1,s_height-font_h*2, g.TOP|g.HCENTER);
g.drawString("*:Exit", s_width>>1,s_height-font_h*1, g.TOP|g.HCENTER);
}
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 static int act_off_x = 0; //方块在左右边界旋转的时候调整方块位置的偏移
private static final int act_move = 0;
private static final int act_transfiguration = 1;
private boolean isCanMove()
{
return isCanMove( act_move );
}
private boolean isCanMove( int act )
{
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 ) //左边界检测
{
if( act == act_transfiguration )//左边界检测失败 调整 BOX 位置右移动 最多2格
{
act_off_x=1;
s_box_x ++;
if( isCanMove() )
{
return true;
}
else
{
act_off_x=2;
s_box_x ++;
if( isCanMove() )
{
return true;
}
else
{
act_off_x = 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 ) //右边界检测
{
if( act == act_transfiguration )//右边界检测失败 调整 BOX 位置左移动 最多1格
{
act_off_x = -1;
s_box_x --;
if( isCanMove() )
{
return true;
}
else
{
act_off_x = 0;
}
}
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 ) //地图格子检测
{
System.out.println( "map s_box_y="+s_box_y+" matrix["+i+"]["+j+"]="+matrix[i][j]);
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;
// s_box_x -= act_off_x; //恢复偏移中心到未偏移前//不恢复的好1
if( !isCanMove( act_transfiguration ) )
{
box_state --;
if( box_state<0 )
box_state = 3;
}
break;
case DOWN: //向下移动
act_off_x = 0; //恢复BOX旋转位置偏移为0
if( isKeyDown == 2 )
isKeyDown = 1;
if( isKeyDown == 1 )
{
s_box_y ++;
if( !isCanMove() )
s_box_y --;
}
break;
case LEFT: //向左移动BOX
act_off_x = 0; //恢复BOX旋转位置偏移为0
isKeyDown = 0; //0没有按下
s_box_x --;
if( !isCanMove() )
s_box_x ++;
break;
case RIGHT: //向右移动BOX
act_off_x = 0; //恢复BOX旋转位置偏移为0
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 + -