Java俄罗斯方块单人游戏课程设计.docx
《Java俄罗斯方块单人游戏课程设计.docx》由会员分享,可在线阅读,更多相关《Java俄罗斯方块单人游戏课程设计.docx(51页珍藏版)》请在冰点文库上搜索。
![Java俄罗斯方块单人游戏课程设计.docx](https://file1.bingdoc.com/fileroot1/2023-7/17/508af1fe-26ea-460e-a8ad-7829b613c49e/508af1fe-26ea-460e-a8ad-7829b613c49e1.gif)
Java俄罗斯方块单人游戏课程设计
Java俄罗斯方块单人游戏课程设计
JAVA程序设计课程设计报告
课题:
俄罗斯方块单人游戏
姓名:
赵云杰
学号:
201417030202
同组姓名:
刘杨、汪世军、成功
专业班级:
网络工程14102班
指导教师:
谭文学
设计时间:
评阅意见:
评定成绩:
指导老师签名:
年月日
1.系统概述………………………………………………32.设计说明书……………………………………………53.系统操作界面…………………………………………74.源程序编码……………………………………………285.测试计划………………………………………………296.改进意见…………………………………………………317(课程设计心得体会………………………………………328.参考书籍、资料…………………………………………34
-2-
系统概述
1.1现状分析
在个人电脑日益普及的今天,一些有趣的桌面游戏已经成为人们在使
用计算机进行工作或学习之余休闲娱乐的首选,而俄罗斯方块游戏是人们
最熟悉的小游戏之一,它以其趣味性强,易上手等诸多特点得到了大众的
认可,因此开发此游戏软件可满足人们的一些娱乐的需求。
此俄罗斯方块游戏可以为用户提供一个可在普通个人电脑上运行的,界面
美观的,易于控制的俄罗斯方块游戏。
1.2项目要求
俄罗斯方块游戏是一款适合大众的游戏软件,它适合不同年龄的人玩。
本软件要实现的功能如下:
(1)游戏区:
玩家可以在游戏区中堆积方块,并能够在游戏过程中随时了解得分情况。
(2)游戏控制:
玩家可以通过游戏控制功能来选择开始新的一局游戏,暂停或退出游戏。
(3)级别设置:
玩家可以根据自己的需要自行设定游戏的开始级别,级别越
高,游戏的速度越快,难度越大。
1.3系统功能模块示意图
显示玩家操作
游戏区
显示操作结果
开始
俄罗斯方块游戏
暂停/继续
提高等级游戏控制
降低等级
退出
-3-
设计说明
1.1游戏区模块
创建游戏区
处理玩家游戏操作游戏区模块
显示游戏结果
1.2控制区模块
开始游戏
暂停游戏
游戏控制模块
初始级别设置
退出游戏
1.3系统流程图
-4-
开始
设置初始级别随机选择方块类
型
创建游戏区
是否到顶部
游戏开局
是方块下落一行
否游戏结束
处理玩家操
作
是否到顶部
1.4模块简介
(1)模块功能简介:
模块4-方块颜色、形状、变化此模块主要通过接口、类,来实现俄罗斯方块的颜色、形状(共28种形态)、变
化(包括移动与变形)
(2)功能模块图:
方块的颜色(出现时为绿色,提示出现时为红色)
方块的形状:
28种方块
方块的变化(移动与变形)
-5-
模块实现代码:
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{
-6-
publicfinalstaticboxesint_rows=4;
publicfinalstaticintboxes_cols=4;
publicfinalstatiflatgcintene=3;
publicfinalstatibetwecintenleveltime=50;
privatefinalstatiblockcintkindnum=7;
privatefinalstaticint=4;blockstatusnum
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=newErsBoboxesx[_rowsboxes][_cols];
privateintstyle,y,x,level;
privatebooleanpausing=false,moving=true;
ErsBlock(intstyle,inty,intx,intlevel,GameCanvascanvas)public
{
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
-7-
{
sleepbetwe(enleveltime
*(ErsBlocksGmaxleame.vel-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;
}
-8-
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-9-
{
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;
}
-10-
}
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;
}
}
模块实现:
颜色:
(提示出现时为红色);
(出现时为绿色)
-11-
形状:
注:
每一个方块有四种形态,共计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("开始"),
-12-
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);
-13-
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);
}
});
-14-
miPlay.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
playGame();
}
});
miPause.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
pauseGame();
}
});
miResume.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
resumeGame();
}
});
miStop.addActionListener(newActionListener(){
ActionEventae)publicvoidactionPerformed(
{
stopGame();
}
});
miCtrlBlock.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae)
{
reportGameMethod();
}
});
}
publicvoidreset()
{
ctrlPanel.reset();
canvas.reset();
}
publicbooleanisPlaying()
{
returnplaying;
}
publicErsBlockgetCurBlock()
-15-
{
returnblock;
}
publicGameCanvasgetCanvas()
{
returncanvas;
}
publicvoidplayGame()
{
play();
ctrlPanel.setPlayButtonEnable(false);
miPlay.setEnabled(false);
ctrlPanel.requestFocu