方块.docx

上传人:b****6 文档编号:7958738 上传时间:2023-01-27 格式:DOCX 页数:28 大小:21.04KB
下载 相关 举报
方块.docx_第1页
第1页 / 共28页
方块.docx_第2页
第2页 / 共28页
方块.docx_第3页
第3页 / 共28页
方块.docx_第4页
第4页 / 共28页
方块.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

方块.docx

《方块.docx》由会员分享,可在线阅读,更多相关《方块.docx(28页珍藏版)》请在冰豆网上搜索。

方块.docx

方块

importjavax.swing.UIManager;

importjava.awt.*;

/**

*

Title:

*

Description:

*

Copyright:

Copyright(c)2008

*

Company:

*@authornotattributable

*@version1.0

*/

publicclassApplication1{

booleanpackFrame=false;

//Constructtheapplication

publicApplication1(){

Frameframe=newFrame();

//Validateframesthathavepresetsizes

//Packframesthathaveusefulpreferredsizeinfo,e.g.fromtheirlayout

if(packFrame){

frame.pack();

}

else{

frame.validate();

}

//Centerthewindow

DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();

DimensionframeSize=frame.getSize();

if(frameSize.height>screenSize.height){

frameSize.height=screenSize.height;

}

if(frameSize.width>screenSize.width){

frameSize.width=screenSize.width;

}

frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);

frame.setVisible(true);

}

//Mainmethod

publicstaticvoidmain(String[]args){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exceptione){

e.printStackTrace();

}

newApplication1();

}

}

packageErsBlockGameDemo;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.border.EtchedBorder;

importjavax.swing.border.Border;

/**

*

Title:

俄罗斯方块

*

Description:

*

Copyright:

Copyright(c)2008

*

Company:

*@authornotattributable

*@version1.0

*/

classErsBlocksGameextendsJFrame{

//每填满一行记100分

publicfinalstaticintPER_LINE_SCORE=100;

//积多少分可以升级

publicfinalstaticintPER_LEVEL_SCORE=PER_LINE_SCORE;

//最大级数为10级

publicfinalstaticintMAX_LEVEL=10;

//默认级数为5级

publicfinalstaticintDEFAULT_LEVEL=5;

privateGameCanvascanvas;

privateErsBlockblock;

privatebooleanplaying=false;

privateControlPanelctrlPanel;

privateJMenuBarbar=newJMenuBar();

privateJMenumGame=newJMenu("游戏"),

mControl=newJMenu("控制"),

mWindowStyle=newJMenu("游戏风格"),

mInfo=newJMenu("信息");

privateJMenuItemmiNewGame=newJMenuItem("新游戏"),

miSetBlockColor=newJMenuItem("设置方块的颜色……"),

miSetBackColor=newJMenuItem("设置背景的颜色……"),

miTurnHarder=newJMenuItem("升高游戏难度"),

miTurnEasier=newJMenuItem("降低游戏难度"),

miExit=newJMenuItem("退出"),

miPlay=newJMenuItem("开始"),

miPause=newJMenuItem("暂停"),

miResume=newJMenuItem("恢复"),

miStop=newJMenuItem("中止游戏"),

miAuthor=newJMenuItem("版本:

俄罗斯方块1.0"),

miSourceInfo=newJMenuItem("实现源代码由JAVA");

privateJCheckBoxMenuItem

miAsWindows=newJCheckBoxMenuItem("Windows"),

miAsMotif=newJCheckBoxMenuItem("Motif"),

miAsMetal=newJCheckBoxMenuItem("Metal");

//主游戏类的构造方法,窗口标题

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

}

//判断游戏是否在进行

publicbooleanisPlaying(){

returnplaying;

}

publicErsBlockgetCurBlock(){

returnblock;

}

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

}

//接受游戏者设置的游戏难度

publicintgetLevel(){

returnctrlPanel.getLevel();

}

//游戏者设置游戏难度

publicvoidsetLevel(intlevel){

if(level<11&&level>0)ctrlPanel.setLevel(level);

}

//得到游戏积分

publicintgetScore(){

if(canvas!

=null)returncanvas.getScore();

return0;

}

//得到自上次升级以来的积分,升级以后,此积分为0

publicintgetScoreForLevelUpdate(){

if(canvas!

=null)returncanvas.getScoreForLevelUpdate();

return0;

}

//当分数达到一定数量时,升级

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,"设置方块颜色",canvas.getBlockColor());

if(newFrontColor!

=null)canvas.setBlockColor(newFrontColor);

}

});

miSetBackColor.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEventae){

ColornewBackColor=JColorChooser.showDialog(ErsBlocksGame.this,"设置方块颜色",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="com.sun.java.swing.plaf.metal.MetalLookAndFeel";

setWindowStyle(plaf);

canvas.fanning();

ctrlPanel.fanning();

miAsWindows.setState(false);

miAsMetal.setState(true);

miAsMotif.setState(false);

}

});

}

privatevoidsetWindowStyle(Stringplaf){

try{

UIManager.setLookAndFeel(plaf);

SwingUtilities.updateComponentTreeUI(this);

}catch(Exceptione){

}

}

privateclassGameimplementsRunnable{

publicvoidrun(){

intcol=(int)(Math.random()*(canvas.getCols()-3));

intstyle=ErsBlock.STYLES[(int)(Math.random()*7)][(int)(Math.random()*4)];

while(playing){

if(block!

=null){

if(block.isAlive()){

try{

Thread.currentThread().sleep(100);

}

catch(InterruptedExceptionie){

ie.printStackTrace();

}

continue;

}

}

checkFullLine();

if(isGameOver()){

miPlay.setEnabled(true);

miPause.setEnabled(true);

miResume.setEnabled(false);

ctrlPanel.setPlayButtonEnable(true);

ctrlPanel.setPauseButtonLabel(true);

reportGameOver();

return;

}

block=newErsBlock(style,-1,col,getLevel(),canvas);

block.start();

col=(int)(Math.random()*(canvas.getCols()-3));

style=ErsBlock.STYLES[(int)(Math.random()*7)][(int)(Math.random()*4)];

ctrlPanel.setTipStyle(style);

}

}

publicvoidcheckFullLine(){

for(inti=0;i

introw=-1;

booleanfullLineC

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

当前位置:首页 > 解决方案 > 学习计划

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

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