java编写俄罗斯方块三.docx

上传人:b****5 文档编号:6732455 上传时间:2023-01-09 格式:DOCX 页数:15 大小:18.69KB
下载 相关 举报
java编写俄罗斯方块三.docx_第1页
第1页 / 共15页
java编写俄罗斯方块三.docx_第2页
第2页 / 共15页
java编写俄罗斯方块三.docx_第3页
第3页 / 共15页
java编写俄罗斯方块三.docx_第4页
第4页 / 共15页
java编写俄罗斯方块三.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

java编写俄罗斯方块三.docx

《java编写俄罗斯方块三.docx》由会员分享,可在线阅读,更多相关《java编写俄罗斯方块三.docx(15页珍藏版)》请在冰豆网上搜索。

java编写俄罗斯方块三.docx

java编写俄罗斯方块三

/**

*File:

ErsBlocksGame.java

*User:

Administrator

*Date:

Jan15,2003

*Describe:

俄罗斯方块的Java实现

*/

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

/**

*游戏主类,继承自JFrame类,负责游戏的全局控制。

*内含

*1,一个GameCanvas画布类的实例引用,

*2,一个保存当前活动块(ErsBlock)实例的引用,

*3,一个保存当前控制面板(ControlPanel)实例的引用;

*/

publicclassErsBlocksGameextendsJFrame{

/**

*每填满一行计多少分

*/

publicfinalstaticintPER_LINE_SCORE=100;

/**

*积多少分以后能升级

*/

publicfinalstaticintPER_LEVEL_SCORE=PER_LINE_SCORE*20;

/**

*最大级数是10级

*/

publicfinalstaticintMAX_LEVEL=10;

/**

*默认级数是5

*/

publicfinalstaticintDEFAULT_LEVEL=5;

privateGameCanvascanvas;

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(){

if(block!

=null)block.resumeMove();

ctrlPanel.setPauseButtonLabel(true);

miPause.setEnabled(true);

miResume.setEnabled(false);

ctrlPanel.requestFocus();

}

/**

*用户停止游戏

*/

publicvoidstopGame(){

playing=false;

if(block!

=null)block.stopMove();

miPlay.setEnabled(true);

miPause.setEnabled(true);

miResume.setEnabled(false);

ctrlPanel.setPlayButtonEnable(true);

ctrlPanel.setPauseButtonLabel(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;

}

/**

*得到自上次升级以来的游戏积分,升级以后,此积分清零

*@returnint,积分。

*/

publicintgetScoreForLevelUpdate(){

if(canvas!

=null)returncanvas.getScoreForLevelUpdate();

return0;

}

/**

*当分数累计到一定的数量时,升一次级

*@returnboolean,ture-updatesuccessufl,false-updatefail

*/

publicbooleanlevelUpdate(){

intcurLevel=getLevel();

if(curLevel

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.addSeparator();

mGame.add(miTurnHarder);

mGame.add(miTurnEasier);

mGame.addSeparator();

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){

stopGame();

reset();

setLevel(DEFAULT_LEVEL);

}

});

miSetBlockColor.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

ColornewFrontColor=

JColorChooser.showDialog(ErsBlocksGame.this,

"Setcolorforblock",canvas.getBlockColor());

if(newFrontColor!

=null)

canvas.setBlockColor(newFrontColor);

}

});

miSetBackColor.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

ColornewBackColor=

JColorChooser.showDialog(ErsBlocksGame.this,

"Setcolorforblock",canvas.getBackgroundColor());

if(newBackColor!

=null)

canvas.setBackgroundColor(newBackColor);

}

});

miTurnHarder.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

intcurLevel=getLevel();

if(curLevel

}

});

miTurnEasier.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

intcurLevel=getLevel();

if(curLevel>1)setLevel(curLevel-1);

}

});

miExit.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

System.exit(0);

}

});

miPlay.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

playGame();

}

});

miPause.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

pauseGame();

}

});

miResume.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

resumeGame();

}

});

miStop.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

stopGame();

}

});

miAsWindows.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

Stringplaf="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

setWindowStyle(plaf);

canvas.fanning();

ctrlPanel.fanning();

miAsWindows.setState(true);

miAsMetal.setState(false);

miAsMotif.setState(false);

}

});

miAsMotif.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

Stringplaf="com.sun.java.swing.plaf.motif.MotifLookAndFeel";

setWindowStyle(plaf);

canvas.fanning();

ctrlPanel.fanning();

miAsWindows.setState(false);

miAsMetal.setState(false);

miAsMotif.setState(true);

}

});

miAsMetal.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

Stringplaf="javax.swing.plaf.metal.MetalLookAndFeel";

setWindowStyle(plaf);

canvas.fanning();

ctrlPanel.fanning();

miAsWindows.setState(false);

miAsMetal.setState(true);

miAsMotif.setState(false);

}

});

}

/**

*根据字串设置窗口外观

*@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()){//检查游戏是否应该结束了

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1