Java俄罗斯方块单人游戏课程设计.docx
《Java俄罗斯方块单人游戏课程设计.docx》由会员分享,可在线阅读,更多相关《Java俄罗斯方块单人游戏课程设计.docx(55页珍藏版)》请在冰豆网上搜索。
Java俄罗斯方块单人游戏课程设计
JAVA程序设计课程设计报告
课题:
俄罗斯方块单人游戏
姓名:
赵云杰
学号:
201417030202
同组姓名:
刘杨、汪世军、成功
专业班级:
网络工程14102班
指导教师:
谭文学
设计时间:
评阅意见:
评定成绩:
指导老师签名:
年月日
1.系统概述………………………………………………3
2.设计说明书……………………………………………5
3.系统操作界面…………………………………………7
4.源程序编码……………………………………………28
5.测试计划………………………………………………29
6.改进意见…………………………………………………31
7.课程设计心得体会………………………………………32
8.参考书籍、资料…………………………………………34
系统概述
1.1现状分析
在个人电脑日益普及的今天,一些有趣的桌面游戏已经成为人们在使用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们最熟悉的小游戏之一,它以其趣味性强,易上手等诸多特点得到了大众的认可,因此开发此游戏软件可满足人们的一些娱乐的需求。
此俄罗斯方块游戏可以为用户提供一个可在普通个人电脑上运行的,界面美观的,易于控制的俄罗斯方块游戏。
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)
{
this.isColor=isColor;
}
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},//'Z'型的四种状态
{0x6220,0x1700,0x2230,0x0740},//'7'型的四种状态
{0x6440,0x0e20,0x44c0,0x8e00},//反'7'型的四种状态
{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{
for(intj=0;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()
{
for(inti=0;i{
for(intj=0;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(){
for(inti=0;i{
for(intj=0;j{
if(boxes[i][j].isColorBox())
{
ErsBoxbox=canvas.getBox(i+y,j+x);
if(box==null)
continue;
box.setColor(false);
}
}
}
}
privatevoiddisplay()
{
for(inti=0;i{
for(intj=0;j{
if(boxes[i][j].isColorBox())
{
ErsBoxbox=canvas.getBox(y+i,x+j);
if(box==null)
continue;
box.setColor(true);
}
}
}
}
privatebooleanisMoveAble(intnewRow,intnewCol)
{
earse();
for(inti=0;i{
for(intj=0;j{
if(boxes[i][j].isColorBox())
{
ErsBoxbox=canvas.getBox(newRow+i,newCol+j);
if(box==null||(box.isColorBox()))
{
display();
returnfalse;
}
}
}
}
display();
returntrue;
}
privatesynchronizedbooleanmoveTo(intnewRow,intnewCol){
if(!
isMoveAble(newRow,newCol)||!
moving)
returnfalse;
earse();
y=newRow;
x=newCol;
display();
canvas.repaint();
returntrue;
}
privatebooleanisTurnAble(intnewStyle)
{
intkey=0x8000;
earse();
for(inti=0;i{
for(intj=0;j{
if((newStyle&key)!
=0)
{
ErsBoxbox=canvas.getBox(y+i,x+j);
if(box==null||box.isColorBox())
{
display();
returnfalse;
}
}
key>>=1;
}
}
display();
returntrue;
}
privatebooleanturnTo(intnewStyle)
{
if(!
isTurnAble(newStyle)||!
moving)
returnfalse;
earse();
intkey=0x8000;
for(inti=0;i{
for(intj=0;j{
booleanisColor=((newStyle&key)!
=0);
boxes[i][j].setColor(isColor);
key>>=1;
}
}
style=newStyle;
display();
canvas.repaint();
returntrue;
}
}
模块实现:
颜色:
(提示出现时为红色);
(出现时为绿色)
形状:
注:
每一个方块有四种形态,共计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.addSeparator();
mGame.add(mileveldown);
mGame.addSeparator();
mGame.add(miExit);
mControl.add(miPlay);
mControl.addSeparator();
mControl.add(miPause);
mControl.addSeparator();
mControl.add(miResume);
mControl.addSeparator();
mControl.add(miStop);
mhelp.add(miCtrlBlock);
setJMenuBar(bar);
miNewGame.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
stopGame();
reset();
setLevel(initlevel);
}
});
mileveldown.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
intcurLevel=getLevel();
if(curLevel>1)
setLevel(curLevel-1);
}
});
milevelup.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();
}
});
miCtrlBlock.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
reportGameMethod();
}
});
}
publicvoidreset()
{
ctrlPanel.reset();
canvas.reset();
}
publicbooleanisPlaying()
{
returnplaying;
}
publicErsBlockgetCurBlock()
{
returnbl