综合运用所学知识编写相对复杂的程序.docx

上传人:b****2 文档编号:317283 上传时间:2023-04-28 格式:DOCX 页数:31 大小:33.77KB
下载 相关 举报
综合运用所学知识编写相对复杂的程序.docx_第1页
第1页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第2页
第2页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第3页
第3页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第4页
第4页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第5页
第5页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第6页
第6页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第7页
第7页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第8页
第8页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第9页
第9页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第10页
第10页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第11页
第11页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第12页
第12页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第13页
第13页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第14页
第14页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第15页
第15页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第16页
第16页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第17页
第17页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第18页
第18页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第19页
第19页 / 共31页
综合运用所学知识编写相对复杂的程序.docx_第20页
第20页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

综合运用所学知识编写相对复杂的程序.docx

《综合运用所学知识编写相对复杂的程序.docx》由会员分享,可在线阅读,更多相关《综合运用所学知识编写相对复杂的程序.docx(31页珍藏版)》请在冰点文库上搜索。

综合运用所学知识编写相对复杂的程序.docx

综合运用所学知识编写相对复杂的程序

实验项目:

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]=

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

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2