gameFieldSquares[rowIndex][columnIndex]=s;
}
clearFullRows();//方块满行消除
over();
block=block2;
block2.clear2Block(xy);
block2=ShapeBlock.returnaobj();
returnxy(block2);
block2.setGraphics(panel2.getGraphics());
block2.draw2Block(xy);
//block2.clear2Block(xy);
block.setGraphics(panel1.getGraphics());
block.drawBlock();
}
}
}
publicMain(){
squ=newSquare();
block=newShapeBlock();
block2=newShapeBlock();
panel1=newJPanel();
panel1.setBounds(20,20,300,400);
panel1.setBackground(Color.black);
add(panel1);
this.setResizable(false);
panel2=newJPanel();
panel2.setBounds(350,20,130,130);
panel2.setBackground(Color.black);
add(panel2);
label2=newJLabel("Score:
");
//label2.setBounds(450,80,100,50);
Fontfont2=newFont("宋体",Font.BOLD,19);//设置个人信息字体
label2.setFont(font2);//为登陆标签添加字体
label2.setForeground(Color.black);//为登陆标签添加字体颜色
label2.setBounds(350,140,100,50);
add(label2);
label3=newJLabel("0");
label3.setBounds(420,140,100,50);
Fontfont3=newFont("宋体",Font.BOLD,20);//设置个人信息字体
label3.setFont(font3);//为登陆标签添加字体
label3.setForeground(Color.red);//为登陆标签添加字体颜色
add(label3);
label1=newJLabel("游戏结束!
");
label1.setBounds(110,140,300,200);
label4=newJLabel("作者:
2012__级计算机系__wqn");
label4.setBounds(13,410,1000,50);
Fontfont4=newFont("宋体",Font.BOLD,15);//设置个人信息字体
label4.setFont(font4);//为登陆标签添加字体
label4.setForeground(Color.black);//为登陆标签添加字体颜色
add(label4);
Button1=newJButton("开始");
Button1.setBounds(400,190,60,30);
Button1.addKeyListener(this);
Button1.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(start){
block=ShapeBlock.returnaobj();
block.setGraphics(panel1.getGraphics());
block.drawBlock();
block2=ShapeBlock.returnaobj();
returnxy(block2);
block2.setGraphics(panel2.getGraphics());
block2.draw2Block(xy);
//block2.clear2Block(xy);
//Button1.setEnabled(false);
gameTimer=newTimer();
gameTimer.schedule(newGameTimerTask(),0,timerPeriod);
start=false;
Button1.setText("结束");
}
else
{
System.exit(0);
}
}
});
add(Button1);
Button2=newJButton("暂停");
Button2.setBounds(400,240,60,30);
Button2.addKeyListener(this);
Button2.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(!
end){
if(Button2.getText()=="暂停")
{
gameTimer.cancel();
Button2.setText("恢复");
}
elseif(Button2.getText()=="恢复")
{
gameTimer=newTimer();
gameTimer.schedule(newGameTimerTask(),0,timerPeriod);
Button2.setText("暂停");
}
}
}
});
add(Button2);
Button3=newJButton("旋转");
Button3.setBounds(400,290,60,30);
Button3.addKeyListener(this);
Button3.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(!
end){
if(Main.this.canrorate())
{
block.clearBlock();
block.rotateBlock();
block.drawBlock();
}
}
}
});
add(Button3);
Button4=newJButton("左移");
Button4.setBounds(350,340,60,30);
Button4.addKeyListener(this);
Button4.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(!
end){
if(canStepLeft())
block.leftBlock();
}
}
});
add(Button4);
Button5=newJButton("右移");
Button5.setBounds(450,340,60,30);
Button5.addKeyListener(this);
Button5.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(!
end){
if(canStepRight())
block.rigthtBlock();
}
}
});
add(Button5);
Button6=newJButton("下移");
Button6.setBounds(400,390,60,30);
Button6.addKeyListener(this);
Button6.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(!
end){
if(canStepDown())
block.downBlock();
}
}
});
add(Button6);
}
publicvoidreturnxy(ShapeBlockblock)
{
for(inti=0;i<4;i++)
{
xy[i][0]=block2.squares[i].getLocationX();
xy[i][1]=block2.squares[i].getLocationY();
}
}
privatebooleancanStepDown()
{
booleancandown=true;
for(inti=0;i{
Squares;
s=block.squares[i];/*不超下界*/
if(s.getLocationY()>=(rowCount-1)*height)
{
candown=false;
break;
}
introwIndex=s.getLocationY()/height;
intcolumnIndex=s.getLocationX()/width;/*不往下穿越*/
if(rowIndex>=0&&rowIndex=0&&columnIndexif(gameFieldSquares[rowIndex+1][columnIndex]!
=null)
candown=false;
}
returncandown;
}
privatebooleancanStepLeft()
{
booleancanLeft=true;
for(inti=0;iSquares=block.squares[i];
if(s.getLocationX()<=0)/*不超左界*/
{
canLeft=false;
break;
}
introwIndex=s.getLocationY()/height;
intcolumnIndex=s.getLocationX()/width;/*不往左穿越*/
if(rowIndex>=0&&rowIndex=0&&columnIndexif(gameFieldSquares[rowIndex][columnIndex-1]!
=null)
canLeft=false;
}
returncanLeft;
}
privatebooleancanStepRight(){
booleancanRight=true;
for(inti=0;iSquares=block.squares[i];/*不超右界*/
if(s.getLocationX()>=(columnCount-1)*width){
canRight=false;
break;
}
introwIndex=s.getLocationY()/height;
intcolumnIndex=s.getLocationX()/width;
if(rowIndex>=0&&rowIndex=0&&columnIndexif(gameFieldSquares[rowIndex][columnIndex+1]!
=null)/*不往右穿越*/
canRight=false;
}
returncanRight;
}
privatebooleancanrorate(){
booleancanRorate=true;
for(inti=0;i{
Squares;
s=block.squares[i];//不超下界
//if(s.getLocationX()>=(columnCount)*width||s.getLocationX()+width<=0||s.getLocationY()>=(rowCount)
*height)
{
introwIndex=s.getLocationY()/height;
intcolumnIndex=s.getLocationX()/width;//不往下穿越
//System.out.println(rowIndex+""+columnIndex);
if(rowIndex>=0&&rowIndex=0&&columnIndexif(rowIndex>=this.rowCount-1||columnIndex>=this.columnCount-1)
break;
//System.out.println("2="+rowIndex+""+columnIndex);
if(rowIndex>=this.rowCount-1)
break;
if(gameFieldSquares[rowIndex+1][columnIndex]!
=null)
canRorate=false;
if(columnIndex<=0)
break;
if(gameFieldSquares[rowIndex][columnIndex-1]!
=null)
canRorate=false;
if(columnIndex>=this.columnCount-1)
break;
if(gameFieldSquares[rowIndex][columnIndex+1]!
=null)//不往右穿越
canRorate=false;
}
}
}
block.rotateBlock();
for(inti=0;iSquares=block.squares[i];
if(s.getLocationX()>=(columnCount)*width||s.getLocationX()+width<=0||s.getLocationY()>=(rowCount)
*height){
canRorate=false;
break;
}
}
for(intk=1;k<=3;k++)
block.rotateBlock();
returncanRorate;
}
privatebooleanfullJudgement(introwIndex)
{
booleanisFullrow=true;
for(intcolumnIndex=0;columnIndexif(gameFieldSquares[rowIndex][columnIndex]==null)
isFullrow=false;
returnisFullrow;
}
privatevoidclearRow(introwIndex){
for(intcolumnIndex=0;columnIndexif(this.gameFieldSquares[rowIndex][columnIndex]!
=null){
this.gameFieldSquares[rowIndex][columnIndex].clear();
this.gameFieldSquares[rowIndex][columnIndex]=null;
}
}
}
privatevoidmoveRowDown(introwIndex){
for(introw=rowIndex;row>0;row--){
for(intcolumnIndex=0;columnIndexif(this.gameFieldSquares[row-1][columnIndex]!
=null){
this.gameFieldSquares[row-1][columnIndex].clear();
this.gameFieldSquares[row-1][columnIndex].stepDown();
this.gameFieldSquares[row][columnIndex]=this.