综合运用所学知识编写相对复杂的程序.docx
《综合运用所学知识编写相对复杂的程序.docx》由会员分享,可在线阅读,更多相关《综合运用所学知识编写相对复杂的程序.docx(31页珍藏版)》请在冰点文库上搜索。
综合运用所学知识编写相对复杂的程序
实验项目:
java游戏初探(俄罗斯方块)
实验者:
05计算机4班陈华19号2007-11-27(截稿)
实验目的:
综合运用所学知识编写相对复杂的程序
程序分析及源代码:
1需求分析:
本游戏的总体目标是按照游戏规则为玩家提供一个方便友好的界面,同时满足不同玩家的不同难度的需求
2总体思想是通过线程和javagui编程实现本游戏的基本功能,由于此游戏很大众化,故界面设计是开发的重点和难点
//主类
importjavax.swing.event.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassGameMainFrameextendsJFrameimplementsKeyListener{
JPanelgameJPanel,clew1JPanel,clew2JPanel;//游戏区面板
JLabeldefen_JLabel,score_JLabel;//游戏记分等标签
JLabeldengji_JLabel,level_JLabel;
JButton[][]playBlocks;//游戏方块数组
int[][]flagBlocks;//游戏方块标志数组
JButton[][]nextBlocks;//游戏提示区方块
longscore;//积分
intlevel,speed,totalLines,currentLines;//游戏标志
booleanisPause,isEnd;//游戏开始与暂停标志
intblockType,nextblockType;//方块类型标志
Blockblock;//方块类型
GameThreadthread;//游戏主线程
//主窗体构造方法
publicGameMainFrame(){
super("稀饭的俄罗斯方块");
this.addGameMenu();
gameJPanel=newJPanel(newGridLayout(20,10));
clew1JPanel=newJPanel(newGridLayout(10,1));
clew2JPanel=newJPanel(newGridLayout(4,4));
defen_JLabel=newJLabel("得分");
score_JLabel=newJLabel();
dengji_JLabel=newJLabel("等级");
level_JLabel=newJLabel();
clew1JPanel.add(clew2JPanel);
clew1JPanel.add(defen_JLabel);
clew1JPanel.add(score_JLabel);
clew1JPanel.add(dengji_JLabel);
clew1JPanel.add(level_JLabel);
BorderLayoutborderlayout=newBorderLayout();
this.setLayout(borderlayout);
this.add(gameJPanel,borderlayout.CENTER);
this.add(clew1JPanel,borderlayout.EAST);
this.addKeyListener(this);
this.setFocusable(true);
//创建并初始化游戏区方块数组
playBlocks=newJButton[20][10];
for(inti=0;i<20;i++){
for(intj=1;j<10;j++){
playBlocks[i][j]=newJButton();
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
playBlocks[i][j].setEnabled(false);
gameJPanel.add(playBlocks[i][j]);
}
}
//创建并初始化游戏区方块标志数组
flagBlocks=newint[20][10];
for(inti=0;i<20;i++){
for(intj=0;j<10;j++){
flagBlocks[i][j]=0;
}
}
//创建并初始化方块提示区方块数组
nextBlocks=newJButton[4][4];
for(inti=0;i<4;i++){
for(intj=0;j<4;j++){
nextBlocks[i][j]=newJButton();
nextBlocks[i][j].setBackground(Color.lightGray);
nextBlocks[i][j].setVisible(false);
nextBlocks[i][j].setEnabled(false);
clew2JPanel.add(nextBlocks[i][j]);
}
}
score=0;//初始化游戏参数
level=0;
speed=0;
totalLines=0;
currentLines=0;
isPause=false;
isEnd=false;
blockType=(int)(Math.random()*1000%7);
switch(blockType){//随即产生游戏区方块
case0:
block=newBlockOne(this);break;
case1:
block=newBlockTwo(this);break;
case2:
block=newBlockThree(this);break;
case3:
block=newBlockFour(this);break;
case4:
block=newBlockFive(this);break;
case5:
block=newBlockSix(this);break;
case6:
block=newBlockSeven(this);break;
}
nextblockType=(int)(Math.random()*1000%7);//随即产生提示区方块
showNextBlock(nextblockType);
score_JLabel.setText(Long.toString(score));
level_JLabel.setText(Integer.toString(level));
thread=newGameThread(this);
try{
//thread.run();
thread.start();
//(newGameThread(this)).start();
//thread.start();//启动游戏主线程
}catch(IllegalThreadStateExceptione){}
this.setSize(500,500);
this.setVisible(true);
}
//显示下一方块
publicvoidshowNextBlock(inttype){
for(inti=0;i<4;i++){//清楚提示区的方块
for(intj=0;j<4;j++){
nextBlocks[i][j].setBackground(Color.lightGray);
nextBlocks[i][j].setVisible(false);
}
}
switch(type){//根据方块类型在提示区显示方块
case0:
nextBlocks[1][0].setBackground(Color.green);
nextBlocks[1][0].setVisible(true);
nextBlocks[1][1].setBackground(Color.green);
nextBlocks[1][1].setVisible(true);
nextBlocks[2][1].setBackground(Color.green);
nextBlocks[2][1].setVisible(true);
nextBlocks[2][2].setBackground(Color.green);
nextBlocks[2][2].setVisible(true);
break;
case1:
nextBlocks[1][1].setBackground(Color.blue);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.blue);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][0].setBackground(Color.blue);
nextBlocks[2][0].setVisible(true);
nextBlocks[2][1].setBackground(Color.blue);
nextBlocks[2][1].setVisible(true);
break;
case2:
nextBlocks[1][1].setBackground(Color.cyan);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.cyan);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][1].setBackground(Color.cyan);
nextBlocks[2][1].setVisible(true);
nextBlocks[2][2].setBackground(Color.cyan);
nextBlocks[2][2].setVisible(true);
break;
case3:
nextBlocks[1][0].setBackground(Color.yellow);
nextBlocks[1][0].setVisible(true);
nextBlocks[1][1].setBackground(Color.yellow);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.yellow);
nextBlocks[1][2].setVisible(true);
nextBlocks[2][2].setBackground(Color.yellow);
nextBlocks[2][2].setVisible(true);
break;
case4:
nextBlocks[1][1].setBackground(Color.magenta);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.magenta);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][0].setBackground(Color.magenta);
nextBlocks[1][0].setVisible(true);
nextBlocks[2][0].setBackground(Color.magenta);
nextBlocks[2][0].setVisible(true);
break;
case5:
nextBlocks[1][1].setBackground(Color.darkGray);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.darkGray);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][0].setBackground(Color.darkGray);
nextBlocks[1][0].setVisible(true);
nextBlocks[2][1].setBackground(Color.darkGray);
nextBlocks[2][1].setVisible(true);
break;
case6:
nextBlocks[1][1].setBackground(Color.red);
nextBlocks[1][1].setVisible(true);
nextBlocks[1][2].setBackground(Color.red);
nextBlocks[1][2].setVisible(true);
nextBlocks[1][3].setBackground(Color.red);
nextBlocks[1][3].setVisible(true);
nextBlocks[1][0].setBackground(Color.red);
nextBlocks[1][0].setVisible(true);
break;
}
}
privatevoidaddGameMenu(){
JMenuBarjmunebar=newJMenuBar();//菜单栏
this.setJMenuBar(jmunebar);
JMenufile_JMenu=newJMenu("文件");
JMenuItemnewgame_JMenuItem=newJMenuItem("新游戏");
JMenuItempause_JMenuItem=newJMenuItem("暂停");
JMenuItemcontinue_JMenuItem=newJMenuItem("继续");
JMenuItemexit_JMenuItem=newJMenuItem("退出");
file_JMenu.add(newgame_JMenuItem);
file_JMenu.add(pause_JMenuItem);
file_JMenu.add(continue_JMenuItem);
file_JMenu.addSeparator();
file_JMenu.add(exit_JMenuItem);
jmunebar.add(file_JMenu);
JMenuopinion_JMenu=newJMenu("选项");
JMenumode_JMenu=newJMenu("模式");
JMenuItemsinglemode_JMenuItem=newJMenuItem("单人模式");
JMenuItemdoublemode_JMenuItem=newJMenuItem("双人模式");
mode_JMenu.add(singlemode_JMenuItem);
mode_JMenu.add(doublemode_JMenuItem);
opinion_JMenu.add(mode_JMenu);
JMenuItemlevelset_JMenuItem=newJMenuItem("级别设置");
JMenuItemheriorecord_JMenuItem=newJMenuItem("英雄榜");
opinion_JMenu.add(levelset_JMenuItem);
opinion_JMenu.add(heriorecord_JMenuItem);
jmunebar.add(opinion_JMenu);
JMenuhelp_JMenu=newJMenu("帮助");
JMenuItemhelp_JMenuItem=newJMenuItem("游戏帮助");
JMenuItemaboutgame_JMenuItem=newJMenuItem("关于稀饭的俄罗斯");
help_JMenu.add(help_JMenuItem);
help_JMenu.addSeparator();
help_JMenu.add(aboutgame_JMenuItem);
jmunebar.add(help_JMenu);
}
publicvoidkeyPressed(KeyEvente){
if(!
isEnd&&!
isPause){
if(e.getKeyCode()==KeyEvent.VK_UP)block.setDirection(block.TURN;
if(e.getKeyCode()==KeyEvent.VK_DOWN)block.setDirection(block.DOWN);
if(e.getKeyCode()==KeyEvent.VK_LEFT)block.setDirection(block.LEFT);
if(e.getKeyCode()==KeyEvent.VK_RIGHT)block.setDirection(block.RIGHT);
}
}
publicvoidkeyReleased(KeyEvente){}
publicvoidkeyTyped(KeyEvente){}
publicstaticvoidmain(String[]args){
newGameMainFrame();
}
}
//方块根类
importjava.awt.*;
importjavax.swing.*;
//所有方块的根类,作为主窗体类的内部类
publicabstractclassBlock{
int[]rows;//四个小方块的行号
int[]columns;//四个小方块的列号
Colorcolor;
BooleanisToBottom,isToTop,isClear;//到顶,到底和是否可以清除方块以计分的标志
finalintLEFT=1;//方块动作标志常量
finalintRIGHT=2;
finalintTURN=3;
finalintDOWN=4;
finalintNORMAL=5;
intdirection;//动作标志
intstatus;//方块翻转的当前状态
GameMainFramegmf;
publicBlock(GameMainFramegmf){
this.gmf=gmf;
rows=newint[4];
columns=newint[4];
for(inti=0;i<4;i++){
rows[i]=0;
columns[i]=0;
}
isToBottom=false;
isToTop=false;
isClear=false;
direction=0;
status=1;
}
abstractvoidturn();//抽象方法,由所有方块子类实现(实现各方块的翻转方法)
publicvoidclear(){//清楚方法
for(inti=0;i<4;i++){
gmf.playBlocks[rows[i]][columns[i]].setBackground(Color.lightGray);
gmf.playBlocks[rows[i]][columns[i]].setVisible(false);
}
}
publicvoidshow(){//刷新方法
for(inti=0;i<4;i++){
gmf.playBlocks[rows[i]][columns[i]].setBackground(color);
gmf.playBlocks[rows[i]][columns[i]].setVisible(true);
}
}
publicvoiddownToBottom(){//方块一下到底
while(!
isToBottom)dowmoneLine();
}
publicvoiddowmoneLine(){//方块逐层下落
for(inti=0;i<4;i++){
if(rows[i]==19){
isToBottom=true;
break;
}
if(gmf.flagBlocks[rows[i]+1][columns[i]]!
=0){//方块下有方块则标志到底
isToBottom=true;
if(rows[i]<=2){
isToTop=true;
}
break;
}
}
if(isToTop){//到顶,游戏结束
JOptionPane.showMessageDialog(gmf.gameJPanel,this,"游戏结束!
");
JOptionPane.showMseeageDialog(gmf,"游戏结束!
","游戏结束!
",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(isToBottom){//到底,设置底部标志
for(intj=0;j<4;j++){
gmf.flagBlocks[rows[j]][columns[j]]=1;
}
gmf.score+=5;//每下一个方块加5分
//清楚能够消除的行
for(inti=0;i<20;i++){
isClear=true;
for(intj=0;j<15;j++){
if(gmf.flagBlocks[i][j]=