俄罗斯方块java代码.docx
- 文档编号:3186798
- 上传时间:2022-11-19
- 格式:DOCX
- 页数:41
- 大小:23.47KB
俄罗斯方块java代码.docx
《俄罗斯方块java代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块java代码.docx(41页珍藏版)》请在冰豆网上搜索。
俄罗斯方块java代码
俄罗斯方块java代码
=================1======================
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=f
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块 java 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)