俄罗斯方块java代码Word文档格式.docx
《俄罗斯方块java代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块java代码Word文档格式.docx(41页珍藏版)》请在冰豆网上搜索。
"
),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);
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(
if(pauseButton.getText().equals("
))
game.pauseGame();
else
game.resumeGame();
);
stopButton.addActionListener(
game.stopGame();
turnHarderButton.addActionListener(
intlevel=0;
try{
level=Integer.parseInt(levelField.getText());
setLevel(level+1);
}catch(NumberFormatExceptione)
e.printStackTrace();
turnEasilyButton.addActionListener(
setLevel(level-1);
*时间驱动程序,每格500毫秒对level,score值进行更新
timer=newTimer(500,
scoreField.setText("
+game.getScore());
game.levelUpdate();
timer.start();
}
*设置预显方块的样式
publicvoidsetBlockStyle(intstyle)
tipBlockPanel.setStyle(style);
tipBlockPanel.repaint();
*重置,将所有数据恢复到最初值
publicvoidreset()
levelField.setText("
setPlayButtonEnabled(true);
setPauseButtonLabel(true);
tipBlockPanel.setStyle(0);
*设置playButton是否可用
publicvoidsetPlayButtonEnabled(booleanenable)
playButton.setEnabled(enable);
*设置pauseButton的文本
publicvoidsetPauseButtonLabel(booleanpause)
pauseButton.setText(pause?
"
:
Rusume"
*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸
publicvoidfanning()
tipBlockPanel.fanning();
*根据level文本域的值返回当前的级别
publicintgetLevel()
try
level=Integer.parseInt(levelField.getText());
returnlevel;
*设置level文本域的值
publicvoidsetLevel(intlevel)
if(level>
0&
&
level<
=RussiaBlocksGame.MAX_LEVEL)
+level);
*内部类为预显方块的显示区域
privateclassTipBlockPanelextendsJPanel
privateColorbgColor=Color.darkGray,
blockColor=Color.lightGray;
privateRussiaBox[][]boxes=newRussiaBox[RussiaBlock.ROWS][RussiaBlock.COLS];
privateintboxWidth,boxHeight,style;
privatebooleanisTiled=false;
*构造函数
publicTipBlockPanel()
for(inti=0;
i<
boxes.length;
i++)
for(intj=0;
j<
boxes[i].length;
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();
j<
boxes[i].length;
j++)
Colorcolor=(style&
key)!
=0?
blockColor:
bgColor;
g.setColor(color);
g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);
key>
>
=1;
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();
caseKeyEvent.VK_RIGHT:
block.moveRight();
caseKeyEvent.VK_UP:
block.turnNext();
caseKeyEvent.VK_SPACE:
//一键到底
while(block.moveDown())
{
default:
}
*游戏中方块显示的画布类
publicclassGameCanvasextendsJPanel
privateRussiaBox[][]boxes;
privateintrows=20,cols=12;
privatestaticGameCanvascanvas=null;
privateintboxWidth,boxHeight;
//默认为零需要调用fanning函数设置
privateColorblockColor=Color.RED,bgColor=newColor(0,204,204);
*采用单件模式,构造函数私有
privateGameCanvas()
boxes=newRussiaBox[rows][cols];
boxes[i].length;
boxes[i][j]=newRussiaBox(false);
setBorder(border);
*获得GameCanvas实例
publicstaticGameCanvasgetCanvasInstance()
if(canvas==null)
canvas=newGameCanvas();
returncanvas;
*设置画布的背景色
publicvoidsetBgColor(ColorbgColor)
*获得画布的背景色
publicColorgetBgColor()
returnbgColor;
*设置方块的颜色
publicvoidsetBlockColor(ColorblockColor)
*方块的颜色
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<
rows&
col>
col<
cols)
returnboxes[row][col];
else
returnnull;
*在画布中绘制方块
Colorcolor=boxes[i][j].isColorBox()?
g.fill3DRect(j*boxWidth,i*boxHeight,boxWidth,boxHeight,true);
*清除第row行
publicvoidremoveLine(introw)
for(inti=row;
i>
0;
i--)
cols;
boxes[i][j]=(RussiaBox)boxes[i-1][j].clone();
*重置为初始时的状态
i++)
for(intj=0;
j<
j++)
boxes[i][j].setColor(false);
*根据窗体的大小自动调整方格的大小
boxWidth=getSize().width/cols;
boxHeight=getSize().height/rows;
*方块类
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},//'