俄罗斯方块java代码.docx
《俄罗斯方块java代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块java代码.docx(41页珍藏版)》请在冰点文库上搜索。
俄罗斯方块java代码
packagerussia;
/*
*控制面板类
*/
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.*;
publicclassControlPanelextendsJPanel
{
privateTipBlockPaneltipBlockPanel;
privateJPaneltipPanel,InfoPanel,buttonPanel;
privatefinalJTextFieldlevelField,scoreField;
privateJButtonplayButton,pauseButton,stopButton,
turnHarderButton,turnEasilyButton;
privateEtchedBorderborder=newEtchedBorder(EtchedBorder.RAISED,Color.WHITE,newColor(148,145,140));
privateRussiaBlocksGamegame;
privateTimertimer;
publicControlPanel(finalRussiaBlocksGamegame)
{
this.game=game;
/*
*图形界面部分
*/
setLayout(newGridLayout(3,1,0,4));
tipBlockPanel=newTipBlockPanel();
tipPanel=newJPanel(newBorderLayout());
tipPanel.add(newJLabel("NextBlock:
"),BorderLayout.NORTH);
tipPanel.add(tipBlockPanel,BorderLayout.CENTER);
tipPanel.setBorder(border);
InfoPanel=newJPanel(newGridLayout(4,1,0,0));
levelField=newJTextField(""+RussiaBlocksGame.DEFAULT_LEVEL);
levelField.setEditable(false);
scoreField=newJTextField("0");
scoreField.setEditable(false);
InfoPanel.add(newJLabel("Level:
"));
InfoPanel.add(levelField);
InfoPanel.add(newJLabel("Score:
"));
InfoPanel.add(scoreField);
InfoPanel.setBorder(border);
buttonPanel=newJPanel(newGridLayout(5,1,0,0));
playButton=newJButton("Play");
pauseButton=newJButton("Pause");
stopButton=newJButton("Stop");
turnHarderButton=newJButton("Turnharder");
turnEasilyButton=newJButton("Turneasily");
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
buttonPanel.add(turnHarderButton);
buttonPanel.add(turnEasilyButton);
buttonPanel.setBorder(border);
addKeyListener(newControlKeyListener());//添加
add(tipPanel);
add(InfoPanel);
add(buttonPanel);
/*
*添加事件监听器
*/
playButton.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
game.playGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
pauseButton.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
if(pauseButton.getText().equals("Pause"))
game.pauseGame();
else
game.resumeGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
}
);
stopButton.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
game.stopGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
turnHarderButton.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
intlevel=0;
try{
level=Integer.parseInt(levelField.getText());
setLevel(level+1);
}catch(NumberFormatExceptione)
{
e.printStackTrace();
}
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
turnEasilyButton.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
intlevel=0;
try{
level=Integer.parseInt(levelField.getText());
setLevel(level-1);
}catch(NumberFormatExceptione)
{
e.printStackTrace();
}
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
/*
*时间驱动程序,每格500毫秒对level,score值进行更新
*/
timer=newTimer(500,
newActionListener()
{
publicvoidactionPerformed(ActionEventevent)
{
scoreField.setText(""+game.getScore());
game.levelUpdate();
}
}
);
timer.start();
}
/*
*设置预显方块的样式
*/
publicvoidsetBlockStyle(intstyle)
{
tipBlockPanel.setStyle(style);
tipBlockPanel.repaint();
}
/*
*重置,将所有数据恢复到最初值
*/
publicvoidreset()
{
levelField.setText(""+RussiaBlocksGame.DEFAULT_LEVEL);
scoreField.setText("0");
setPlayButtonEnabled(true);
setPauseButtonLabel(true);
tipBlockPanel.setStyle(0);
}
/*
*设置playButton是否可用
*/
publicvoidsetPlayButtonEnabled(booleanenable)
{
playButton.setEnabled(enable);
}
/*
*设置pauseButton的文本
*/
publicvoidsetPauseButtonLabel(booleanpause)
{
pauseButton.setText(pause?
"Pause":
"Rusume");
}
/*
*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸
*/
publicvoidfanning()
{
tipBlockPanel.fanning();
}
/*
*根据level文本域的值返回当前的级别
*/
publicintgetLevel()
{
intlevel=0;
try
{
level=Integer.parseInt(levelField.getText());
}catch(NumberFormatExceptione)
{
e.printStackTrace();
}
returnlevel;
}
/*
*设置level文本域的值
*/
publicvoidsetLevel(intlevel)
{
if(level>0&&level<=RussiaBlocksGame.MAX_LEVEL)
levelField.setText(""+level);
}
/*
*内部类为预显方块的显示区域
*/
privateclassTipBlockPanelextendsJPanel
{
privateColorbgColor=Color.darkGray,
blockColor=Color.lightGray;
privateRussiaBox[][]boxes=newRussiaBox[RussiaBlock.ROWS][RussiaBlock.COLS];
privateintboxWidth,boxHeight,style;
privatebooleanisTiled=false;
/*
*构造函数
*/
publicTipBlockPanel()
{
for(inti=0;ifor(intj=0;j{
boxes[i][j]=newRussiaBox(false);
}
style=0x0000;
}
/*
*构造函数
*/
publicTipBlockPanel(ColorbgColor,ColorblockColor)
{
this();
this.bgColor=bgColor;
this.blockColor=blockColor;
}
/*
*设置方块的风格
*/
publicvoidsetStyle(intstyle)
{
this.style=style;
repaint();
}
/*
*绘制预显方块
*/
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
intkey=0x8000;
if(!
isTiled)
fanning();
for(inti=0;ifor(intj=0;j{
Colorcolor=(style&key)!
=0?
blockColor:
bgColor;
g.setColor(color);
g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);
key>>=1;
}
}
/*
*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸
*/
publicvoidfanning()
{
boxWidth=getSize().width/RussiaBlock.COLS;
boxHeight=getSize().height/RussiaBlock.ROWS;
isTiled=true;
}
}
/*
*内部类键盘键听器,响应键盘事件
*/
classControlKeyListenerextendsKeyAdapter{
publicvoidkeyPressed(KeyEventke)
{
if(!
game.isPlaying())return;
RussiaBlockblock=game.getCurBlock();
switch(ke.getKeyCode()){
caseKeyEvent.VK_DOWN:
block.moveDown();
break;
caseKeyEvent.VK_LEFT:
block.moveLeft();
break;
caseKeyEvent.VK_RIGHT:
block.moveRight();
break;
caseKeyEvent.VK_UP:
block.turnNext();
break;
caseKeyEvent.VK_SPACE:
//一键到底
while(block.moveDown())
{
}
break;
default:
break;
}
}
}
}
packagerussia;
/*
*游戏中方块显示的画布类
*/
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.*;
publicclassGameCanvasextendsJPanel
{
privateRussiaBox[][]boxes;
privateintrows=20,cols=12;
privatestaticGameCanvascanvas=null;
privateintboxWidth,boxHeight;//默认为零需要调用fanning函数设置
privateColorblockColor=Color.RED,bgColor=newColor(0,204,204);
privateEtchedBorderborder=newEtchedBorder(EtchedBorder.RAISED,Color.WHITE,newColor(148,145,140));
/*
*采用单件模式,构造函数私有
*/
privateGameCanvas()
{
boxes=newRussiaBox[rows][cols];
for(inti=0;ifor(intj=0;jboxes[i][j]=newRussiaBox(false);
setBorder(border);
}
/*
*获得GameCanvas实例
*/
publicstaticGameCanvasgetCanvasInstance()
{
if(canvas==null)
canvas=newGameCanvas();
returncanvas;
}
/*
*设置画布的背景色
*/
publicvoidsetBgColor(ColorbgColor)
{
this.bgColor=bgColor;
}
/*
*获得画布的背景色
*/
publicColorgetBgColor()
{
returnbgColor;
}
/*
*设置方块的颜色
*/
publicvoidsetBlockColor(ColorblockColor)
{
this.blockColor=blockColor;
}
/*
*方块的颜色
*/
publicColorgetBlockColor()
{
returnblockColor;
}
/*
*设置画布中方块的行数
*/
publicvoidsetRows(introws)
{
this.rows=rows;
}
/*
*得到画布中方块的行数
*/
publicintgetRows()
{
returnrows;
}
/*
*设置画布中方块的列数
*/
publicvoidsetCols(intcols)
{
this.cols=cols;
}
/*
*得到画布中方块的列数
*/
publicintgetCols()
{
returncols;
}
/*
*得到row行,col列的方格
*/
publicRussiaBoxgetBox(introw,intcol)
{
if(row>=0&&row=0&&colreturnboxes[row][col];
else
returnnull;
}
/*
*在画布中绘制方块
*/
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
fanning();
for(inti=0;ifor(intj=0;j{
Colorcolor=boxes[i][j].isColorBox()?
blockColor:
bgColor;
g.setColor(color);
g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);
}
}
/*
*清除第row行
*/
publicvoidremoveLine(introw)
{
for(inti=row;i>0;i--)
for(intj=0;j{
boxes[i][j]=(RussiaBox)boxes[i-1][j].clone();
}
}
/*
*重置为初始时的状态
*/
publicvoidreset()
{
for(inti=0;ifor(intj=0;j{
boxes[i][j].setColor(false);
}
repaint();
}
/*
*根据窗体的大小自动调整方格的大小
*/
publicvoidfanning()
{
boxWidth=getSize().width/cols;
boxHeight=getSize().height/rows;
}
}
packagerussia;
/*
*方块类
*/
publicclassRussiaBlockextendsThread
{
privateintstyle,y,x,level;
privatebooleanmoving,pausing;
privateRussiaBoxboxes[][];
privateGameCanvascanvas;
publicfinalstaticintROWS=4;
publicfinalstaticintCOLS=4;
publicfinalstaticintBLOCK_KIND_NUMBER=7;
publicfinalstaticintBLOCK_STATUS_NUMBER=4;
publicfinalstaticintBETWEEN_LEVELS_TIME=50;
publicfinalstaticintLEVEL_FLATNESS_GENE=3;
/*
*方块的所有风格及其不同的状态
*/
publicfinalstaticint[][]STYLES={//共28种状态
{0x0f00,0x4444,0x0f00,0x4444},//长条型的四种状态
{0x04e0,0x0464,0x00e4,0x04c4},//'T'型的四种状态
{0x4620,0x6c00,0x4620,0x6c00},//反'Z'型的四种状态
{0x2640,0xc600,0x2640,0xc600},//'Z'型的四种状态