Java俄罗斯方块单人游戏课程设计Word格式.docx
《Java俄罗斯方块单人游戏课程设计Word格式.docx》由会员分享,可在线阅读,更多相关《Java俄罗斯方块单人游戏课程设计Word格式.docx(55页珍藏版)》请在冰点文库上搜索。
在个人电脑日益普及的今天,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一,它以其趣味性强,易上手等诸多特点得到了大众的认可,因此开发此游戏软件可满足人们的一些娱乐的需求。
此俄罗斯方块游戏可以为用户提供一个可在普通个人电脑上运行的,界面美观的,易于控制的俄罗斯方块游戏。
1.2项目要求
俄罗斯方块游戏是一款适合大众的游戏软件,它适合不同年龄的人玩。
本软件要实现的功能如下:
(1)游戏区:
玩家可以在游戏区中堆积方块,并能够在游戏过程中随时了解得分情况。
(2)游戏控制:
玩家可以通过游戏控制功能来选择开始新的一局游戏,暂停或退出游戏。
(3)级别设置:
玩家可以根据自己的需要自行设定游戏的开始级别,级别越高,游戏的速度越快,难度越大。
1.3系统功能模块示意图
设计说明
1.1游戏区模块
1.2控制区模块
1.3系统流程图
1.4模块简介
(1)模块功能简介:
模块4-方块颜色、形状、变化
此模块主要通过接口、类,来实现俄罗斯方块的颜色、形状(共28种形态)、变化(包括移动与变形)
(2)功能模块图:
模块实现代码:
classErsBoximplementsCloneable
{
privatebooleanisColor;
privateDimensionsize=newDimension();
publicErsBox(booleanisColor)
{
this.isColor=isColor;
}
publicbooleanisColorBox()
returnisColor;
publicvoidsetColor(booleanisColor)
publicDimensiongetSize()
returnsize;
publicvoidsetSize(Dimensionsize)
this.size=size;
publicObjectclone()
Objectcloned=null;
try
{
cloned=super.clone();
}catch(Exceptionex)
ex.printStackTrace();
}
returncloned;
}
classErsBlockextendsThread
publicfinalstaticintboxes_rows=4;
publicfinalstaticintboxes_cols=4;
publicfinalstaticintflatgene=3;
publicfinalstaticintbetweenleveltime=50;
privatefinalstaticintblockkindnum=7;
privatefinalstaticintblockstatusnum=4;
publicfinalstaticint[][]STYLES={//共28种状态
{0x0f00,0x4444,0x0f00,0x4444},//长条型的四种状态
{0x04e0,0x0464,0x00e4,0x04c4},//'
T'
型的四种状态
{0x4620,0x6c00,0x4620,0x6c00},//反'
Z'
{0x2640,0xc600,0x2640,0xc600},//'
{0x6220,0x1700,0x2230,0x0740},//'
7'
{0x6440,0x0e20,0x44c0,0x8e00},//反'
{0x0660,0x0660,0x0660,0x0660},//方块的四种状态
};
privateGameCanvascanvas;
privateErsBox[][]boxes=newErsBox[boxes_rows][boxes_cols];
privateintstyle,y,x,level;
privatebooleanpausing=false,moving=true;
publicErsBlock(intstyle,inty,intx,intlevel,GameCanvascanvas){
this.style=style;
this.y=y;
this.x=x;
this.level=level;
this.canvas=canvas;
intkey=0x8000;
for(inti=0;
i<
boxes.length;
i++)
for(intj=0;
j<
boxes[i].length;
j++)
{
booleanisColor=((style&
key)!
=0);
boxes[i][j]=newErsBox(isColor);
key>
>
=1;
}
display();
publicvoidrun()
while(moving)
try
sleep(betweenleveltime
*(ErsBlocksGame.maxlevel-level+flatgene));
}catch(InterruptedExceptionie)
ie.printStackTrace();
if(!
pausing)
moving=(moveTo(y+1,x)&
&
moving);
publicvoidmoveLeft()
moveTo(y,x-1);
publicvoidmoveRight()
moveTo(y,x+1);
publicvoidmoveDown()
moveTo(y+1,x);
publicvoidturnNext()
blockkindnum;
blockstatusnum;
j++)
if(STYLES[i][j]==style)
{
intnewStyle=STYLES[i][(j+1)%blockstatusnum];
turnTo(newStyle);
return;
}
publicvoidpauseMove()
pausing=true;
publicvoidresumeMove()
pausing=false;
publicvoidstopMove()
moving=false;
privatevoidearse(){
if(boxes[i][j].isColorBox())
ErsBoxbox=canvas.getBox(i+y,j+x);
if(box==null)
continue;
box.setColor(false);
privatevoiddisplay()
i++)
ErsBoxbox=canvas.getBox(y+i,x+j);
if(box==null)
box.setColor(true);
privatebooleanisMoveAble(intnewRow,intnewCol)
earse();
if(boxes[i][j].isColorBox())
ErsBoxbox=canvas.getBox(newRow+i,newCol+j);
if(box==null||(box.isColorBox()))
{
display();
returnfalse;
}
returntrue;
privatesynchronizedbooleanmoveTo(intnewRow,intnewCol){
if(!
isMoveAble(newRow,newCol)||!
moving)
returnfalse;
y=newRow;
x=newCol;
canvas.repaint();
privatebooleanisTurnAble(intnewStyle)
if((newStyle&
=0)
if(box==null||box.isColorBox())
privatebooleanturnTo(intnewStyle)
isTurnAble(newStyle)||!
booleanisColor=((newStyle&
boxes[i][j].setColor(isColor);
style=newStyle;
模块实现:
颜色:
(提示出现时为红色);
(出现时为绿色)
形状:
注:
每一个方块有四种形态,共计7*4=28种
源程序编码
importjavax.swing.*;
importjava.awt.*;
importjavax.swing.border.Border;
importjava.awt.event.*;
publicclassErsBlocksGameextendsJFrame{
publicfinalstaticintalinescore=100;
publicfinalstaticinteverylevelscore=alinescore*20;
publicfinalstaticintmaxlevel=10;
publicfinalstaticintinitlevel=5;
privateGameCanvascanvas;
privateErsBlockblock;
privatebooleanplaying=false;
privateControlPanelctrlPanel;
privateJMenuBarbar=newJMenuBar();
privateJMenu
mGame=newJMenu("
游戏"
),
mControl=newJMenu("
控制"
mhelp=newJMenu("
帮助"
);
privateJMenuItem
miNewGame=newJMenuItem("
新游戏"
milevelup=newJMenuItem("
提高级数"
mileveldown=newJMenuItem("
降低级数"
miExit=newJMenuItem("
退出"
miPlay=newJMenuItem("
开始"
miPause=newJMenuItem("
暂停"
miResume=newJMenuItem("
重新开始"
miStop=newJMenuItem("
停止"
miCtrlBlock=newJMenuItem("
方块控制键"
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();
privatevoidcreateMenu()
bar.add(mGame);
bar.add(mControl);
bar.add(mhelp);
mGame.add(miNewGame);
mGame.addSeparator();
mGame.add(milevelup);
mGame.add(mileveldown);
mGame.add(miExit);
mControl.add(miPlay);
mControl.addSeparator();
mControl.add(miPause);
mControl.add(miResume);
mControl.add(miStop);
mhelp.add(miCtrlBlock);
setJMenuBar(bar);
miNewGame.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
reset();
setLevel(initlevel);
mileveldown.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
intcurLevel=getLevel();
if(curLevel>
1)
setLevel(curLevel-1);
milevelup.addActionListener(newActionListener(){
setLevel(curLevel+1);
miExit.addActionListener(newActionListener(){
miPlay.addActionListener(newActionListener(){
playGame();
miPause.addActionListener(newActionListener(){
pauseGame();
miResume.addActionListener(newActionListener(){
resumeGame();
miStop.addActionListener(newActionListener(){
miCtrlBlock.addActionListener(newActionListener(){
{
reportGameMethod();
publicvoidreset()
ctrlPanel.reset();
canvas.reset();
publicbooleanisPlaying()
returnplaying;
publicErsBlockgetCurBlock()
returnbl