java编写俄罗斯方块三Word文档下载推荐.docx
《java编写俄罗斯方块三Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java编写俄罗斯方块三Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
![java编写俄罗斯方块三Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2023-1/9/f0e0caa7-87c7-4dfa-a4d0-53190be38e13/f0e0caa7-87c7-4dfa-a4d0-53190be38e131.gif)
privateErsBlockblock;
privatebooleanplaying=false;
privateControlPanelctrlPanel;
privateJMenuBarbar=newJMenuBar();
privateJMenu
mGame=newJMenu("
Game"
),
mControl=newJMenu("
Control"
mWindowStyle=newJMenu("
WindowStyle"
mInfo=newJMenu("
Information"
);
privateJMenuItem
miNewGame=newJMenuItem("
NewGame"
miSetBlockColor=newJMenuItem("
SetBlockColor..."
miSetBackColor=newJMenuItem("
SetBackgroundColor..."
miTurnHarder=newJMenuItem("
Turnupthelevel"
miTurnEasier=newJMenuItem("
Turndownthelevel"
miExit=newJMenuItem("
Eixt"
miPlay=newJMenuItem("
Play"
miPause=newJMenuItem("
Pause"
miResume=newJMenuItem("
Resume"
miStop=newJMenuItem("
Stop"
miAuthor=newJMenuItem("
Author:
apple@"
miSourceInfo=newJMenuItem("
Youcangetthesourcecode/documentbyemail"
privateJCheckBoxMenuItem
miAsWindows=newJCheckBoxMenuItem("
Windows"
miAsMotif=newJCheckBoxMenuItem("
Motif"
miAsMetal=newJCheckBoxMenuItem("
Metal"
true);
*主游戏类的构造函数
*@paramtitleString,窗口标题
publicErsBlocksGame(Stringtitle){
super(title);
setSize(315,392);
DimensionscrSize=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((scrSize.width-getSize().width)/2,
(scrSize.height-getSize().height)/2);
createMenu();
Containercontainer=getContentPane();
container.setLayout(newBorderLayout(6,0));
canvas=newGameCanvas(20,12);
ctrlPanel=newControlPanel(this);
container.add(canvas,BorderLayout.CENTER);
container.add(ctrlPanel,BorderLayout.EAST);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventwe){
stopGame();
System.exit(0);
}
});
addComponentListener(newComponentAdapter(){
publicvoidcomponentResized(ComponentEventce){
canvas.fanning();
show();
canvas.fanning();
}
*让游戏“复位”
publicvoidreset(){
ctrlPanel.reset();
canvas.reset();
*判断游戏是否还在进行
*@returnboolean,true-还在运行,false-已经停止
publicbooleanisPlaying(){
returnplaying;
*得到当前活动的块
*@returnErsBlock,当前活动块的引用
publicErsBlockgetCurBlock(){
returnblock;
*得到当前画布
*@returnGameCanvas,当前画布的引用
publicGameCanvasgetCanvas(){
returncanvas;
*开始游戏
publicvoidplayGame(){
play();
ctrlPanel.setPlayButtonEnable(false);
miPlay.setEnabled(false);
ctrlPanel.requestFocus();
*游戏暂停
publicvoidpauseGame(){
if(block!
=null)block.pauseMove();
ctrlPanel.setPauseButtonLabel(false);
miPause.setEnabled(false);
miResume.setEnabled(true);
*让暂停中的游戏继续
publicvoidresumeGame(){
=null)block.resumeMove();
ctrlPanel.setPauseButtonLabel(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
*用户停止游戏
publicvoidstopGame(){
playing=false;
=null)block.stopMove();
miPlay.setEnabled(true);
ctrlPanel.setPlayButtonEnable(true);
*得到当前游戏者设置的游戏难度
*@returnint,游戏难度1-MAX_LEVEL
publicintgetLevel(){
returnctrlPanel.getLevel();
*让用户设置游戏难度
*@paramlevelint,游戏难度1-MAX_LEVEL
publicvoidsetLevel(intlevel){
if(level<
11&
&
level>
0)ctrlPanel.setLevel(level);
*得到游戏积分
*@returnint,积分。
publicintgetScore(){
if(canvas!
=null)returncanvas.getScore();
return0;
*得到自上次升级以来的游戏积分,升级以后,此积分清零
publicintgetScoreForLevelUpdate(){
=null)returncanvas.getScoreForLevelUpdate();
*当分数累计到一定的数量时,升一次级
*@returnboolean,ture-updatesuccessufl,false-updatefail
publicbooleanlevelUpdate(){
intcurLevel=getLevel();
if(curLevel<
MAX_LEVEL){
setLevel(curLevel+1);
canvas.resetScoreForLevelUpdate();
returntrue;
}
returnfalse;
*游戏开始
privatevoidplay(){
reset();
playing=true;
Threadthread=newThread(newGame());
thread.start();
*报告游戏结束了
privatevoidreportGameOver(){
JOptionPane.showMessageDialog(this,"
GameOver!
"
*建立并设置窗口菜单
privatevoidcreateMenu(){
bar.add(mGame);
bar.add(mControl);
bar.add(mWindowStyle);
bar.add(mInfo);
mGame.add(miNewGame);
mGame.addSeparator();
mGame.add(miSetBlockColor);
mGame.add(miSetBackColor);
mGame.add(miTurnHarder);
mGame.add(miTurnEasier);
mGame.add(miExit);
mControl.add(miPlay);
mControl.add(miPause);
mControl.add(miResume);
mControl.add(miStop);
mWindowStyle.add(miAsWindows);
mWindowStyle.add(miAsMotif);
mWindowStyle.add(miAsMetal);
mInfo.add(miAuthor);
mInfo.add(miSourceInfo);
setJMenuBar(bar);
miPause.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_P,KeyEvent.CTRL_MASK));
miResume.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));
miNewGame.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
reset();
setLevel(DEFAULT_LEVEL);
miSetBlockColor.addActionListener(newActionListener(){
ColornewFrontColor=
JColorChooser.showDialog(ErsBlocksGame.this,
"
Setcolorforblock"
canvas.getBlockColor());
if(newFrontColor!
=null)
canvas.setBlockColor(newFrontColor);
miSetBackColor.addActionListener(newActionListener(){
ColornewBackColor=
canvas.getBackgroundColor());
if(newBackColor!
canvas.setBackgroundColor(newBackColor);
miTurnHarder.addActionListener(newActionListener(){
intcurLevel=getLevel();
if(curLevel<
MAX_LEVEL)setLevel(curLevel+1);
miTurnEasier.addActionListener(newActionListener(){
if(curLevel>
1)setLevel(curLevel-1);
miExit.addActionListener(newActionListener(){
miPlay.addActionListener(newActionListener(){
playGame();
miPause.addActionListener(newActionListener(){
pauseGame();
miResume.addActionListener(newActionListener(){
resumeGame();
miStop.addActionListener(newActionListener(){
miAsWindows.addActionListener(newActionListener(){
Stringplaf="
com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
;
setWindowStyle(plaf);
ctrlPanel.fanning();
miAsWindows.setState(true);
miAsMetal.setState(false);
miAsMotif.setState(false);
miAsMotif.addActionListener(newActionListener(){
com.sun.java.swing.plaf.motif.MotifLookAndFeel"
miAsWindows.setState(false);
miAsMotif.setState(true);
miAsMetal.addActionListener(newActionListener(){
javax.swing.plaf.metal.MetalLookAndFeel"
miAsMetal.setState(true);
*根据字串设置窗口外观
*@paramplafString,窗口外观的描述
privatevoidsetWindowStyle(Stringplaf){
try{
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
}catch(Exceptione){
*一轮游戏过程,实现了Runnable接口
*一轮游戏是一个大循环,在这个循环中,每隔100毫秒,
*检查游戏中的当前块是否已经到底了,如果没有,
*就继续等待。
如果到底了,就看有没有全填满的行,
*如果有就删除它,并为游戏者加分,同时随机产生一个
*新的当前块,让它自动下落。
*当新产生一个块时,先检查画布最顶上的一行是否已经
*被占了,如果是,可以判断GameOver了。
privateclassGameimplementsRunnable{
publicvoidrun(){
intcol=(int)(Math.random()*(canvas.getCols()-3)),
style=ErsBlock.STYLES[(int)(Math.random()*7)][(int)(Math.random()*4)];
while(playing){
if(block!
=null){//第一次循环时,block为空
if(block.isAlive()){
try{
Thread.currentThread().sleep(100);
}catch(InterruptedExceptionie){
ie.printStackTrace();
}
continue;
}
}
checkFullLine();
//检查是否有全填满的行
if(isGameOver()){//检查游戏是否应该结束了